mybatisplus如何指定使用druid多数据源
时间: 2023-06-29 15:02:22 浏览: 101
MybatisPlus可以通过配置多个数据源来实现对多个数据库的访问,同时也可以使用Druid作为数据源连接池。下面是一个使用Druid多数据源的MybatisPlus配置示例:
1. 引入Druid和MybatisPlus的依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
```
2. 配置数据源连接池,并定义多个数据源:
```java
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DruidDataSource masterDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
public DruidDataSource slaveDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Primary
@Bean(name = "master")
public DataSource master(@Qualifier("masterDataSource") DruidDataSource dataSource) {
return dataSource;
}
@Bean(name = "slave")
public DataSource slave(@Qualifier("slaveDataSource") DruidDataSource dataSource) {
return dataSource;
}
}
```
3. 配置MybatisPlus的SqlSessionFactory,指定要使用的数据源:
```java
@Configuration
@MapperScan(basePackages = {"com.example.mapper"})
public class MybatisPlusConfig {
@Autowired
@Qualifier("master")
private DataSource masterDataSource;
@Autowired
@Qualifier("slave")
private DataSource slaveDataSource;
@Bean
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(masterDataSource);
return factoryBean.getObject();
}
@Bean
public SqlSessionFactory slaveSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(slaveDataSource);
return factoryBean.getObject();
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate(SqlSessionFactory masterSqlSessionFactory) {
return new SqlSessionTemplate(masterSqlSessionFactory);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(SqlSessionFactory slaveSqlSessionFactory) {
return new SqlSessionTemplate(slaveSqlSessionFactory);
}
}
```
4. 在Mapper接口中使用`@Mapper`注解指定要使用的SqlSessionTemplate:
```java
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
User selectById(@Param("id") Long id);
}
```
```java
@Mapper
public interface OrderMapper {
@Select("select * from order where user_id = #{userId}")
List<Order> selectByUserId(@Param("userId") Long userId);
}
```
```java
@Service
public class UserService {
@Autowired
@Qualifier("masterSqlSessionTemplate")
private SqlSessionTemplate masterSqlSessionTemplate;
@Autowired
@Qualifier("slaveSqlSessionTemplate")
private SqlSessionTemplate slaveSqlSessionTemplate;
@Autowired
private UserMapper userMapper;
public User selectUserById(Long id) {
return userMapper.selectById(id);
}
}
```
```java
@Service
public class OrderService {
@Autowired
@Qualifier("slaveSqlSessionTemplate")
private SqlSessionTemplate slaveSqlSessionTemplate;
@Autowired
private OrderMapper orderMapper;
public List<Order> selectOrdersByUserId(Long userId) {
return orderMapper.selectByUserId(userId);
}
}
```
通过以上配置,就可以实现使用Druid连接池的MybatisPlus多数据源配置。
阅读全文