springboot mybatis-plus 配置多数据源
时间: 2023-09-18 09:05:52 浏览: 139
要在Spring Boot中配置多数据源使用mybatis-plus,你可以按照以下步骤进行操作:
1. 首先,在`application.properties`(或`application.yml`)文件中配置你的数据源信息。假设你有两个数据源,分别为`datasource1`和`datasource2`,你可以在配置文件中添加以下内容:
```properties
# 数据源1
spring.datasource.datasource1.url=jdbc:mysql://localhost:3306/db1
spring.datasource.datasource1.username=username1
spring.datasource.datasource1.password=password1
spring.datasource.datasource1.driver-class-name=com.mysql.jdbc.Driver
# 数据源2
spring.datasource.datasource2.url=jdbc:mysql://localhost:3306/db2
spring.datasource.datasource2.username=username2
spring.datasource.datasource2.password=password2
spring.datasource.datasource2.driver-class-name=com.mysql.jdbc.Driver
```
2. 创建两个数据源的配置类,用于配置和管理数据源。例如,创建`DataSource1Config`和`DataSource2Config`类,并在类上添加注解`@Configuration`。
```java
@Configuration
public class DataSource1Config {
@Bean(name = "dataSource1")
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
}
@Configuration
public class DataSource2Config {
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
}
```
3. 创建两个`SqlSessionFactory`,分别指定对应的数据源。
```java
@Configuration
@MapperScan(basePackages = "com.example.mapper1", sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisPlusConfig1 {
@Autowired
@Qualifier("dataSource1")
private DataSource dataSource1;
@Bean(name = "sqlSessionFactory1")
public SqlSessionFactory sqlSessionFactory1() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(dataSource1);
return factoryBean.getObject();
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper2", sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisPlusConfig2 {
@Autowired
@Qualifier("dataSource2")
private DataSource dataSource2;
@Bean(name = "sqlSessionFactory2")
public SqlSessionFactory sqlSessionFactory2() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(dataSource2);
return factoryBean.getObject();
}
}
```
4. 创建两个事务管理器,分别指定对应的数据源。
```java
@Configuration
public class TransactionManager1Config {
@Autowired
@Qualifier("dataSource1")
private DataSource dataSource1;
@Bean(name = "transactionManager1")
public DataSourceTransactionManager transactionManager1() {
return new DataSourceTransactionManager(dataSource1);
}
}
@Configuration
public class TransactionManager2Config {
@Autowired
@Qualifier("dataSource2")
private DataSource dataSource2;
@Bean(name = "transactionManager2")
public DataSourceTransactionManager transactionManager2() {
return new DataSourceTransactionManager(dataSource2);
}
}
```
5. 最后,你可以在你的Mapper接口上使用`@Qualifier`注解指定使用哪个数据源。
```java
@Mapper
public interface UserMapper1 {
@Select("SELECT * FROM user")
@Qualifier("sqlSessionFactory1")
List<User> findAll();
}
@Mapper
public interface UserMapper2 {
@Select("SELECT * FROM user")
@Qualifier("sqlSessionFactory2")
List<User> findAll();
}
```
这样,你就成功配置了多数据源的mybatis-plus。请根据实际需求修改配置和代码。
阅读全文