您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: