springboot+mybatis+hikari实现多数据源
时间: 2023-08-02 13:05:51 浏览: 201
springboot+mybatis多数据源集成
在Spring Boot应用中使用Mybatis和Hikari实现多数据源,可以按照以下步骤进行操作:
1. 在pom.xml文件中添加相关依赖,包括spring-boot-starter-jdbc、mybatis-spring-boot-starter、hikariCP等。
2. 在application.yml配置文件中配置多个数据源,例如:
```
spring:
datasource:
master:
jdbc-url: jdbc:mysql://localhost:3306/master
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
slave:
jdbc-url: jdbc:mysql://localhost:3306/slave
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
```
3. 创建多个数据源的配置类,例如:
```
@Configuration
@MapperScan(basePackages = "com.example.demo.master.mapper", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MasterDataSourceConfig {
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Primary
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
```
@Configuration
@MapperScan(basePackages = "com.example.demo.slave.mapper", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
public class SlaveDataSourceConfig {
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
4. 在需要使用数据源的地方使用@Qualifier注解指定数据源,例如:
```
@Service
public class UserService {
@Autowired
@Qualifier("masterSqlSessionTemplate")
private SqlSessionTemplate masterSqlSessionTemplate;
@Autowired
@Qualifier("slaveSqlSessionTemplate")
private SqlSessionTemplate slaveSqlSessionTemplate;
public List<User> getUsersFromMaster() {
return masterSqlSessionTemplate.selectList("com.example.demo.master.mapper.UserMapper.selectAll");
}
public List<User> getUsersFromSlave() {
return slaveSqlSessionTemplate.selectList("com.example.demo.slave.mapper.UserMapper.selectAll");
}
}
```
这样就可以在Spring Boot应用中使用Mybatis和Hikari实现多数据源了。
阅读全文