spring项目如何配置多数据源(已上生产,亲测有效)
更新时间:2023年12月29日 09:21:15 作者:陈賝
这篇文章主要介绍了spring项目如何配置多数据源(已上生产,亲测有效),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
方式一(推荐)
注解形式添加
引入依赖
<!-- dynamic-datasource 多数据源--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.2</version> </dependency>
修改配置文件
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource dynamic: # 设置默认的数据源或者数据源组,默认值即为 master primary: master # 严格模式 匹配不到数据源则报错 strict: true datasource: # 主库数据源 master: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: xxxxx url: jdbc:mysql://127.0.0.1:3306/ifssc-iot?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai # 从库库数据源 slave: driver-class-name: com.taosdata.jdbc.rs.RestfulDriver url: jdbc:TAOS-RS://127.0.0.1:6041/power?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8 username: root password: xxxxx # 从库库数据源 slave2: driver-class-name: com.taosdata.jdbc.rs.RestfulDriver url: jdbc:TAOS-RS://127.0.0.1:6041/power?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8 username: root password: xxxxx druid: enable: true max-active: 50 min-idle: 50 initial-size: 50 max-wait: 60000 time-between-eviction-runs-millis: 60000 validation-query: select server_status() test-on-return: false test-while-idle: true test-on-borrow: false async-close-connection-enable: true async-init: true
使用 直接加在使用的方法上即可
- @Slave
- @DS(“slave2”)
- @DS(“xxx”)
@Slave public ResultVO test() { LambdaQueryWrapper<Temperature> lqw = new QueryWrapper<Temperature>().lambda() .eq(Temperature::getLocation, "杭州") .last("limit 10"); List<Temperature> temperatureList = temperatureMapper.selectList(lqw); return ResultVO.success(temperatureList); } @Slave2 public ResultVO test() { LambdaQueryWrapper<Temperature> lqw = new QueryWrapper<Temperature>().lambda() .eq(Temperature::getLocation, "杭州") .last("limit 10"); List<Temperature> temperatureList = temperatureMapper.selectList(lqw); return ResultVO.success(temperatureList); }
方式二:分开配置mapper,不影响之前的mapper
修改配置文件,添加数据源配置
修改application.yaml
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: db1: enable: true driver-class-name: com.mysql.cj.jdbc.Driver max-active: 100 min-idle: 100 initial-size: 100 max-wait: 60000 time-between-eviction-runs-millis: 60000 validation-query: SELECT 1 FROM DUAL jdbc-url: jdbc:mysql://***:3306/***2?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai username: root password: *** test-on-return: false test-while-idle: true test-on-borrow: false async-close-connection-enable: true async-init: true use-unfair-lock: true db2: enable: true driver-class-name: com.mysql.cj.jdbc.Driver max-active: 100 min-idle: 100 initial-size: 100 max-wait: 60000 time-between-eviction-runs-millis: 60000 validation-query: SELECT 1 FROM DUAL jdbc-url: jdbc:mysql://***:3306/***1?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai username: root password: *** test-on-return: false test-while-idle: true test-on-borrow: false async-close-connection-enable: true async-init: true use-unfair-lock: true #mybatis mybatis-plus: type-aliases-package: com.***.cloud.common.entity # 所有Entity别名类所在包 mapper-locations: classpath:mapper/**/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置打印日志 call-setters-on-nulls: true cache-enabled: false
添加配置类
主数据源配置
import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.logging.stdout.StdOutImpl; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; 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.context.annotation.Scope; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.annotation.Resource; import javax.sql.DataSource; /** * 主数据源配置 * * @author 陈琛 * @date 2022/05/09 */ @Configuration @MapperScan(basePackages = {"com.xxx.cloud.platform.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory") public class PrimaryDataSourceConfig { @Resource private MybatisPlusInterceptor mybatisPlusInterceptor; @Value("${mybatis-plus.mapper-locations}") private String locationPattern; @Value("${mybatis-plus.type-aliases-package}") private String typeAliasesPackage; @Bean @Scope(value = "prototype") @ConfigurationProperties(prefix = "mybatis-plus.configuration") public MybatisConfiguration getCfg() { return new MybatisConfiguration(); } @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource.druid.db1") @Primary public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setPlugins(mybatisPlusInterceptor); bean.setConfiguration(getCfg()); bean.setTypeAliasesPackage(typeAliasesPackage); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern)); return bean.getObject(); } @Bean(name = "transactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
次数据源配置
import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.logging.stdout.StdOutImpl; 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.beans.factory.annotation.Value; 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; import javax.annotation.Resource; import javax.sql.DataSource; /** * 第二个数据源配置 * * @author 陈琛 * @date 2022/05/09 */ @Configuration @MapperScan(basePackages = {"com.xxx.cloud.platform.websiteMapper"}, sqlSessionFactoryRef = "sqlSessionFactory2") public class SecondDataSourceConfig { @Resource private MybatisPlusInterceptor mybatisPlusInterceptor; @Value("${mybatis-plus.mapper-locations}") private String locationPattern; @Value("${mybatis-plus.type-aliases-package}") private String typeAliasesPackage; @Resource private MybatisConfiguration mybatisConfiguration; @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "spring.datasource.druid.db2") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory2") public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setPlugins(mybatisPlusInterceptor); bean.setConfiguration(mybatisConfiguration); bean.setTypeAliasesPackage(typeAliasesPackage); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern)); return bean.getObject(); } @Bean(name = "transactionManager2") public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate2") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
数据源分配
- 主数据源正常使用之前的mapper
- 次数据源使用之后新建的websiteMapper
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring 容器初始化 register 与 refresh方法
这篇文章主要介绍了Spring 容器初始化 register 与 refresh方法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-07-07
最新评论