SpringBoot Mybatis多模块多数据源全局事务配置
项目结构:多模块的项目结构,SpringBoot搭建,整合Mybatis

数据源配置放在common模块,其他模块要使用直接进行jar依赖

mapper和mapper.xml进行分开管理

采用分包方式的创建多数据源,其创建的过程:
1、加载配置信息,创建DataSource;
2、通过数据源创建SqlSessionFactory ;
3、通过数据源,创建事务管理机制;
4、通过SqlSessionFactory 创建 SqlSessionTemplate ;
5、将SqlSessionTemplate 注入到对应的 Mapper 包路径下。
#############################数据库配置################################
spring.datasource.def.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.def.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.def.jdbc-url=jdbc:mysql://192.168.3.6:3306/wcs_dev?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.def.username=root
spring.datasource.def.password=Admin@2019
spring.datasource.dbs.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dbs.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.dbs.jdbc-url=jdbc:mysql://192.168.3.6:3306/wcs_dbs?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.dbs.username=root
spring.datasource.dbs.password=Admin@2019
#############################数据库连接池配置################################
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true
#############################MyBatis配置################################
mybatis.type-aliases-package: com.qcloud.wcs.domain.pojo
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.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 javax.sql.DataSource;
//注册到springboot容器中
@Configuration
//指定该SqlSession对象对应的dao(basePackages , dao扫包 sqlSessionFactoryRef: SqlSessionFactory对象注入到该变量中)
@MapperScan(basePackages = "com.qcloud.wcs.mapper.def",sqlSessionFactoryRef = "sqlSessionFactory1",sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DefDataSourceConfig {
//创建数据源
// 将这个对象放入Spring容器中
@Bean(name = "defaultDataSource")
@ConfigurationProperties(prefix = "spring.datasource.def")
@Primary// 表示这个数据源是默认数据源
DataSource defaultDataSource() {
return DataSourceBuilder.create().build();
}
//创建SqlSessionFactory
@Bean(name = "sqlSessionFactory1")
@Primary// 表示这个数据源是默认数据源
SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(defaultDataSource());
//注意:设置mybatis的xml所在位置
//Invalid bound statement (not found): com.zhongjun.data02.mapper.User02Mapper.innet 不进行配置会报错
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
return bean.getObject();
}
//创建SqlSessionTemplate
@Bean(name = "sqlSessionTemplate1")
@Primary// 表示这个数据源是默认数据源
SqlSessionTemplate sqlSessionTemplate1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
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.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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
//指定该SqlSession对象对应的dao(basePackages , dao扫包 sqlSessionFactoryRef: SqlSessionFactory对象注入到该变量中)
@MapperScan(basePackages = "com.qcloud.wcs.mapper.dbs", sqlSessionFactoryRef = "sqlSessionFactory2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DbsDataSourceConfig {
@Bean(name = "dbsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dbs")
DataSource dbsDataSource() {
return DataSourceBuilder.create().build();
//return new DruidDataSource();
}
@Bean(name = "sqlSessionFactory2")
SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dbsDataSource());
// 设置mybatis的xml所在位置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
return bean.getObject();
}
@Bean(name = "sqlSessionTemplate2")
SqlSessionTemplate sqlSessionTemplate2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory2());
}
}
事务配置:经过测试,全局配置是没有问题的




数据库数据没有被修改,事务是没有问题的。

扫描包的MapperScan可以进行设置,也可以不进行设置,不进行设置需要每一个Mapper添加上@Mapper


注意点:
1、springboot多数据源配置时url已经不再适用,要采用jdbc-url。

2、mapper.xml文件必须配置正确,不然会报错:Invalid bound statement (not found): XXX.XXXMapper.update

1万+




被折叠的 条评论
为什么被折叠?



