【项目】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: 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)
这样即可进行全局事物管理