Spring jdbc 模板:将 sql 语句排除在代码之外的最佳方法是什么
Spring JDBC 模板中似乎没有命名查询支持。我所说的命名查询是指在 java 代码中按名称引用 sql 语句并将实际语句保留在某些配置文件中的工具。
在缺乏现成支持的情况下,我正在研究将 sql 语句保留在 java 代码之外的最佳方法。
以下是替代方案:
- 属性文件
- xml 属性文件
- spring context xml (依赖注入)
注释?
There doesn't seem to be a named query support in Spring JDBC Templates. By named query I mean the facility to reference sql statement by name in java code and keep the actual statements in some configuration file.
In the absence of out-of-box support, I'm researching the best approach to keep the sql statements outside java code.
Here are the alternatives:
- properties file
- xml properties file
- spring context xml (dependency inject)
comments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java - 在外部文件中存储 SQL 语句
我做了一个查询也存储在数据库中的系统,这对于我们所需要的来说绝对是完美的,但非常有趣。进行查询查找的查询也在数据库中,当它崩溃时,这引起了极大的欢闹(不要问)。
Java - Storing SQL statements in an external file
I did one system where queries were stored in the DB, too, which for what we needed was absolutely perfect, but very entertaining. The query to do query lookups was in the DB as well, which caused great hilarity when it broke (don't ask).
您考虑作为替代方案的所有方法都将起作用,但如果您将语句从 Java 类中移出,那么您就会在设计中添加额外的层,这对我来说是不必要的。考虑将语句保留在 Java 类之外的开销。
如果我是你,我将创建一个类并将语句列在 static Final 中。这使得它变得透明,避免不必要的层,并且对于将来维护代码的任何人来说都变得灵活。
例子:
All the approaches you've considered as alternative will work, yet you are adding additional layer to your design if you move the statements out of Java classes which to me is unnecessary. Consider the overhead of keeping the statements outside Java class.
If I were you, I will create a class and have the statement listed in static final. This makes it transparent, avoid unnecessary layers and makes it flexible for whoever is going to maintain your code in the future.
Example:
我更喜欢将所有 SQL 查询存储在属性或 xml 文件中。
然后在程序启动时获取所有查询并将它们存储在 Map 或 Properties 对象中。
即使在代码部署之后,这也提供了更改查询的可行性。
我们可以更改属性文件中的查询,然后重新启动应用程序。
I would prefer to store all the SQL queries in properties or xml file.
Then fetch all the queries and store them in Map or Properties object at the program boot up.
This provides the feasibility to change the query even after code deployment.
We can change the query in property file and then restart the application.