回顾在Spring中配置MyBatis SqlSessionFactory的配置:

  1. <!-- mybatis 的SqlSessionFactory -->
  2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype">
  3. <property name="dataSource" ref="dataSource"/>
  4. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  5. </bean>

所以实际上在Spring Boot中配置MyBatis多数据源的关键在于创建SqlSessionFactory的时候为其分配不同的数据源。

引入依赖

先根据开启Spring Boot开启一个最简单的Spring Boot应用,然后引入如下依赖:

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.3.1</version>
  5. </dependency>
  6. <!-- oracle驱动 -->
  7. <dependency>
  8. <groupId>com.oracle.database.jdbc</groupId>
  9. <artifactId>ojdbc8</artifactId>
  10. <version>21.1.0.0</version>
  11. </dependency>
  12. <!-- mysql驱动 -->
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. </dependency>
  17. <!-- druid数据源驱动 -->
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>druid-spring-boot-starter</artifactId>
  21. <version>1.1.10</version>
  22. </dependency>

多数据源配置

在Spring Boot配置文件application.yml中配置多数据源和 Spring Boot JdbcTemplate配置Druid多数据源 一致。

然后根据application.yml创建两个数据源配置类MysqlDatasourceConfig和OracleDatasourceConfig:

MysqlDatasourceConfig:

  1. @Configuration
  2. @MapperScan(basePackages = MysqlDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
  3. public class MysqlDatasourceConfig {
  4. // mysqldao扫描路径
  5. static final String PACKAGE = "com.springboot.mysqldao";
  6. // mybatis mapper扫描路径
  7. static final String MAPPER_LOCATION = "classpath:mapper/mysql/*.xml";
  8. @Primary
  9. @Bean(name = "mysqldatasource")
  10. @ConfigurationProperties("spring.datasource.druid.mysql")
  11. public DataSource mysqlDataSource() {
  12. return DruidDataSourceBuilder.create().build();
  13. }
  14. @Bean(name = "mysqlTransactionManager")
  15. @Primary
  16. public DataSourceTransactionManager mysqlTransactionManager() {
  17. return new DataSourceTransactionManager(mysqlDataSource());
  18. }
  19. @Bean(name = "mysqlSqlSessionFactory")
  20. @Primary
  21. public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqldatasource") DataSource dataSource)
  22. throws Exception {
  23. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  24. sessionFactory.setDataSource(dataSource);
  25. //如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
  26. sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
  27. .getResources(MysqlDatasourceConfig.MAPPER_LOCATION));
  28. return sessionFactory.getObject();
  29. }
  30. }

上面代码配置了一个名为mysqldatasource的数据源,对应application.yml中spring.datasource.druid.mysql前缀配置的数据库。然后创建了一个名为mysqlSqlSessionFactory的Bean,并且注入了mysqldatasource。与此同时,还分别定了两个扫描路径PACKAGE和MAPPER_LOCATION,前者为Mysql数据库对应的mapper接口地址,后者为对应的mapper xml文件路径。

@Primary标志这个Bean如果在多个同类Bean候选时,该Bean优先被考虑。多数据源配置的时候,必须要有一个主数据源,用@Primary标志该Bean。

同理,接着配置Oracle数据库对应的配置类:

OracleDatasourceConfig:

  1. @Configuration
  2. @MapperScan(basePackages = OracleDatasourceConfig.PACKAGE,
  3. sqlSessionFactoryRef = "oracleSqlSessionFactory")
  4. public class OracleDatasourceConfig {
  5. // oracledao扫描路径
  6. static final String PACKAGE = "com.springboot.oracledao";
  7. // mybatis mapper扫描路径
  8. static final String MAPPER_LOCATION = "classpath:mapper/oracle/*.xml";
  9. @Bean(name = "oracledatasource")
  10. @ConfigurationProperties("spring.datasource.druid.oracle")
  11. public DataSource oracleDataSource() {
  12. return DruidDataSourceBuilder.create().build();
  13. }
  14. @Bean(name = "oracleTransactionManager")
  15. public DataSourceTransactionManager oracleTransactionManager() {
  16. return new DataSourceTransactionManager(oracleDataSource());
  17. }
  18. @Bean(name = "oracleSqlSessionFactory")
  19. public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracledatasource") DataSource dataSource)
  20. throws Exception {
  21. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  22. sessionFactory.setDataSource(dataSource);
  23. //如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
  24. sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
  25. .getResources(OracleDatasourceConfig.MAPPER_LOCATION));
  26. return sessionFactory.getObject();
  27. }
  28. }

测试

配置完多数据源,接下来分别在com.springboot.mysqldao路径和com.springboot.oracledao路径下创建两个mapper接口:

MysqlStudentMapper:

  1. package com.springboot.mysqldao;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper
  6. public interface MysqlStudentMapper {
  7. List<Map<String, Object>> getAllStudents();
  8. }

OracleStudentMapper:

  1. package com.springboot.oracledao;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper
  6. public interface OracleStudentMapper {
  7. List<Map<String, Object>> getAllStudents();
  8. }

接着创建mapper接口对应的实现:

在src/main/resource/mapper/mysql/路径下创建MysqlStudentMapper.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.springboot.mysqldao.MysqlStudentMapper">
  5. <select id="getAllStudents" resultType="java.util.Map">
  6. select * from student
  7. </select>
  8. </mapper>

在src/main/resource/mapper/oracle/路径下创建OracleStudentMapper.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.springboot.oracledao.OracleStudentMapper">
  5. <select id="getAllStudents" resultType="java.util.Map">
  6. select * from student
  7. </select>
  8. </mapper>

Service,Controller以及测试数据同 Spring Boot JdbcTemplate配置Druid多数据源,这里不再赘述。

最终项目目录如下图所示:

Spring Boot MyBatis配置Druid多数据源 - 图1

启动项目,访问:http://localhost:8080/web/querystudentsfrommysql:

Spring Boot MyBatis配置Druid多数据源 - 图2

http://localhost:8080/web/querystudentsfromoracle:

Spring Boot MyBatis配置Druid多数据源 - 图3