本文介绍了使用spring jdbc时可以长整型(+20行sql)的干净方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的应用程序中的一些大型查询外部化为properties \ sql \ xml文件.但是,我想知道是否有人对如何以一种干净的方式实现这一目标提出了一些建议.大多数结果建议使用ORM框架,但是由于某些数据限制,该方法不适用.

I want to externalize some large queries in my application to properties\sql\xml files. However I was wondering if anyone has some recommendations as to how achieve this in a clean fashion. Most results recommend using an ORM framework but this isn't applicable due to some data constraints.

我看了一下: Java-将SQL语句存储在外部文件 ,但是对于一些查询(每次都长于20行),使用此属性名.1,.2等似乎并不是很干净.

I took a look at: Java - Storing SQL statements in an external file but doing this propertyname .1, .2 etc for a few queries that are each longer that 20 lines does not seem that clean.

推荐答案

您可以将查询放入xml文件

You can put your queries in a xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
<entry key="getPersonById">
    <![CDATA[
        Select Name From Person
        Where Id =?
    ]]>

</entry>
<entry key="getPersonBySSN">
    <![CDATA[

    ]]>
</entry>

</properties>

在Spring应用程序上下文中,加载此xml文件

In Spring application Context, load this xml file

<bean id="queryProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations" value="classpath:/queries.xml" />
</bean>

将此bean注入您的DAO类中

Inject this bean in your DAO class

<bean id="myDAO" class="com.xyz.dao.MyDAOImpl">
  <property name="queryProps" ref="queryProps" />
</bean>

在您的DAO类中定义queryProps,不要忘记为此使用setter方法

Define queryProps in your DAO class and don't forget to have setter method for this

 private Properties queryProps;

现在,您可以像这样在DAO中访问查询-

Now you can access the query in your DAO like this -

 String query = queryProps.getProperty("getPersonById");

希望这会有所帮助.

这篇关于使用spring jdbc时可以长整型(+20行sql)的干净方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:46