Mybatis-plus使用PageHelper插件时的冲突问题和分页不生效问题

1.前情提要:
项目之前使用的分页是Mybatis-plus再带的,现在想加入mybatis+PageHelper插件,进行分页
2.开始实施
首先添加分页插件的maven

       <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.jsqlparser</groupId>
                    <artifactId>jsqlparser</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 		PageHelper.startPage(sysLog.getPageNo(),sysLog.getPageSize());
        List<SysLog> pageList = logMapper.logPageList(sysLog);
        PageInfo<SysLog> info = new PageInfo<SysLog>(pageList);

实现效果:
启动项目没有报错,但是分页不生效
其中看过博客日志无数来处理这个问题,最多的就是
PageHelper.startPage这个方法要在查询条件之前,但是对于我这个项目分页无效
3.继续操作
将分页的pom maven换成

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

换了pom 文件后分页确实可以使用了,本来感觉已经解决问题,但是另一哥们说他的mybatisplus自带分页报错失效了
报错问题:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: net.sf.jsqlparser.statement.select.PlainSelect.getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: net.sf.jsqlparser.statement.select.PlainSelect.getGroupByColumnReferences()Ljava/util/List;

这两个问题都是因为jsqparser包版本问题导致的

4.继续处理版本问题
查看自己项目的mybtisplus中引用的是4.0的jsqparser包
在这里插入图片描述
所以在引用分页插件的时候要处理好分页的maven包版本
经过不断的测试,终于找到了分页插件1.3.1版本对应的4.0 jsqparser包

在这里插入图片描述

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.1</version>
       <!--使用spring boot2整合 pagehelper-spring-boot-starter必须排除一下依赖
     因为pagehelper-spring-boot-starter也已经在pom依赖了mybatis与mybatis-spring
     所以会与mybatis-plus-boot-starter中的mybatis与mybatis-spring发生冲突
 -->
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

同时将mybatis的包也给去除了,防止有冲突

启动正常,测试mybatisplus自带分页显示正常,mybatis+分页插件也正常

感受:重要的是两者的版本要一致!!!

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mybatis-plus的动态分页查询可以通过使用插件提供的分页机制来实现。首先,你需要在配置类中添加分页插件的配置。具体的配置类代码如下所示: ```java package com.hxstrive.mybatis_plus; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { /** * 分页插件。如果你不配置,分页插件不生效 */ @Bean public MybatisPlusInterceptor paginationInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 指定数据库方言为 MYSQL interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } ``` 这段代码中,我们创建了一个MybatisPlusInterceptor对象,并添加了PaginationInnerInterceptor作为内部拦截器,指定了数据库方言为MySQL。这样就完成了分页插件的配置。 接下来,你可以在你的Mapper接口中使用Mybatis-plus提供的分页查询方法来进行动态分页查询。示例代码如下所示: ```java import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hxstrive.mybatis_plus.entity.User; import org.apache.ibatis.annotations.Param; public interface UserMapper { /** * 动态分页查询用户 * * @param page 分页参数 * @param age 年龄 * @return 用户列表 */ IPage<User> selectUserByAge(Page<User> page, @Param("age") Integer age); } ``` 在上面的示例代码中,我们使用Mybatis-plus提供的IPagePage类来实现动态分页查询。你可以通过传入Page对象和其他的查询条件来进行分页查询。 总结起来,Mybatis-plus的动态分页查询需要配置分页插件,并在Mapper接口中使用相应的分页方法来实现。这样就可以实现根据条件动态进行分页查询了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Mybatis-plus分页查询](https://blog.csdn.net/weixin_46213083/article/details/125258551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值