目的:
项目中以前有整合mybatis + spring操作数据库,但是以前都是单数据库,现需要实现mybatis访问多数据源,依旧使用spring调用mybatis。
通过注解的方式整合 spring + mybatis 多数据源,同时使两者能够执行事务操作
网上虽然也有类似的文章,但是或多或少有些问题。先将我的解决方法记录下来,以供参考。
步骤
- 导入相关的包
- 有2个数据库,分别是spring1 和 spring2
spring1 有表:teacher和数据
1. CREATE TABLE
2. int(11) NOT NULL
3. varchar(32) DEFAULT NULL,
4. PRIMARY KEY
5. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT
6.
7. INSERT INTO `teacher` VALUES (1, 'teacher1');
8. INSERT INTO `teacher` VALUES (2, 'teacher2');
spring2有表:
1. CREATE TABLE
2. int(11) NOT NULL
3. varchar(32) DEFAULT NULL,
4. int(11) DEFAULT NULL COMMENT '班级',
5. PRIMARY KEY
6. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT
7. INSERT INTO `student` VALUES (1, 'student1', 1);
8. INSERT INTO `student` VALUES (2, 'student2', 2);
9. INSERT INTO `student` VALUES (3, 'student3', 2);
- 功能:同时查询spring1.teacher和spring2.student 且支持事务操作
建立工程如下:
各个包说明:
- com.my.teacher.pojo,com.my.student.pojo:和表结构一一对应的pojo类
Teacher类:
1. public class
2. private
3. private
4. ....
5. }
Student类:
1. public class
2. private
3. private
4. private
5. ...
6. }
- com.my.student.mapper,com.my.teacher.mapper:mybatis的Mapper类,每个mapper都必须有一个同名的mapper.xml
StudentMapper类和对应的mapper.xml
1. public interface
2. List<Student> querys(Map<String,Object> map);
3. }
1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
2. <mapper namespace="com.my.student.mapper.StudentMapper" >
3. <!-- resultMap -->
4. <resultMap id="ResultMap" type="Student" >
5. <id column="iStudentId" property="iStudentId" jdbcType="INTEGER" />
6. <result column="sName" property="sName" jdbcType="VARCHAR" />
7. <result column="iClass" property="iClass" jdbcType="INTEGER" />
8. </resultMap>
9.
10. <!-- querys -->
11. <select id="querys" resultMap="ResultMap" parameterType="map" >
12. select * from student
13. </select>
14.
15. </mapper>
TeacherMapper类和对应的mapper.xml
1. public interface
2. List<Teacher> querys(Map<String,Object> map);
3. }
1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
2. <mapper namespace="com.my.teacher.mapper.TeacherMapper" >
3. <!-- resultMap -->
4. <resultMap id="ResultMap" type="Teacher" >
5. <id column="iTeacherId" property="iTeacherId" jdbcType="INTEGER" />
6. <result column="sName" property="sName" jdbcType="VARCHAR" />
7. </resultMap>
8.
9. <!-- querys -->
10. <select id="querys" resultMap="ResultMap" parameterType="map" >
11. select * from teacher
12. </select>
13.
14. </mapper>
- com.my.student.dao,com.my.student.dao.imp:调用mapper,逐个获取student、teacher记录
1. public interface
2. public void
3. public void
4. public void
5. }
1. @Component
2. @Transactional
3. public class DaoImp implements
4. @Resource private
5. @Resource private
6.
7.
8. @Override
9. public void
10. // TODO Auto-generated method stub
11. this.studentMapper.querys(new
12. for(Student o : l){
13. System.out.println(o);
14. }
15.
16. this.teacherMapper.querys(new
17. for(Teacher o : l2){
18. System.out.println(o);
19. }
20. }
21.
22. @Override
23. public void
24. // TODO Auto-generated method stub
25. this.studentMapper.querys(new
26. for(Student o : l){
27. System.out.println(o);
28. }
29. }
30.
31. @Override
32. public void
33.
34.
35. this.teacherMapper.querys(new
36. for(Teacher o : l2){
37. System.out.println(o);
38. }
39. }
40.
41. }
- com.my.student.dao.imp.test:测试dao类,代码略
- resource:配置类,这里是重点介绍部分
main.properties:属性配置文件,把一些易变的属性配置到这个文件中,比配置到spring的配置文件中,要易于维护。
1. # ALL database are same
2. main.db.sIp=127.0.0.1
3. main.db.sPort=3306
4. main.db.sUsername=mysql
5. main.db.sPassword=password
mybatis_student.xml:spring2的mybatis配置xml
1. <configuration>
2. <settings>
3. <!-- changes from the defaults for testing -->
4. <setting name="cacheEnabled" value="false" />
5. <setting name="useGeneratedKeys" value="true" />
6. <setting name="defaultExecutorType" value="REUSE" />
7. </settings>
8.
9. <typeAliases>
10. <!-- 类重命名 -->
11. <typeAlias type="com.my.student.pojo.Student" alias="Student"/>
12. </typeAliases>
13.
14. </configuration>
mybatis_teacher.xml:spring1的mybatis配置xml 和 mybatis_student相似
1. <configuration>
2. <settings>
3. <!-- changes from the defaults for testing -->
4. <setting name="cacheEnabled" value="false" />
5. <setting name="useGeneratedKeys" value="true" />
6. <setting name="defaultExecutorType" value="REUSE" />
7. </settings>
8.
9. <typeAliases>
10. <!-- 类重命名 -->
11. <typeAlias type="com.my.teacher.pojo.Teacher" alias="Teacher"/>
12. </typeAliases>
13.
14. </configuration>
spring_student.xml:spring2的spring配置文件(具体说明见xml里的注释)
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:aop="http://www.springframework.org/schema/aop"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xmlns:cnotallow="http://www.springframework.org/schema/context"
7. xsi:schemaLocatinotallow="
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
11. >
12.
13. <!-- 开启注释扫描 -->
14. <context:annotation-config />
15. <!-- 包扫描 -->
16. <context:component-scan base-package="
17. com.my.student.dao
18. />
19.
20. <!-- 加载外部属性配置文件 -->
21. <context:property-placeholder locatinotallow="classpath:resource/main.properties" />
22.
23. <!-- 多个数据源的 DataSource名称不能相等 -->
24. <bean id="studentDataSource" destroy-method="close"
25. class="org.apache.commons.dbcp.BasicDataSource">
26. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
27. <property name="url" value="jdbc:mysql://${main.db.sIp}:${main.db.sPort}/spring2" />
28. <property name="username" value="${main.db.sUsername}" />
29. <property name="password" value="${main.db.sPassword}" />
30. <property name="initialSize" value="20"/>
31. <property name="maxActive" value="200"/>
32. <property name="maxIdle" value="30"/>
33. <property name="maxWait" value="1000"/>
34.
35. <property name="testOnBorrow" value="true"/>
36. <property name="testWhileIdle" value="true"/>
37. <property name="testOnReturn" value="true"/>
38. <property name="minEvictableIdleTimeMillis" value="300000" />
39. <property name="timeBetweenEvictionRunsMillis" value="120000" />
40. <property name="validationQuery" value="select 1 from dual" />
41. </bean>
42.
43.
44. <!-- 多个数据源的SqlSessionFactory名称不能相等 -->
45. <bean id="studentSqlSessionFactory" name="studentSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
46. <property name="dataSource" ref="studentDataSource" />
47. <!-- 加载mybatis配置类 -->
48. <property name="configLocation" value="resource/mybatis_student.xml" />
49. </bean>
50. <!-- ScanMapperFiles: mapper类要扫描的包;扫描mapper类 ,也不能同名 -->
51. <bean name="studentMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
52. <!-- mapper类所在的包 -->
53. <property name="basePackage" value="com.my.student.mapper"/>
54. <property name="sqlSessionFactoryBeanName" value="studentSqlSessionFactory"/>
55. </bean>
56.
57.
58.
59. <!-- ================================事务相关控制================================================= -->
60. <!-- 开启通过注解实现事务,设置事务的名称 . 事务管理对象的名称也要不一样。系统默认事务管理对象名称为transactionManager -->
61. <tx:annotation-driven transaction-manager="studentTransactionManager"/>
62. <bean name="studentTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
63. <property name="dataSource" ref="studentDataSource"></property>
64. </bean>
65.
66. </beans>
spring_teacher.xml:spring1的spring配置文件
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:aop="http://www.springframework.org/schema/aop"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xmlns:cnotallow="http://www.springframework.org/schema/context"
7. xsi:schemaLocatinotallow="
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
11. >
12.
13.
14. <!-- 多个数据源的 DataSource名称不能相等 -->
15. <bean id="teacherDataSource" destroy-method="close"
16. class="org.apache.commons.dbcp.BasicDataSource">
17. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
18. <property name="url" value="jdbc:mysql://${main.db.sIp}:${main.db.sPort}/spring1" />
19. <property name="username" value="${main.db.sUsername}" />
20. <property name="password" value="${main.db.sPassword}" />
21. <property name="initialSize" value="20"/>
22. <property name="maxActive" value="200"/>
23. <property name="maxIdle" value="30"/>
24. <property name="maxWait" value="1000"/>
25.
26. <property name="testOnBorrow" value="true"/>
27. <property name="testWhileIdle" value="true"/>
28. <property name="testOnReturn" value="true"/>
29. <property name="minEvictableIdleTimeMillis" value="300000" />
30. <property name="timeBetweenEvictionRunsMillis" value="120000" />
31. <property name="validationQuery" value="select 1 from dual" />
32. </bean>
33.
34.
35. <!-- 多个数据源的SqlSessionFactory名称不能相等 -->
36. <bean id="teacherSessionFactory" name="teacherSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
37. <property name="dataSource" ref="teacherDataSource" />
38. <!-- 加载mybatis的配置文件 -->
39. <property name="configLocation" value="resource/mybatis_teacher.xml" />
40. </bean>
41. <!-- ScanMapperFiles 扫描mapper类 ,也不能同名-->
42. <bean name="teacherMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
43. <!-- mapper类所在的包 -->
44. <property name="basePackage" value="com.my.teacher.mapper"/>
45. <property name="sqlSessionFactoryBeanName" value="teacherSessionFactory"/>
46. </bean>
47.
48. <!-- ================================事务相关控制================================================= -->
49. <!-- 开启通过注解实现事务,设置事务的名称 . 事务管理对象的名称也要不一样。系统默认事务管理对象名称为transactionManager -->
50. <tx:annotation-driven transaction-manager="teachaerTransactionManager" />
51.
52. <bean name="teachaerTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
53. <property name="dataSource" ref="teacherDataSource"></property>
54. </bean>
55. </beans>
重点说明:
- mybatis + spring 中,多数据库源的配置和单数据源配置和相似,最大的不同时,2个数据源的配置的名称不能相等。如上面配置spring配置xml中 DataSource、SqlSessionFactoryBean、MapperScannerConfigurer、DataSourceTransactionManager等在2个spring配置名称中都不相等。尤其是事务管理类DataSourceTransactionManager,必须通过以下方式开启并进行命名:<tx:annotation-driven transaction-manager="teachaerTransactionManager" />
同时部分只演示了同时操作2个数据库,事务操作通过更新操作才能够更好演示出来,其实以上已经实现事务了,这里就省略步骤了。
以上内容源代码见附近