SpringBoot +MybatisPlus集成多数据源的使用案例
作者:小徐Chao努力
这篇文章主要介绍了SpringBoot +MybatisPlus集成多数据源的使用案例,本文分步骤结合实例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
引言
应项目需求,需要引入另外的Mysql数据库,但是项目已经引入一个Mysql,这时有几种方案
- 通过Dynamic-DataSource 框架,无缝集成 但是是动态切换数据源的,跟项目需求不符合,于是采取第二种
- 通过自定义数据源配置类,无缝引入多个数据源,并且基于文件目录隔离
步骤
引入依赖
项目需要引入了MyBatis Plus即可,无需引入额外依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.12</version>
</dependency>修改配置文件
spring:
datasource:
d1:
password: xxxx
username: xxxx
jdbc-url: jdbc:mysql://xxxx:3306/ecshop_wp?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
d2:
password: root
username: root
jdbc-url: jdbc:mysql://127.0.0.1:3306/xg_bi_lcc?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
driver-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) 这样即可进行全局事物管理
到此这篇关于SpringBoot +MybatisPlus集成多数据源的文章就介绍到这了,更多相关SpringBoot MybatisPlus多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
