1、引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
2、配置application.properties中配置多个数据源
#slave dataSource数据源
spring.datasource.slave.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.slave.jdbc-url =jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8
spring.datasource.slave.username =root
spring.datasource.slave.password =root
#master dataSource数据源
spring.datasource.master.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.master.jdbc-url =jdbc:mysql://localhost:3306/study01?useUnicode=true&characterEncoding=utf-8
spring.datasource.master.username =root
spring.datasource.master.password =root
3、创建多个配置类
数据源1;
@Configuration
@MapperScan(basePackages =DataSourceMaterConfig.PACKAGE,sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DataSourceMaterConfig {
// master dao所在的包
public static final String PACKAGE = "com.yehui.mapper.master";
//配置文件所在目录
//private static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
@Bean(name = "mapper/master")
@Primary //默认访问
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource msaterDataSource() {
return DataSourceBuilder.create().build();
}
// 创建Session
@Bean(name = "masterSqlSessionFactory")
@Primary //默认访问
public SqlSessionFactory sqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(msaterDataSource());
/* 使用配置文件是加入
Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources(DataSourceMaterConfig.MAPPER_LOCATION);
sqlSessionFactoryBean.setMapperLocations(mapperLocations);*/
return sqlSessionFactoryBean.getObject();
}
// 数据源事务管理器
@Bean(name = "masterDataSourceTransactionManager")
@Primary //默认访问
public DataSourceTransactionManager masterDataSourceTransactionManager() {
return new DataSourceTransactionManager(msaterDataSource());
}
}
数据源2:
@Configuration @MapperScan(basePackages =DataSourceSalveConfig.PACKAGE,sqlSessionFactoryRef = "slaveSqlSessionFactory") public class DataSourceSalveConfig { // slave dao所在的包 public static final String PACKAGE = "com.yehui.mapper.slave"; //配置文件所在目录 private static final String MAPPER_LOCATION = "classpath:mapper/slave/*.xml"; @Bean(name = "mapper/slave") @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource slaveDataSource() { return DataSourceBuilder.create().build(); } // 创建Session @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(slaveDataSource()); /*使用配置文件时加入 Resource[] mapperLocations = new PathMatchingResourcePatternResolver() .getResources(DataSourceSalveConfig.MAPPER_LOCATION); sqlSessionFactoryBean.setMapperLocations(mapperLocations);*/ return sqlSessionFactoryBean.getObject(); } // 数据源事务管理器 @Bean(name = "slaveDataSourceTransactionManager") public DataSourceTransactionManager slaveDataSourceTransactionManager() { return new DataSourceTransactionManager(slaveDataSource()); } }
4、创建多个包mapper
数据源1:
package com.yehui.mapper.master;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
public interface MasterMapper {
@Select("SELECT * FROM tb_user")
public List<Map<String,Object>> selectList();
}
数据源2:
package com.yehui.mapper.slave;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
public interface SlaveMapper {
@Select("SELECT * FROM tb_user")
public List<Map<String,Object>> selectList();
}
5、创建service类
@Service
public class UserService {
@Autowired
private MasterMapper masterMapper;
@Autowired
private SlaveMapper savlesMapper;
public List<Map<String,Object>> selectList1(){
return masterMapper.selectList();
}
public List<Map<String,Object>> selectList2(){
return savlesMapper.selectList();
}
}
6、测试类
@RunWith(SpringRunner.class) @SpringBootTest public class DataSourceTest { @Autowired private UserService userService; @Test public void test1(){ List<Map<String, Object>> mapList = userService.selectList1(); System.out.println(mapList); } @Test public void test2(){ List<Map<String, Object>> mapList = userService.selectList2(); System.out.println(mapList); } }
注意配置过程中如果报此错误
No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found amon
导致原因@Primary这个注解只能在一个包下存在,不能在其他存在