首先导入mybatis等包,这里就不多说。
下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板
数据源1:
1 package com.aaaaaaa.config.datasource; 2 3 import javax.sql.DataSource; 4 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.mybatis.spring.SqlSessionFactoryBean; 7 import org.mybatis.spring.SqlSessionTemplate; 8 import org.mybatis.spring.annotation.MapperScan; 9 import org.springframework.beans.factory.annotation.Qualifier; 10 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 11 import org.springframework.boot.context.properties.ConfigurationProperties; 12 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Configuration; 14 import org.springframework.context.annotation.Primary; 15 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 16 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 17 18 /** 19 * 20 * @desc 支持多数据源配置,复制一份代码修改所有appdb即可<br> 21 * 以该数据源为主数据源。只能设置@Primary到一份数据源作为主数据源 22 * @author josnow 23 * @date 2018年1月12日 下午9:58:18 24 * @version 1.0.0 25 */ 26 @Configuration 27 @MapperScan(basePackages = "com.aaaaaaa.mapper.appdb", sqlSessionTemplateRef = "appdbSqlSessionTemplate") 28 public class DataSourceConfigappdb { 29 30 @Bean(name = "appdbDataSource") 31 @ConfigurationProperties(prefix = "spring.datasource.appdb") 32 @Primary 33 public DataSource testDataSource() { 34 return DataSourceBuilder.create().build(); 35 } 36 37 @Bean(name = "appdbSqlSessionFactory") 38 @Primary 39 public SqlSessionFactory testSqlSessionFactory(@Qualifier("appdbDataSource") DataSource dataSource) 40 throws Exception { 41 SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 42 bean.setDataSource(dataSource); 43 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/appdb/*.xml")); 44 return bean.getObject(); 45 } 46 47 @Bean(name = "appdbTransactionManager") 48 @Primary 49 public DataSourceTransactionManager testTransactionManager(@Qualifier("appdbDataSource") DataSource dataSource) { 50 return new DataSourceTransactionManager(dataSource); 51 } 52 53 @Bean(name = "appdbSqlSessionTemplate") 54 @Primary 55 public SqlSessionTemplate testSqlSessionTemplate( 56 @Qualifier("appdbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 57 return new SqlSessionTemplate(sqlSessionFactory); 58 } 59 60 }
application.yml配置
spring: datasource: appdb: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://********:3306/******?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: ******** password: ******** initialSize: 5 minIdle: 5 maxActive: 20 testWhileIdle: true validationQuery: select 1 filters: stat,wall,log4j
就这样配置完了第一个数据源和mybatis。
第一个数据源由appdbTransactionManager来管理事务,在使用事务的时候用@Transactional(value = "appdbTransactionManager")注解来表明使用哪个事务管理器。
这里并未采用分布式事务管理器,分布式事务管理器是个大话题,如果你考虑分布式事务可以自己改造。
下面进行第二个数据源和mybatis的配置
1 package com.aaaaaaa.config.datasource; 2 3 import javax.sql.DataSource; 4 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.mybatis.spring.SqlSessionFactoryBean; 7 import org.mybatis.spring.SqlSessionTemplate; 8 import org.mybatis.spring.annotation.MapperScan; 9 import org.springframework.beans.factory.annotation.Qualifier; 10 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 11 import org.springframework.boot.context.properties.ConfigurationProperties; 12 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Configuration; 14 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 15 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 16 17 @Configuration 18 @MapperScan(basePackages = "com.aaaaaaa.mapper.testdb", sqlSessionTemplateRef = "testdbSqlSessionTemplate") 19 public class DataSourceConfigtestdb { 20 21 @Bean(name = "testdbDataSource") 22 @ConfigurationProperties(prefix = "spring.datasource.testdb") 23 public DataSource testDataSource() { 24 return DataSourceBuilder.create().build(); 25 } 26 27 @Bean(name = "testdbSqlSessionFactory") 28 public SqlSessionFactory testSqlSessionFactory(@Qualifier("testdbDataSource") DataSource dataSource) 29 throws Exception { 30 SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 31 bean.setDataSource(dataSource); 32 bean.setMapperLocations( 33 new PathMatchingResourcePatternResolver().getResources("classpath:mapper/testdb/*.xml")); 34 return bean.getObject(); 35 } 36 37 @Bean(name = "testdbTransactionManager") 38 public DataSourceTransactionManager testTransactionManager(@Qualifier("testdbDataSource") DataSource dataSource) { 39 return new DataSourceTransactionManager(dataSource); 40 } 41 42 @Bean(name = "testdbSqlSessionTemplate") 43 public SqlSessionTemplate testSqlSessionTemplate( 44 @Qualifier("testdbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 45 return new SqlSessionTemplate(sqlSessionFactory); 46 } 47 48 }
数据源2的配置
application.yml配置
spring: datasource: appdb: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://********:3306/******?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: ******** password: ******** initialSize: 5 minIdle: 5 maxActive: 20 testWhileIdle: true validationQuery: select 1 filters: stat,wall,log4j
testdb: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://********:3306/******?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: ******** password: ******** initialSize: 5 minIdle: 5 maxActive: 20 testWhileIdle: true validationQuery: select 1 filters: stat,wall,log4j
看一下两个数据源有哪些不同之处:
1:数据源2其实是由数据源1copy了一份代码,并将全部appdb改为testdb
2:此外数据源1有@Primary注解,而数据源2没有@Primary注解。使用@Primary注解是将数据源1作为默认数据源,并且多个数据源必须指定一个默认的数据源,否则会启动报错。
mybatis多数据源就是这么简单