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

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

问题描述

我使用 springCrudRepositorys 进行数据库连接.

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();

推荐答案

如果你的项目设置有resources文件夹,在/META-INF/jpa-named-queries.properties下创建文件并添加键值对为 repoClass.methodName=yoursql.春季数据将回升.

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.

对于较长的查询,最好使用带有 CDATA 标签的 xml 属性文件: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

这篇关于如何将@Query sql 存储在 CrudRepository 的外部文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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