一、模块环境
- SpringBoot版本:2.7.12
- MySQL版本:8.0.33
- Druid版本:1.2.23
- Dynamic版本:3.6.1
二、数据源配置文件
spring:
autoconfigure:
# 排除 Druid 自动配置
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
type: com.alibaba.druid.pool.DruidDataSource
#多数据源配置
dynamic:
primary: db1
strict: false
datasource:
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://IP:3306/库1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: 用户
password: 密码
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://IP:3306/库2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: 用户
password: 密码
#数据源连接池配置
# druid相关配置
druid:
# 初始化 最小 最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
三、数据源配置类指定连接库
package com.example.userauth.config.data;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
/**
* 返回dataSource1数据库的数据源
*/
@Bean(name = "dataSource1")
@Primary//主数据源
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db1")
public DataSource dataSource1() {
return new DruidDataSource();
}
/**
* 返回dataSource1数据库的会话工厂
*/
@Bean(name = "dataSqlSessionFactory1")
@Primary
public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
//MybatisPlus使用的是MybatisSqlSessionFactory 使用普通的加载不到配置文件
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//此处设置为了解决找不到mapper文件的问题
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
//加载时不能使用getResource加载一个,两种方式加载不一样,会记载不到classpath*:mapper/model1/*.xml文件
//同时加载两个model指定一个数据源
sqlSessionFactoryBean.setMapperLocations(ArrayUtil.addAll(
//model1模块的Mapper.xml文件加载
pathMatchingResourcePatternResolver.getResources("classpath*:mapper/model1/*.xml"),
//model2模块的Mapper.xml文件加载
pathMatchingResourcePatternResolver.getResources("classpath*:mapper/model2/*.xml")));
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean.getObject();
}
/**
* 返回dataSource1数据库的事务
*/
@Bean(name = "dataTransactionManager1")
@Primary
public PlatformTransactionManager transactionManager1(@Qualifier("dataSource1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/********************分割*********************/
/**
* 返回dataSource2数据库的数据源
*/
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.db2")
public DataSource dataSource2() {
return new DruidDataSource();
}
/**
* 返回dataSource2数据库的会话工厂
*/
@Bean(name = "dataSqlSessionFactory2")
public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
//model3模块的Mapper.xml文件加载
.getResources("classpath*:mapper/model3/*.xml"));
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean.getObject();
}
/**
* 返回dataSource2数据库的事务
*/
@Bean(name = "dataTransactionManager2")
public PlatformTransactionManager transactionManager2(@Qualifier("dataSource2") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
PS:在配置了配置文件的加载资源后,在文件下对应使用@DS()注解会失效
标签:SpringBoot,数据源,dynamic,springframework,import,org,new,name From: https://blog.csdn.net/qq_74842675/article/details/140296071