Multiple data source configuration: Spring + ibatis
2016-01-22 18:39
309 查看
Spring + ibatis, Multiple data source configuration:
suit for : spring3.2.4RELEASE / ibatis2.3.4.726
<1>
applicationContext.xml
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean= "dataSource1/dataSource2" /> <!-- this is sqlMapClient default datasource, when other dao have not config dataSource, will use this one -->
</property>
</bean>
<bean id="AclassDao" class="com.company.project......dao.impl.AclassDaoImpl">
<property name="dataSource">
<ref bean="dataSource1" />
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="BclassDao" class="com.company.project......dao.impl.BclassDaoImpl">
<property name="dataSource">
<ref bean="dataSource2" />
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<2>
AclassDaoImpl class :
(1)extends SqlMapClientDaoSupport
(2)
@Autowired(required = true)
@Qualifier("sqlMapClient")
public void setSqlMapClientWorkaround(SqlMapClient sqlMapClient) {
this.setSqlMapClient(sqlMapClient);
}
BclassDaoImpl class :
config same to AclassDaoImpl
<!-- can use a baseDaoSupport to extends SqlMapClientDaoSupport and override method setSqlMapClientWorkaround, other class extends the baseDaoSupport. -->
<3>
sqlMapConfig.xml
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings lazyLoadingEnabled="true" useStatementNamespaces="true" />
<sqlMap resource="sqlMapConfig_subConfig1.xml"/>
<sqlMap resource="sqlMapConfig_subConfig2.xml"/>
</sqlMapConfig>
<4>
sqlMapConfig_subConfig1.xml
<!-- can have no namespace -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<select id="selectMappingConfig" resultClass="java.util.HashMap" >
WITH t1 AS
(SELECT SUBSTR((ORAINST), 1, LENGTH(TO_CHAR(ORAINST)) - 2) AS ORAINST
FROM AUTOSYS.REFRESH_STATUS
)
SELECT DISTINCT ORAINST FROM t1
</select>
<select id="mappingName" parameterClass="java.lang.String" resultClass="java.util.HashMap">
$sql$
</select>
</sqlMap>
suit for : spring3.2.4RELEASE / ibatis2.3.4.726
<1>
applicationContext.xml
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean= "dataSource1/dataSource2" /> <!-- this is sqlMapClient default datasource, when other dao have not config dataSource, will use this one -->
</property>
</bean>
<bean id="AclassDao" class="com.company.project......dao.impl.AclassDaoImpl">
<property name="dataSource">
<ref bean="dataSource1" />
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="BclassDao" class="com.company.project......dao.impl.BclassDaoImpl">
<property name="dataSource">
<ref bean="dataSource2" />
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<2>
AclassDaoImpl class :
(1)extends SqlMapClientDaoSupport
(2)
@Autowired(required = true)
@Qualifier("sqlMapClient")
public void setSqlMapClientWorkaround(SqlMapClient sqlMapClient) {
this.setSqlMapClient(sqlMapClient);
}
BclassDaoImpl class :
config same to AclassDaoImpl
<!-- can use a baseDaoSupport to extends SqlMapClientDaoSupport and override method setSqlMapClientWorkaround, other class extends the baseDaoSupport. -->
<3>
sqlMapConfig.xml
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings lazyLoadingEnabled="true" useStatementNamespaces="true" />
<sqlMap resource="sqlMapConfig_subConfig1.xml"/>
<sqlMap resource="sqlMapConfig_subConfig2.xml"/>
</sqlMapConfig>
<4>
sqlMapConfig_subConfig1.xml
<!-- can have no namespace -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<select id="selectMappingConfig" resultClass="java.util.HashMap" >
WITH t1 AS
(SELECT SUBSTR((ORAINST), 1, LENGTH(TO_CHAR(ORAINST)) - 2) AS ORAINST
FROM AUTOSYS.REFRESH_STATUS
)
SELECT DISTINCT ORAINST FROM t1
</select>
<select id="mappingName" parameterClass="java.lang.String" resultClass="java.util.HashMap">
$sql$
</select>
</sqlMap>
相关文章推荐
- Multiple data source configuration spring+mybatis
- Java继承理解
- myEclipse出现An internal error occurred during...的解决方案
- eclipse添加js,html,jsp编辑输入补充提示
- 深度解析 Java 内存原型
- Spring-Mybatis配置文件基本配置
- Jar URL规范
- spring IoC
- java多线程学习笔记
- java 中 return 的两种常见的用法
- 【慕课笔记】第六章 数组 第3节 使用循环操作JAVA中的数组
- spring mvc添加静态资源访问时@Controller无效的解决
- JAVA中对类进行序列化Serializable
- 错误:找不到或无法加载主类
- spring-boot 加载本地静态资源文件路径配置
- spring学习概览
- Android studio导入eclipse项目混淆打包出错
- 【慕课笔记】第六章 数组 第2节 如何使用JAVA中的数组
- java中类,对象,方法的理解
- Ubuntu14.04安装JDK与配置环境变量