简体   繁体   English

当查询存储为外部文件时,如何在 Spring boot Repository 中为本机查询填充 @Query 值

[英]How to populate @Query value for native queries in Spring boot Repository when Queries are stored as external files

Similar question has been asked and answered back in 2015 here .类似的问题已于 2015 年在这里提出并回答。

However , rather than storing queries in XML files , I would like to store them in external .sql files and read query from there.但是,我不想将查询存储在 XML 文件中,而是希望将它们存储在外部 .sql 文件中并从那里读取查询。 To achieve this in our spring boot application - 2.1.5.RELEASE with Java 8 , we are storing queries in resources folder as below为了在我们的 Spring Boot 应用程序 - 2.1.5.RELEASE with Java 8 中实现这一点,我们将查询存储在资源文件夹中,如下所示

src/main/resources - queries - A.sql - B.sql

To read the above queries , I'm reading them in QueryReader class as below要阅读上述查询,我​​正在 QueryReader 类中阅读它们,如下所示

@Component
public class QueryReader {

    public String getQueryFromFile(String fileName) {
        System.out.println("getQueryFromFile() : " + fileName);
        return this.convertInputStreamToString(this.getTemplateAsStream(fileName));
    }




     private InputStream getTemplateAsStream(String queryFileName){
            ClassLoader classLoader = getClass().getClassLoader();
            InputStream iStream =classLoader.getResourceAsStream("queries/" + queryFileName);

            return iStream;
        }
}

And to use it anywhere in the code , i'm having below class , so that i can call it's methods并在代码中的任何地方使用它,我在类下面,这样我就可以调用它的方法

@Component
public class MyQueries {


    @Autowired
    private QueryReader qfr;

    public String queryErroredRecords() {
        return qfr.getQueryFromFile("A.sql");
    }

While using it with JDBCTemplate , this works as expected but when I'm trying to use this method from @Query annotation from Repository , i'm not able to do so with errors as below.将它与 JDBCTemplate 一起使用时,这按预期工作,但是当我尝试从 Repository 的 @Query 注释中使用此方法时,我无法使用以下错误。

Repository Code存储库代码

@Repository
public interface AdminRepository extends JpaRepository<OrderBackupRecordEO, BigDecimal>{

    @Autowired
    public MyQueries queries;


@Query(value = queries.queryErroredRecords() , nativeQuery= true)
    List<String> findX();
}

Above repository is giving errors as below :上面的存储库给出了如下错误:

1.The value for annotation attribute Query.value must be a constant expression (For @Query annotation) 1.注解属性Query.value的值必须是常量表达式(对于@Query注解)

2.The blank final field queries may not have been initialized (For @Autowired annotation) 2.空白的final字段查询可能没有初始化(对于@Autowired注解)

How can I make it work?我怎样才能让它工作?

I would just add a note that may be helpful for you.我只想添加一个可能对您有帮助的注释。

Your solution cannot work just because in java in an interface all the fields (variables) are by default public , static and final and that why you cannot @Autowire any dependencies inside the interface.您的解决方案不能仅仅因为在接口中的 java 中所有字段(变量)默认为publicstaticfinal ,这就是为什么您不能@Autowire接口内的任何依赖项的原因。

That's why it works for your case for JDBCTemplate (which is a class) and doesn't work for spring data repository (which is an interface).这就是为什么它适用于您的 JDBCTemplate (这是一个类)的情况,而不适用于 spring 数据存储库(这是一个接口)。

Spring already has a solution "from the box", you don't need to make all these manipulations. Spring 已经有一个“开箱即用”的解决方案,您不需要进行所有这些操作。

@Repository
public interface AdminRepository extends JpaRepository<OrderBackupRecordEO, BigDecimal> {

    List<String> findX();
}

File src/main/resources/META-INF/jpa-named-queries.properties :文件src/main/resources/META-INF/jpa-named-queries.properties

OrderBackupRecordEO.findX=\
    SELECT record FROM OrderBackupRecordEO record WHERE ...

That's all!就这样! No any loading, manipulations, manual handling - an implementation is hidden in Spring - very simple and reliable.没有任何加载、操作、手动处理——一个实现隐藏在 Spring 中——非常简单和可靠。

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

相关问题 如何在Spring Boot应用程序中测试本地查询? - How to test native queries in Spring Boot application? 在 spring 启动 mongodb 中使用方法查询时如何为所有查询添加默认查询条件 - How to add default query criteria for all the queries when using method queries in spring boot mongodb 在Spring中使用@Query注释的本机查询 - Native queries with @Query annotation in Spring 在使用 findall 或其他查询从存储库中获取属性值后更改属性值时,在 Spring 启动 JPA 中禁用数据库更新 - Disable DB Updates in Spring boot JPA when changing the value of a property after it is fetched from the repository using findall or other queries 具有多个查询的jdbcTemplate查询-Spring Boot - jdbcTemplate query with multiple queries - spring boot 高级查询spring boot - advanced queries spring boot 如何从 spring boot jdbc 存储库中的属性或 yml 文件存储和读取 SQL 查询? - How to store and read SQL queries from properties or yml file in spring boot jdbc repository? 如何加入自定义查询 spring 开机 - How to join custom queries spring boot 如何在Spring Boot中使用MySql数据库查询? - How to use MySql Database queries in Spring Boot? 将数据库配置查询值传递给本机查询 - Passing database config query value to native queries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM