如何在CrudRepository的外部文件中存储@Query sql? [英] How to store @Query sql in external file for CrudRepository?
问题描述
我正在使用 spring
与 CrudRepository
进行数据库连接。
I'm using spring
with CrudRepository
s for database connection.
现在我需要一个很长的(几行)sql查询,我更喜欢在类路径中的文件中维护,而不是直接在代码中。
Now I require a quite long (several lines) sql query that I'd prefer to maintain in a file in classpath, rather than directly inside the code.
但我怎么能做到这一点?
我的仓库看起来如下:
But how could I achieve this? My repo looks as follows:
@Query(value = "<my very long sql query>", nativeQuery = true) //how to inject file content?
@Modifying
@Transactional
public void executeSpecificSql();
推荐答案
如果您的项目设置了资源文件夹,请在 /META-INF/jpa-named-queries.properties
文件并将键值对添加为 repoClass.methodName = yoursql
。 Spring数据将会恢复。
if your project set up has resources folder, create under /META-INF/jpa-named-queries.properties
file and add key value pair as repoClass.methodName=yoursql
. Spring data will pick up.
对于更长的查询,最好使用 xml
属性文件 CDATA
标签: https://stackoverflow.com/a/19128259/1194415
For longer queries it's probably best to use xml
properties file with CDATA
tags: https://stackoverflow.com/a/19128259/1194415
这篇关于如何在CrudRepository的外部文件中存储@Query sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!