基于 Spring Boot 2.x + Mybatis 的多数据源配置

相关链接: 纯洁的微笑 - Spring Boot(七):Mybatis 多数据源最简解决方案

Spring Boot2(四):使用Spring Boot多数据源实现过程

springboot-mybatis多数据源的两种整合方法

分包法

yaml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
datasource:
jwkq:
driver-class-name: ${attendance.datasource.dev.driver-class-name}
jdbc-url: ${attendance.datasource.dev.url}
username: ${attendance.datasource.dev.username}
password: ${attendance.datasource.dev.password}

jwkqold:
driver-class-name: ${attendance.datasource.dev.driver-class-name}
jdbc-url: jdbc:oracle:thin:@192.168.3.206:1521/orcl
username: management
password: management

jwkq配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.jonsnows.attendance.teacher.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
@MapperScan(basePackages = "com.jonsnows.attendance.teacher.mapper", sqlSessionTemplateRef = "jwkqSqlSessionTemplate")
public class JwkqDataSourceConfig {

@Bean(name = "jwkqDataSource")
@ConfigurationProperties(prefix = "spring.datasource.jwkq")
@Primary
public DataSource jwkqDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "jwkqSqlSessionFactory")
@Primary
public SqlSessionFactory jwkqSqlSessionFactory(@Qualifier("jwkqDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));
return bean.getObject();
}

@Bean(name = "jwkqTransactionManager")
@Primary
public DataSourceTransactionManager jwkqTransactionManager(@Qualifier("jwkqDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "jwkqSqlSessionTemplate")
@Primary
public SqlSessionTemplate jwkqSqlSessionTemplate(@Qualifier("jwkqSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

jwkqold配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.jonsnows.attendance.teacher.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
@MapperScan(basePackages = "com.jonsnows.attendance.teacher.mapper_jwkqold", sqlSessionTemplateRef = "jwkqOldSqlSessionTemplate")
public class JwkqOldDataSourceConfig {

@Bean(name = "jwkqOldDataSource")
@ConfigurationProperties(prefix = "spring.datasource.jwkqold")
public DataSource jwkqOldDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "jwkqOldSqlSessionFactory")
public SqlSessionFactory jwkqOldSqlSessionFactory(@Qualifier("jwkqOldDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:jwkqold/*.xml"));
return bean.getObject();
}

@Bean(name = "jwkqOldTransactionManager")
public DataSourceTransactionManager jwkqOldTransactionManager(@Qualifier("jwkqOldDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "jwkqOldSqlSessionTemplate")
public SqlSessionTemplate jwkqOldSqlSessionTemplate(@Qualifier("jwkqOldSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

目录结构

Mapper XML

image-20191122141856569

Mapper 接口

image-20191122142131285

采坑

1、springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)

2、多个数据库的 Mapper 接口和 XML 要注意分包,而且必须是在同级目录下。

3、MybatisPlus的SqlSessionFactory不要使用原生的,请使用MybatisSqlSessionFactory,见官方常见问题

AOP法