一、配置文件
目录
application.yaml
spring:application:name: multi-datasourceprofiles:active: devdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/base?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8username: rootpassword: admin
mybatis:mapper-locations: classpath*:mapper/base/*.xmltype-aliases-package: com.mk.mybatis.multidatasourceconfiguration:map-underscore-to-camel-case: truesys:one-mybatis:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/man?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8username: rootpassword: adminmybatis:mapper-locations: classpath*:mapper/one/*.xmltype-aliases-package: com.mk.mybatis.multidatasource.one.entityconfiguration:map-underscore-to-camel-case: truetwo-mybatis:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/person?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8username: rootpassword: adminmybatis:mapper-locations: classpath*:mapper/two/*.xmltype-aliases-package: com.mk.mybatis.multidatasource.two.entityconfiguration:map-underscore-to-camel-case: true
二、默认mybatis数据源配置
@Configuration
@MapperScan(value = "com.mk.mybatis.multidatasource.base.dao", sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseMybatisConfig {@Primary@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSourceProperties baseDataSourceProperties() {return new DataSourceProperties();}@Primary@Bean("datasource")public DataSource baseDataSource(@Qualifier("baseDataSourceProperties") DataSourceProperties dataSourceProperties) {DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();return dataSource;}@Primary@Bean@ConfigurationProperties(prefix = "mybatis")public MybatisProperties baseMybatisProperties() {return new MybatisProperties();}@Primary@Beanpublic SqlSessionFactory baseSqlSessionFactory(@Qualifier("datasource") DataSource dataSource,@Qualifier("baseMybatisProperties") MybatisProperties mybatisProperties) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(mybatisProperties.resolveMapperLocations());bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());bean.setConfiguration(mybatisProperties.getConfiguration());bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location -> {try {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();return resourceResolver.getResource(location);} catch (Exception var3) {return null;}}).orElse(null));return bean.getObject();}@Primary@Beanpublic DataSourceTransactionManager transactionManager(@Qualifier("datasource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Primary@Beanpublic SqlSessionTemplate baseSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
三、one mybatis数据源配置
@Configuration
@MapperScan(value = "com.mk.mybatis.multidatasource.one.dao", sqlSessionTemplateRef = "oneSqlSessionTemplate")
@Slf4j
public class OneMybatisConfig {@Bean@ConfigurationProperties(prefix = "sys.one-mybatis.datasource")public DataSourceProperties oneDataSourceProperties() {return new DataSourceProperties();}@Beanpublic DataSource oneDataSource(@Qualifier("oneDataSourceProperties") DataSourceProperties dataSourceProperties) {DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();return dataSource;}@Bean@ConfigurationProperties(prefix = "sys.one-mybatis.mybatis")public MybatisProperties oneMybatisProperties(){return new MybatisProperties();}@Beanpublic SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource,@Qualifier("oneMybatisProperties")MybatisProperties mybatisProperties)throws Exception{SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(mybatisProperties.resolveMapperLocations());bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());bean.setConfiguration(mybatisProperties.getConfiguration());bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location->{try {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();return resourceResolver.getResource(location);} catch (Exception var3) {return null;}}).orElse(null));return bean.getObject();}@Beanpublic DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Beanpublic SqlSessionTemplate oneSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}
}
四、two mybatis数据源配置
@Configuration
@MapperScan(value = "com.mk.mybatis.multidatasource.two.dao", sqlSessionTemplateRef = "twoSqlSessionTemplate")
@Slf4j
public class TwoMybatisConfig {@Bean@ConfigurationProperties(prefix = "sys.two-mybatis.datasource")public DataSourceProperties twoDataSourceProperties() {return new DataSourceProperties();}@Beanpublic DataSource twoDataSource(@Qualifier("twoDataSourceProperties") DataSourceProperties dataSourceProperties) {DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();return dataSource;}@Bean@ConfigurationProperties(prefix = "sys.two-mybatis.mybatis")public MybatisProperties twoMybatisProperties() {return new MybatisProperties();}@Beanpublic SqlSessionFactory twoSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource,@Qualifier("twoMybatisProperties") MybatisProperties mybatisProperties) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(mybatisProperties.resolveMapperLocations());bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());bean.setConfigurationProperties(mybatisProperties.getConfigurationProperties());bean.setConfiguration(mybatisProperties.getConfiguration());bean.setConfigLocation(Optional.ofNullable(mybatisProperties.getConfigLocation()).map(location -> {try {ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();return resourceResolver.getResource(location);} catch (Exception var3) {return null;}}).orElse(null));return bean.getObject();}@Beanpublic DataSourceTransactionManager twoTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Beanpublic SqlSessionTemplate twoSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}
五、去掉自动配置
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
public class MultiDatasourceApplication {public static void main(String[] args) {SpringApplication.run(MultiDatasourceApplication.class, args);}}