目录结构:
代码如下:
jdbc.properties
dataSource1.drive=com.mysql.jdbc.Driver
dataSource1.url=jdbc:mysql://ip:3306/lilongdb
dataSource1.usernam=XXXX
dataSource1.password=XXXX
dataSource2.drive=com.mysql.jdbc.Driver
dataSource2.url=jdbc:mysql://ip:3306/test
dataSource2.usernam=XXXX
dataSource2.password=XXXX
spring-mybatis.xml配置文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:conf/*.properties" />
</bean>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass">
<value>${dataSource1.drive}</value>
</property>
<property name="jdbcUrl">
<value>${dataSource1.url}</value>
</property>
<property name="username">
<value>${dataSource1.usernam}</value>
</property>
<property name="password">
<value>${dataSource1.password}</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:conf/mybatis-config.xml" />
<property name="typeAliasesPackage" value="cn.yld.webchatapp.model" />
<property name="mapperLocations" value="classpath:mapper/UserMapper.xml" />
</bean>
<!-- spring与mybatis整合配置,扫描所有dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="cn.yld.webchatapp.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
<!-- 对数据源进行事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<!-- =================================================================== -->
<!-- 数据源2 -->
<bean id="dataSource2" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass">
<value>${dataSource2.drive}</value>
</property>
<property name="jdbcUrl">
<value>${dataSource2.url}</value>
</property>
<property name="username">
<value>${dataSource2.usernam}</value>
</property>
<property name="password">
<value>${dataSource2.password}</value>
</property>
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="configLocation" value="classpath:conf/mybatis-config.xml" />
<property name="typeAliasesPackage" value="cn.yld.webchatapp.model" />
<property name="mapperLocations" value="classpath:mapper/WcAppidMapper.xml" />
</bean>
<!-- spring与mybatis整合配置,扫描所有dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="cn.yld.webchatapp.dao2" p:sqlSessionFactoryBeanName="sqlSessionFactory2" />
<!-- 对数据源进行事务管理 -->
<bean id="transactionManager2"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource2" />
</beans>
测试代码:
package cn.yld.webchatapp.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.yld.webchatapp.dao.UserMapper;
import cn.yld.webchatapp.dao2.WcAppidMapper;
import cn.yld.webchatapp.model.User;
import cn.yld.webchatapp.model.WcAppid;
public class MainTest {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
"conf/spring-mybatis.xml", "conf/spring-mvc-manage.xml", "conf/spring.xml" });
// String[] aa = context.getBeanDefinitionNames();
// for (String b : aa) {
// System.out.println(b);
// }
UserMapper userMapper = (UserMapper) context.getBean("userMapper");
User user = userMapper.selectByPrimaryKey("1");
System.out.println("查询到1机器上面的数据库信息是:"+user.toString());
WcAppidMapper wcAppidMapper = (WcAppidMapper) context.getBean("wcAppidMapper");
WcAppid wcAppid = wcAppidMapper.selectByPrimaryKey("0de87c0e-539f-454d-b9c2-cf53178abd4c");
System.out.println("查询到2机器上面的数据库信息是:"+wcAppid.toString());
}
}