springboot 2 hikaricp 多数据源mybatis如何使用
时间: 2023-09-06 20:04:20 浏览: 151
在Spring Boot 2中使用HikariCP多数据源配置MyBatis相对简单。以下是具体步骤:
首先,在pom.xml文件中添加HikariCP、MyBatis和数据库驱动的依赖。例如,可以添加以下依赖:
```xml
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
```
接下来,配置数据源。在application.properties(或application.yml)文件中,可以设置多个数据源的配置。例如:
```properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.second-datasource.url=jdbc:h2:mem:anotherdb
spring.second-datasource.username=sa
spring.second-datasource.password=
spring.second-datasource.driver-class-name=org.h2.Driver
```
然后,创建多个数据源的配置类。可以使用@Configuration注解创建多个配置类,分别对应不同的数据源。例如:
```java
@Configuration
@MapperScan(basePackages = "com.example.mapper.first",
sqlSessionTemplateRef = "firstSqlSessionTemplate")
public class FirstDataSourceConfig {
@Bean(name = "firstDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource firstDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
@Bean(name = "firstSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean("firstSqlSessionTemplate")
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper.second",
sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class SecondDataSourceConfig {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.second-datasource")
public DataSource secondDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean("secondSqlSessionTemplate")
public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
最后,在相应的Mapper接口中使用@Qualifier注解指定使用的数据源。例如:
```java
@Mapper
public interface FirstMapper {
@Select("SELECT * FROM table1")
@Qualifer("firstSqlSessionTemplate")
List<SomeObject> findAll();
}
@Mapper
public interface SecondMapper {
@Select("SELECT * FROM table2")
@Qualifier("secondSqlSessionTemplate")
List<SomeOtherObject> findAll();
}
```
通过以上步骤,就可以在Spring Boot 2中使用HikariCP配置多数据源并同时使用MyBatis。每个数据源都有自己的配置类和对应的Mapper接口,可以独立地访问不同的数据库。
阅读全文