mybatis連線兩個資料來源
1.db.properties配置檔案設定
driver:com.mysql.jdbc.Driver
f_url:jdbc:mysql://**:3306/**?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
s_url:jdbc:mysql://**:3306/**?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
user:**
pwd:**
2.mybatis配置檔案設定
<!-- 讀取db.properties檔案的內容 -->
<util:properties id="jdbc" location="classpath:conf/db.properties"/>
<!-- 配置DataSource 1-->
<bean id="f_dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
<property name="url"value="#{jdbc.f_url}"/>
<property name="username"value="#{jdbc.user}"/>
<property name="password"value="#{jdbc.pwd}"/>
</bean>
<!-- 配置DataSource 2-->
<bean id="s_dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url"value="#{jdbc.s_url}"/>
<property name="username"value="#{jdbc.user}"/>
<property name="password"value="#{jdbc.pwd}"/>
</bean>
<!-- 定義SqlSessionFactory 1-->
<bean id="f_ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="f_dbcp"></property>
<property name="mapperLocations" value="classpath:mapper/UserMapper.xml"></property>
</bean>
<!-- 定義SqlSessionFactory 2-->
<bean id="s_ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="s_dbcp"></property>
<property name="mapperLocations" value="classpath:mapper/ReceiptMapper.xml"></property>
</bean>
注:定義2個MapperScannerConfigurer 時要保證兩個對映dao介面不能處於同一個包下。不然會發生找不到第二個dao介面方法異常
<!-- 定義MapperScannerConfigurer 1 -->
<bean id="f" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="f_ssf"></property>
<property name="basePackage" value="**"></property>
</bean>
<!-- 定義MapperScannerConfigurer 2 -->
<bean id="s" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="s_ssf"></property>
<property name="basePackage" value="**"></property>
</bean>
3.mapper配置檔案設定
資料來源1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="介面對映位置">
<insert id="saveEnt" parameterType="**">
insert into
trans_receive
(
**
)
values
(
**
)
</insert>
</mapper>
資料來源2
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="介面對映位置">
<select id="findById" parameterType="string" resultType="**">
select * from
**
where
**
</select>
</mapper>
注:帶**需要自行修改