目的:
项目中以前有整合mybatis + spring操作数据库,但是以前都是单数据库,现需要实现mybatis访问多数据源,依旧使用spring调用mybatis。
通过注解的方式整合 spring + mybatis 多数据源,同时使两者能够执行事务操作
网上虽然也有类似的文章,但是或多或少有些问题。先将我的解决方法记录下来,以供参考。
步骤
CREATE TABLE `teacher` ( `iTeacherId` int(11) NOT NULL AUTO_INCREMENT, `sName` varchar(32) DEFAULT NULL, PRIMARY KEY (`iTeacherId`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `teacher` VALUES (1, 'teacher1'); INSERT INTO `teacher` VALUES (2, 'teacher2');
spring2有表:
CREATE TABLE `student` ( `iStudentId` int(11) NOT NULL AUTO_INCREMENT, `sName` varchar(32) DEFAULT NULL, `iClass` int(11) DEFAULT NULL COMMENT '班级', PRIMARY KEY (`iStudentId`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `student` VALUES (1, 'student1', 1); INSERT INTO `student` VALUES (2, 'student2', 2); INSERT INTO `student` VALUES (3, 'student3', 2);
public class Teacher { private Integer iTeacherId; private String sName; .... }
public class Student { private Integer iStudentId; private String sName; private Integer iClass; ... }
public interface StudentMapper { List<Student> querys(Map<String,Object> map); }<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.my.student.mapper.StudentMapper" > <!-- resultMap --> <resultMap id="ResultMap" type="Student" > <id column="iStudentId" property="iStudentId" jdbcType="INTEGER" /> <result column="sName" property="sName" jdbcType="VARCHAR" /> <result column="iClass" property="iClass" jdbcType="INTEGER" /> </resultMap> <!-- querys --> <select id="querys" resultMap="ResultMap" parameterType="map" > select * from student </select> </mapper>
public interface TeacherMapper { List<Teacher> querys(Map<String,Object> map); }<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.my.teacher.mapper.TeacherMapper" > <!-- resultMap --> <resultMap id="ResultMap" type="Teacher" > <id column="iTeacherId" property="iTeacherId" jdbcType="INTEGER" /> <result column="sName" property="sName" jdbcType="VARCHAR" /> </resultMap> <!-- querys --> <select id="querys" resultMap="ResultMap" parameterType="map" > select * from teacher </select> </mapper>
public interface Dao { public void printAll(); public void printStudent(); public void printTeacher(); }
@Component @Transactional public class DaoImp implements Dao { @Resource private StudentMapper studentMapper; @Resource private TeacherMapper teacherMapper; @Override public void printAll() { // TODO Auto-generated method stub List<Student> l = this.studentMapper.querys(new HashMap<String,Object>()); for(Student o : l){ System.out.println(o); } List<Teacher> l2 = this.teacherMapper.querys(new HashMap<String,Object>()); for(Teacher o : l2){ System.out.println(o); } } @Override public void printStudent() { // TODO Auto-generated method stub List<Student> l = this.studentMapper.querys(new HashMap<String,Object>()); for(Student o : l){ System.out.println(o); } } @Override public void printTeacher() { List<Teacher> l2 = this.teacherMapper.querys(new HashMap<String,Object>()); for(Teacher o : l2){ System.out.println(o); } } }
# ALL database are same main.db.sIp=127.0.0.1 main.db.sPort=3306 main.db.sUsername=mysql main.db.sPassword=password
<configuration> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <typeAliases> <!-- 类重命名 --> <typeAlias type="com.my.student.pojo.Student" alias="Student"/> </typeAliases> </configuration>
<configuration> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <typeAliases> <!-- 类重命名 --> <typeAlias type="com.my.teacher.pojo.Teacher" alias="Teacher"/> </typeAliases> </configuration>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 开启注释扫描 --> <context:annotation-config /> <!-- 包扫描 --> <context:component-scan base-package=" com.my.student.dao "/> <!-- 加载外部属性配置文件 --> <context:property-placeholder location="classpath:resource/main.properties" /> <!-- 多个数据源的 DataSource名称不能相等 --> <bean id="studentDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://${main.db.sIp}:${main.db.sPort}/spring2" /> <property name="username" value="${main.db.sUsername}" /> <property name="password" value="${main.db.sPassword}" /> <property name="initialSize" value="20"/> <property name="maxActive" value="200"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="testOnBorrow" value="true"/> <property name="testWhileIdle" value="true"/> <property name="testOnReturn" value="true"/> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="timeBetweenEvictionRunsMillis" value="120000" /> <property name="validationQuery" value="select 1 from dual" /> </bean> <!-- 多个数据源的SqlSessionFactory名称不能相等 --> <bean id="studentSqlSessionFactory" name="studentSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="studentDataSource" /> <!-- 加载mybatis配置类 --> <property name="configLocation" value="resource/mybatis_student.xml" /> </bean> <!-- ScanMapperFiles: mapper类要扫描的包;扫描mapper类 ,也不能同名 --> <bean name="studentMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mapper类所在的包 --> <property name="basePackage" value="com.my.student.mapper"/> <property name="sqlSessionFactoryBeanName" value="studentSqlSessionFactory"/> </bean> <!-- ================================事务相关控制================================================= --> <!-- 开启通过注解实现事务,设置事务的名称 . 事务管理对象的名称也要不一样。系统默认事务管理对象名称为transactionManager --> <tx:annotation-driven transaction-manager="studentTransactionManager"/> <bean name="studentTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="studentDataSource"></property> </bean> </beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 多个数据源的 DataSource名称不能相等 --> <bean id="teacherDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://${main.db.sIp}:${main.db.sPort}/spring1" /> <property name="username" value="${main.db.sUsername}" /> <property name="password" value="${main.db.sPassword}" /> <property name="initialSize" value="20"/> <property name="maxActive" value="200"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="testOnBorrow" value="true"/> <property name="testWhileIdle" value="true"/> <property name="testOnReturn" value="true"/> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="timeBetweenEvictionRunsMillis" value="120000" /> <property name="validationQuery" value="select 1 from dual" /> </bean> <!-- 多个数据源的SqlSessionFactory名称不能相等 --> <bean id="teacherSessionFactory" name="teacherSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="teacherDataSource" /> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="resource/mybatis_teacher.xml" /> </bean> <!-- ScanMapperFiles 扫描mapper类 ,也不能同名--> <bean name="teacherMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mapper类所在的包 --> <property name="basePackage" value="com.my.teacher.mapper"/> <property name="sqlSessionFactoryBeanName" value="teacherSessionFactory"/> </bean> <!-- ================================事务相关控制================================================= --> <!-- 开启通过注解实现事务,设置事务的名称 . 事务管理对象的名称也要不一样。系统默认事务管理对象名称为transactionManager --> <tx:annotation-driven transaction-manager="teachaerTransactionManager" /> <bean name="teachaerTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="teacherDataSource"></property> </bean> </beans>
重点说明:
同时部分只演示了同时操作2个数据库,事务操作通过更新操作才能够更好演示出来,其实以上已经实现事务了,这里就省略步骤了。
以上内容源代码见附近
参考文献: