实现功能:
1: 配置两个数据源,根据不同的mapper路径使用不同的数据连接
2 :使用Druid连接池
3:Druid 可监控多个数据源的sql执行操作
分析:
查看mybatis_spring_boot_stater中关于自动装配的类可知【如上图】,需要自定义实现SqlsessionFactory和SqlSessionTemplate的Bean,其中public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
方法依赖于DataSource数据源。所以需要根据不同的数据源配置,自定义配置不同的数据源Bean。然后配置不同的数据源在实现含有SqlsessionFactory 和SqlSessionTemplate的配置。
1 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--持久层框架mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
<exclusions>
<!-- 排除默认的 HikariCP 数据源 -->
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!--监测-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2 application.yml
spring:
datasource:
first:
password: Liuyifei@@
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/face?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
second:
password: Liuyifei@@
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/project_guli?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
# 设置druid连接池 通用配置
druid:
initial-size: 3
max-active: 8
max-wait: 60000
test-on-borrow: false
test-on-return: false
test-while-idle: true
min-evictable-idle-time-millis: 300000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters
filters: stat,wall,slf4j
filter:
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 5000
stat-view-servlet:
login-password: admin
login-username: admin
enabled: true
allow: 10.157.147.149
url-pattern: /druid/*
reset-enable: fasle
web-stat-filter:
enabled: true
session-stat-enable: true
sessionStatMaxCount: 20
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
3 编码配置Druid数据源
根据application.yml可知,分为数据连接的特殊配置和通用配置。根据springboot和Durid的整合可知,SpringBoot整合Druid数据源(实行监控功能)
spring框架会读取prefix 为spring.datasource.druid的一系列配置信息自动装载成为DruidDataSource的Bean,因此可将通用配置放在spring.datasource.druid下,非通用配置后续再通过setXXX()进行设置
/**
* @Author lyf
* @Date 2023/2/16 - 09:40
* @Description
**/
@Data
@ConfigurationProperties(prefix = "spring.datasource.first")
public class FirstDataSourceProperties {
private String username;
private String password;
private String url;
private String driverClassName;
}
/**
* @Author lyf
* @Date 2023/2/16 - 10:11
* @Description
**/
@Data
@ConfigurationProperties(prefix = "spring.datasource.second")
public class SecondDataSourceProperties {
private String username;
private String password;
private String url;
private String driverClassName;
}
/**
* @Author lyf
* @Date 2023/2/15 - 16:48
* @Description 多数据源配置
* 启用Druid数据源的通用设置和区别配置类数据
**/
@Configuration
@EnableConfigurationProperties({
FirstDataSourceProperties.class,
SecondDataSourceProperties.class
})
public class DatasourceConfiguration {
FirstDataSourceProperties d1;
SecondDataSourceProperties d2;
public DatasourceConfiguration(FirstDataSourceProperties d1,SecondDataSourceProperties d2){
this.d1=d1;
this.d2=d2;
}
@ConfigurationProperties("spring.datasource.druid")
@Primary
@Bean
public DruidDataSource firstDatasource(){
//通过spring.datasource.druid 加载通用配置
DruidDataSource source = new DruidDataSource();
//设置数据源的特殊配置
source.setUsername(d1.getUsername());
source.setPassword(d1.getPassword());
source.setUrl(d1.getUrl());
source.setDriverClassName(d1.getDriverClassName());
return source;
}
@ConfigurationProperties("spring.datasource.druid")
@Bean
public DruidDataSource secondDatasource(){
//通过spring.datasource.druid 加载通用配置
DruidDataSource source = new DruidDataSource();
//设置数据源的特殊配置
source.setUsername(d2.getUsername());
source.setPassword(d2.getPassword());
source.setUrl(d2.getUrl());
source.setDriverClassName(d2.getDriverClassName());
return source;
}
}
使用@ConfigurationProperties注解将配置信息与JavaBean进行绑定。
@EnableConfigurationProperties和@ConfigurationProperties的用法可参考我的上一篇文章 注解ConfigurationProperties、EnableConfigurationProperties的用法
当前配置中仅有数据源的连接信息不同,可通过DruidDataSource 的setXXX()方法进行属性的设定,如有其它的特殊的配置可根据application.yml的配置进行设定
4 Mybatis配置实现不同Mapper路径使用不同的数据源
/**
* @Author lyf
* @Date 2023/2/15 - 16:52
* @Description
* com.example.multimybatisdatasourcedemo.mapper.mapper1 下使用数据源1
**/
@MapperScan(
basePackages = "com.example.multimybatisdatasourcedemo.mapper.mapper1",
sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
@Configuration
public class FirstMybatisConfiguration {
private DataSource firstDatasource;
/**
* 构造函数,注入第一个数据源
* @param firstDatasource
*/
public FirstMybatisConfiguration(@Qualifier("firstDatasource") DataSource firstDatasource) {
this.firstDatasource = firstDatasource;
}
@Bean
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(firstDatasource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryPrimary());
}
}
/**
* @Author lyf
* @Date 2023/2/15 - 16:52
* @Description
* com.example.multimybatisdatasourcedemo.mapper.mapper2 下使用数据源2
**/
@MapperScan(
basePackages = "com.example.multimybatisdatasourcedemo.mapper.mapper2",
sqlSessionFactoryRef = "sqlSessionFactorySecond",
sqlSessionTemplateRef = "sqlSessionTemplateSecond")
@Configuration
public class SecondMybatisConfiguration {
private DataSource secondDatasource;
/**
* 构造函数,注入第二个数据源
* @param secondDatasource
*/
public SecondMybatisConfiguration(@Qualifier("secondDatasource") DataSource secondDatasource) {
this.secondDatasource = secondDatasource;
}
@Bean
public SqlSessionFactory sqlSessionFactorySecond() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(secondDatasource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateSecond() throws Exception {
return new SqlSessionTemplate(sqlSessionFactorySecond());
}
}
能够做到多个数据源的关键点 就是每个数据源所扫描的mapper包不一样,谁扫描到哪个mapper那么该mapper就用哪个数据源,同时都扫到了,就用添加@Primary 的数据源。
用 @Primary 标志该 Bean。标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean优先被考虑。本文抢到mybatis的配置,所以忽略Mapper的编写和Controller的代码编写