springboot多数据源


springboot + mybatis

maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

yml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 数据源
spring:
datasource:
dudao: # duao数据源
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dudao?useUnicode=true&characterEncoding=utf8&nullCatalogMeansCurrent=true
username: root
password: root
bpm: # bpm数据源
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/bpm?useUnicode=true&characterEncoding=utf8&nullCatalogMeansCurrent=true
username: root
password: root

# mybatis配置
mybatis:
mapper-locations:
- classpath:mybatis/**/*.xml
type-aliases-package: com.lo.multipools.domain
configuration:
map-underscore-to-camel-case: true

springboot配置类

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 主数据源
@Configuration
@MapperScan(basePackages = "com.lo.multipools.mapper.bpm", sqlSessionTemplateRef = "bpmSqlSessionTemplate")
public class DataSourceBpm {
@Primary
@Bean("bpmDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.bpm")
public DataSourceProperties bpmDataSourceProperties() {
return new DataSourceProperties();
}

@Primary
@Bean(name = "bpmDataSource")
public DataSource bpmDataSource(@Qualifier("bpmDataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}

@Primary
@Bean("bpmSqlSessionFactory")
public SqlSessionFactory bpmSqlSessionFactory(@Qualifier("bpmDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(
// 配置mapper.xml路劲
new PathMatchingResourcePatternResolver().getResources("classpath*:/mybatis/bpm/**/*.xml"));
return sqlSessionFactory.getObject();
}

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

@Primary
@Bean(name = "bpmSqlSessionTemplate")
public SqlSessionTemplate bpmSqlSessionTemplate(
@Qualifier("bpmSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

// dudao数据源
@Configuration
@MapperScan(basePackages = "com.lo.multipools.mapper.dudao", sqlSessionTemplateRef = "dudaoSqlSessionTemplate")
public class DataSourceDudao {
@Bean(name = "dudaoDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.dudao")
public DataSourceProperties dudaoDataSourceProperties() {
return new DataSourceProperties();
}

@Bean(name = "dudaoDataSource")
public DataSource dudaoDataSource(
@Qualifier("dudaoDataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}

@Bean("dudaoSqlSessionFactory")
public SqlSessionFactory bpmSqlSessionFactory(@Qualifier("dudaoDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(
// 配置mapper.xml路劲
new PathMatchingResourcePatternResolver().getResources("classpath*:/mybatis/dudao/**/*.xml"));
return sqlSessionFactory.getObject();
}

@Bean(name = "dudaoTransactionManager")
public DataSourceTransactionManager bpmTransactionManager(@Qualifier("dudaoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "dudaoSqlSessionTemplate")
public SqlSessionTemplate bpmSqlSessionTemplate(
@Qualifier("dudaoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

mybatis-plus 多数据源

@DS


文章作者: jin.zhang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 jin.zhang !
  目录