mybatis-plus3.4.0之后分页插件的替换

Mybatis-plus3.4.0之前一直重写的PaginationInterceptor作为分页插件,但是在3.4.0以后,删除了不少类,PaginationInterceptor也不幸被删除,致使项目报错。

PaginationInterceptor类中有
在这里插入图片描述
红色区域已经说明使用MybatisPlusInterceptor和PaginationInnerInterceptor替代,

现将版本升级前后的代码拿出来,
升级前:

@Configuration
public class MybatisPlusConfig {


    /**
     * mybatis-plus分页插件<br>
     * 文档:http://mp.baomidou.com<br>
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType("Oracle");
        return paginationInterceptor;
    }

}

升级后:

@Configuration
public class MybatisPlusConfig {
    
    /**
     * mybatis-plus分页插件<br>
     * 文档:http://mp.baomidou.com<br>
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor paginationInterceptor = new MybatisPlusInterceptor();
        PageInterceptorInterceptor pageInterceptor = new PageInterceptorInterceptor(DbType.ORACLE);
        pageInterceptor.setOverflow(true);
        pageInterceptor.setMaxLimit(500L);
        paginationInterceptor.addInnerInterceptor(pageInterceptor);
        paginationInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return paginationInterceptor;
    }

}

可以使用MybatisPlusInterceptor的setProperties方法(本地测试时没有进入这个类中,不清楚为啥)

@Configuration
public class MybatisPlusConfig {

    /**
     * mybatis-plus分页插件<br>
     * 文档:http://mp.baomidou.com<br>
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor paginationInterceptor = new MybatisPlusInterceptor();
        
        Properties properties = new Properties();
        properties.setProperty("@page","com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor");
        properties.setProperty("page:overflow","true");
        properties.setProperty("page:dbType","Oracle");
        properties.setProperty("page:maxLimit","500");
        
        paginationInterceptor.setProperties(properties);
        paginationInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return paginationInterceptor;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
spring boot整合mybatis-plus和pagehelper分页插件是一种常见的开发方式,可以实现数据库的分页查询功能。下面是一个简单示例项目的源码,以供参考: 首先,需要在pom.xml文件中添加相关依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> ``` 在application.properties(或application.yml)文件中配置数据库连接信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis-plus.configuration.map-underscore-to-camel-case=true ``` 创建一个简单的实体类User: ```java public class User { private Long id; private String username; private String password; // 省略getter和setter方法 } ``` 创建一个Mapper接口UserMapper: ```java @Mapper public interface UserMapper extends BaseMapper<User> { // 省略其他方法 List<User> getUsersByPage(Page<User> page, @Param("username") String username); } ``` 创建一个Service接口UserService以及其实现类UserServiceImpl: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUsersByPage(Page<User> page, String username) { return userMapper.getUsersByPage(page, username); } } ``` 在Controller中注入UserService,并进行分页查询操作: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public IPage<User> getUsers(@RequestParam(value = "page", defaultValue = "1") Integer pageNum, @RequestParam(value = "size", defaultValue = "10") Integer pageSize, @RequestParam(value = "username", required = false) String username) { Page<User> page = new Page<>(pageNum, pageSize); return userService.getUsersByPage(page, username); } } ``` 至此,就完成了spring boot整合mybatis-plus和pagehelper分页插件的配置和使用。 请注意,这是一个简单示例项目,实际使用中可能需要根据需求进行适当调整和修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LLZYHHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值