如何在CrudRepository的外部文件中存储@Query sql? [英] How to store @Query sql in external file for CrudRepository?

查看:584
本文介绍了如何在CrudRepository的外部文件中存储@Query sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring CrudRepository 进行数据库连接。

I'm using spring with CrudRepositorys 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆