简体   繁体   English

如何在没有 @Query 的情况下使用 Spring Data JDBC 运行自定义 SQL?

[英]How can I run custom SQL with Spring Data JDBC without @Query?

Is there a possibility to run some custom SQL query generated via 3rd party tools like eg jOOQ and still benefit from Spring Data JDBC mapping features ie @Column annotations?是否有可能运行一些通过 3rd 方工具(例如 jOOQ)生成的自定义 SQL 查询,并且仍然受益于 Spring Data JDBC 映射功能,即@Column注释? I don't want to use @Query annotation.我不想使用@Query注释。

class Model { @Id private Long id; @Column("column") private String prop; }

class MyRepo {
  public Model runQuery() {
    val queryString = lib.generateSQL()
    // run query as if it has been generated by Spring Data JDBC and map
    // results to Model automatically
  }
}

Perhaps JdbcTemplate can solve your problem?也许JdbcTemplate可以解决您的问题? Specifically, one of the queryForObject() methods may be of interest since your are requesting a single object:具体来说, queryForObject()方法之一可能会引起您的兴趣,因为您正在请求单个对象:

class MyRepo {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    MyRepo(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }        

    public Model runQuery() {
        val query = lib.generateQuery();
        return jdbcTemplate.queryForObject(query, Model.class);
    }
}

More information and other use cases can be found in the Spring Guide Accessing Relational Data using JDBC with Spring更多信息和其他用例可以在 Spring Guide Accessing Relational Data using JDBC with Spring 中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring Data JDBC中编写自定义@Query? - How to write a custom @Query in Spring Data JDBC? 如何在不执行SQL查询的情况下验证我的jdbc连接是否数据库正在运行 - How can I validate my jdbc connection whether the data base is up running without executing sql query 如何在 Spring 数据 JDBC 中使用示例查询替换? - How can I replace using Query by Example in Spring Data JDBC? 在 Spring 数据 Jpa 中,如何从两个不同的数据源运行 SQL 联合查询...? - In Spring Data Jpa, how can I run an SQL union query from two different datasources...? 如何在Spring Data中使用Pageable运行运行时生成的查询? - How can I run a runtime generated query with Pageable in Spring Data? 如何在没有 iBatis 的情况下在 JDBC 中运行 SQL 脚本? - How to run SQL script in JDBC without iBatis? 如何在没有注释的情况下使用 Spring 数据 JDBC? - How to use Spring Data JDBC without annotations? 如何在没有实体和 JPA 存储库的 Spring 中运行本机 SQL 查询? - How to run a native SQL query in Spring without an entity and a JPA Repository? Jdbc 返回空列表但 SQL 查询成功获取数据 [Spring] - Jdbc returns empty list but SQL query succesfully gets data [Spring] 使用@(at)符号的Firebird SQL查询 - 如何在JDBC(Jaybird)中运行查询? - Firebird SQL Query with @ (at) sign - How to run the query in JDBC (Jaybird)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM