当前位置: 首页 > news >正文

【项目】SpringBoot +MybatisPlus集成多数据源

引言

应项目需求,需要引入另外的Mysql数据库,但是项目已经引入一个Mysql,这时有几种方案

  1. 通过Dynamic-DataSource 框架,无缝集成 但是是动态切换数据源的,跟项目需求不符合,于是采取第二种
  2. 通过自定义数据源配置类,无缝引入多个数据源,并且基于文件目录隔离

步骤

引入依赖

项目需要引入了MyBatis Plus即可,无需引入额外依赖

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.12</version>
</dependency>

修改配置文件

spring:datasource:d1:password: xxxxusername: xxxxjdbc-url: jdbc:mysql://xxxx:3306/ecshop_wp?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=truedriver-class-name: com.mysql.cj.jdbc.Driverd2:password: rootusername: rootjdbc-url: jdbc:mysql://127.0.0.1:3306/xg_bi_lcc?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=truedriver-class-name: com.mysql.cj.jdbc.Driver

添加配置类

@Configuration
@MapperScan(basePackages = "xg.xx.model.front.service.mysql.mapper1", sqlSessionFactoryRef = "d1SqlSessionFactory")
public class EcshopWpDataSourceConfig {@Bean(name = "d1DataSource")@ConfigurationProperties(prefix = "spring.datasource.d1")public DataSource ecshopWpDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "d1SqlSessionFactory")public SqlSessionFactory ecshopWpSqlSessionFactory(@Qualifier("d1DataSource") DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();//configuration配置bean//MybatisConfiguration configuration = new MybatisConfiguration();//configuration.setMapUnderscoreToCamelCase(true);//configuration.setCacheEnabled(false);// 配置打印sql语句s//configuration.setLogImpl(StdOutImpl.class);// 添加自定义SQL注入//bean.setConfiguration(configuration);//插件对象MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();//动态表名//DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();//可以传多个表名参数,指定哪些表使用MonthTableNameHandler处理表名称//dynamicTableNameInnerInterceptor.setTableNameHandler(new MonthTableNameHandler("t_table_name"));//以拦截器的方式处理表名称//可以传递多个拦截器,即:可以传递多个表名处理器TableNameHandler//mybatisPlusInterceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);//分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));bean.setDataSource(dataSource);// 设置mybatis的xml所在位置bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/d1/*.xml"));bean.setPlugins(mybatisPlusInterceptor);return bean.getObject();}@Bean(name = "d1TransactionManager")public DataSourceTransactionManager ecshopWpTransactionManager(@Qualifier("d1DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}

@Configuration
@MapperScan(basePackages = "xg.xx.model.front.service.mysql.Mapper2", sqlSessionFactoryRef = "d2SqlSessionFactory")
public class XgBiLccDataSourceConfig {@Bean(name = "d2DataSource")@ConfigurationProperties(prefix = "spring.datasource.d2")public DataSource ecshopWpDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "d2SqlSessionFactory")public SqlSessionFactory ecshopWpSqlSessionFactory(@Qualifier("d2DataSource") DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();//configuration配置bean//MybatisConfiguration configuration = new MybatisConfiguration();//configuration.setMapUnderscoreToCamelCase(true);//configuration.setCacheEnabled(false);// 配置打印sql语句s//configuration.setLogImpl(StdOutImpl.class);// 添加自定义SQL注入//bean.setConfiguration(configuration);//插件对象MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();//动态表名//DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();//可以传多个表名参数,指定哪些表使用MonthTableNameHandler处理表名称//dynamicTableNameInnerInterceptor.setTableNameHandler(new MonthTableNameHandler("t_table_name"));//以拦截器的方式处理表名称//可以传递多个拦截器,即:可以传递多个表名处理器TableNameHandler//mybatisPlusInterceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);//分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));bean.setDataSource(dataSource);// 设置mybatis的xml所在位置bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/d2/*.xml"));bean.setPlugins(mybatisPlusInterceptor);return bean.getObject();}@Bean(name = "d2TransactionManager")public DataSourceTransactionManager ecshopWpTransactionManager(@Qualifier("d2DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}

使用案例

@Transactional

@Transactional(value = "d2TransactionManager", rollbackFor = Exception.class)
这样即可进行全局事物管理
http://www.dtcms.com/a/204555.html

相关文章:

  • Day123 | 灵神 | 二叉树 | 找树左下角的值
  • 【python】纤维宽度分布分析与可视化
  • Node.js Express 项目现代化打包部署全指南
  • LAN(局域网)和WAN(广域网)
  • osgEarth中视角由跟随模式切换到漫游模式后没有鼠标拖拽功能问题分析及解决方法
  • 【VSCode】在远程服务器Linux 系统 实现 Anaconda 安装与下载
  • jenkins使用Send build artifacts over SSH发布jar包目录配置
  • AUTOSAR 运行时环境 (RTE)
  • CMake 跨平台构建系统详解
  • C++(26): 标准库 <iterator>
  • 基于python的机器学习(八)—— 评估算法(一)
  • 策略的组合与叠加多策略联合交易
  • 前端面经-nginx/docker
  • RTMP协议解析【三】
  • Linux服务器SOS Report完全指南:收集方法、作用解析与最佳实践
  • WPF···
  • 哥德巴赫猜想
  • 本特利内华达125768-01 RIM i/o模块规范
  • 8.2 线性变换的矩阵
  • QT的自定义控件
  • UI自动化测试框架:PO模式+数据驱动
  • 回表是数据库概念,还是mysql的概念?
  • 业务逻辑篇水平越权垂直越权未授权访问检测插件SRC 项目
  • 86.评论日记
  • 人工智能范式:技术革命下的认知重构
  • 手机充电协议
  • AI智能分析网关V4玩手机检测算法精准管控人员手机行为,搭建智慧化安防监管体系
  • 心联网(社群经济)视角下开源AI智能名片、链动2+1模式与S2B2C商城小程序源码的协同创新研究
  • 小刚说C语言刷题—1153 - 查找“支撑数”
  • 如何理解:什么是IT到OT的技术融合?