springboot整合mybatis-plus实现多数据源的动态切换且支持分页查询
时间: 2023-05-10 14:02:41 浏览: 453
SpringBoot是一个高效的Java开发框架,它能够方便开发者集成MyBatis-Plus实现多数据源的动态切换以及支持分页查询。MyBatis-Plus是一种优秀的ORM框架,它增强了MyBatis的基础功能,并支持通过注解方式进行映射。
首先,我们需要在pom.xml文件中添加MyBatis-Plus和数据库连接池的依赖。在application.yml文件中,我们需要配置多个数据源和对应的连接信息。我们可以定义一个DataSourceConfig用于获取多个数据源,然后在Mapper配置类中使用@MapperScan(basePackages = {"com.test.mapper"})来扫描Mapper接口。
要实现动态切换数据源,我们可以自定义一个注解@DataSource来标注Mapper接口或方法,然后使用AOP拦截数据源切换,实现动态切换。在实现分页查询时,我们可以使用MyBatis-Plus提供的分页插件来支持分页查询。
代码示例:
1. 在pom.xml文件中添加MyBatis-Plus和数据库连接池的依赖。
```
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
```
2. 在application.yml文件中配置多个数据源和对应的连接信息。以两个数据源为例:
```
spring:
datasource:
druid:
db1:
url: jdbc:mysql://localhost:3306/db1
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
db2:
url: jdbc:mysql://localhost:3306/db2
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 指定默认数据源
primary: db1
```
3. 定义一个DataSourceConfig用于获取多个数据源。
```
@Configuration
public class DataSourceConfig {
@Bean("db1")
@ConfigurationProperties("spring.datasource.druid.db1")
public DataSource dataSource1() {
return DruidDataSourceBuilder.create().build();
}
@Bean("db2")
@ConfigurationProperties("spring.datasource.druid.db2")
public DataSource dataSource2() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DataSource dataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 设置数据源映射关系
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put("db1", dataSource1());
dataSourceMap.put("db2", dataSource2());
dynamicDataSource.setTargetDataSources(dataSourceMap);
// 设置默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSource1());
return dynamicDataSource;
}
}
```
4. 在Mapper配置类中使用@MapperScan(basePackages = {"com.test.mapper"})来扫描Mapper接口,并使用@DataSource注解来标注Mapper接口或方法。
```
@Configuration
@MapperScan(basePackages = {"com.test.mapper"})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
@DataSource("db1")
public interface UserMapper {
@Select("select * from user where id = #{id}")
User selectById(@Param("id") Long id);
}
```
5. 实现AOP拦截数据源切换。
```
@Aspect
@Component
public class DataSourceAspect {
@Before("@annotation(ds)")
public void beforeSwitchDataSource(JoinPoint point, DataSource ds) {
String dataSource = ds.value();
if (!DynamicDataSourceContextHolder.containDataSourceKey(dataSource)) {
System.err.println("数据源 " + dataSource + " 不存在,使用默认数据源");
} else {
System.out.println("使用数据源:" + dataSource);
DynamicDataSourceContextHolder.setDataSourceKey(dataSource);
}
}
}
```
6. 分页查询的使用示例:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
@DataSource("db1")
public IPage<User> getUserList(int pageNum, int pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
return userMapper.selectPage(page, null);
}
}
```
以上就是SpringBoot整合MyBatis-Plus实现多数据源的动态切换和分页查询的具体实现过程。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)