spring boot mybatis 多数据源
时间: 2023-07-15 08:15:36 浏览: 118
在Spring Boot中使用MyBatis实现多数据源的方式有很多种,下面介绍两种常用的方法。
方法一:使用Spring Boot的自动配置
1.在application.yml或application.properties中定义多个数据源的配置信息,如下所示:
```
# 数据源1
spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/db_master
spring.datasource.master.username=root
spring.datasource.master.password=123456
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
# 数据源2
spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/db_slave
spring.datasource.slave.username=root
spring.datasource.slave.password=123456
spring.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
```
2.创建多个数据源的连接池和SqlSessionFactory,可以使用Spring Boot自动配置的方式来实现。只需要在配置类上添加@MapperScan注解即可,如下所示:
```
@Configuration
@MapperScan(basePackages = "com.example.mapper")
public class MybatisConfig {
@Bean
@ConfigurationProperties(prefix="spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(masterDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml"));
return bean.getObject();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(slaveDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml"));
return bean.getObject();
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
3.在Mapper接口中使用@Qualifier注解来指定使用哪个数据源,如下所示:
```
public interface UserMapper {
@Select("select * from user")
@Qualifier("masterSqlSessionFactory")
List<User> selectAllMaster();
@Select("select * from user")
@Qualifier("slaveSqlSessionFactory")
List<User> selectAllSlave();
}
```
方法二:手动配置多个数据源
1.定义多个数据源的配置信息,如下所示:
```
@Configuration
public class DataSourceConfig {
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix="spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix="spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
}
```
2.创建多个SqlSessionFactory,如下所示:
```
@Configuration
public class MybatisConfig {
@Autowired
@Qualifier("masterDataSource")
private DataSource masterDataSource;
@Autowired
@Qualifier("slaveDataSource")
private DataSource slaveDataSource;
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(masterDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml"));
return bean.getObject();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(slaveDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml"));
return bean.getObject();
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
3.在Mapper接口中使用@Qualifier注解来指定使用哪个数据源,如下所示:
```
public interface UserMapper {
@Select("select * from user")
@Qualifier("masterSqlSessionFactory")
List<User> selectAllMaster();
@Select("select * from user")
@Qualifier("slaveSqlSessionFactory")
List<User> selectAllSlave();
}
```
以上就是Spring Boot中使用MyBatis实现多数据源的两种常用方法,具体使用哪种方法,可以根据自己的实际情况来选择。
阅读全文