[英]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 中所有字段(变量)默认为public 、 static和final ,这就是为什么您不能@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.