springboot+mybatis多数据源
时间: 2023-08-30 17:11:47 浏览: 152
springboot-mybatis-多数据源
在Spring Boot中使用MyBatis实现多数据源配置,可以通过以下步骤实现:
1. 在`application.properties`文件中配置多个数据源的连接信息,例如:
```
# 数据源1
spring.datasource.primary.url=jdbc:mysql://localhost:3306/db1
spring.datasource.primary.username=user1
spring.datasource.primary.password=pass1
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
# 数据源2
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
spring.datasource.secondary.username=user2
spring.datasource.secondary.password=pass2
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
```
2. 创建多个数据源的配置类,分别继承`org.springframework.boot.autoconfigure.jdbc.DataSourceProperties`类,并使用`@ConfigurationProperties`注解来注入对应的配置信息,例如:
```java
@Configuration
public class PrimaryDataSourceConfig extends DataSourceProperties {
@Bean
@Primary
public DataSource dataSource() {
return createDataSource();
}
private DataSource createDataSource() {
return DataSourceBuilder.create().url(getUrl())
.username(getUsername()).password(getPassword())
.driverClassName(getDriverClassName()).build();
}
}
@Configuration
public class SecondaryDataSourceConfig extends DataSourceProperties {
@Bean
public DataSource dataSource() {
return createDataSource();
}
private DataSource createDataSource() {
return DataSourceBuilder.create().url(getUrl())
.username(getUsername()).password(getPassword())
.driverClassName(getDriverClassName()).build();
}
}
```
3. 创建多个`SqlSessionFactory`,并分别注入对应的数据源,例如:
```java
@Configuration
@MapperScan(basePackages = "com.example.mapper.primary", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryMyBatisConfig {
@Bean
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
return sessionFactoryBean.getObject();
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper.secondary", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryMyBatisConfig {
@Bean
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
return sessionFactoryBean.getObject();
}
}
```
4. 在`Mapper`接口上使用`@Qualifier`注解指定对应的`SqlSessionFactory`,例如:
```java
@Mapper
public interface PrimaryUserMapper {
@Select("SELECT * FROM user")
@Results(id = "userMap", value = {
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name")
})
List<User> findAll();
@Insert("INSERT INTO user(name) VALUES(#{name})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void save(User user);
}
@Mapper
public interface SecondaryUserMapper {
@Select("SELECT * FROM user")
@Results(id = "userMap", value = {
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name")
})
List<User> findAll();
@Insert("INSERT INTO user(name) VALUES(#{name})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void save(User user);
}
```
这样就可以在同一个应用中使用多个数据源了。
阅读全文