我指的是问题的answer,以将SQL Java查询外部化为一个外部文件。如何动态查询中的表名(自定义Util java类,返回带有模式的完整表名)。
<?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 **<!-- How to Dynamically resolve the table Name here-->**
Where Id =?
]]>
</entry>
<entry key="getPersonBySSN">
<![CDATA[
]]>
</entry>
</properties>
在Spring应用程序上下文中,加载此xml文件
<bean id="queryProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations" value="classpath:/queries.xml" />
</bean>
将此bean注入您的DAO类中
<bean id="myDAO" class="com.xyz.dao.MyDAOImpl">
<property name="queryProps" ref="queryProps" />
</bean>
在您的DAO类中定义queryProps,不要忘记为此使用setter方法
private Properties queryProps;
现在,您可以像这样在DAO中访问查询-
String query = queryProps.getProperty("getPersonById");
表名解析器Util类:
public class TableNameResolver{
public static String getTableName(String tableShortName){
//Custom logic that return the full table name
return "Full Table Name";
}
}