Spring 中 PageHelper 不生效问题及解决方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring 中 PageHelper 不生效问题及解决方法

张学徒   2022-12-16 我要评论

使用这个插件时要注意版本的问题,不同的版本可能 PageHelper 不会生效

springboot 导入的 pagehelper 包

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

如果导入的单个 pagehelper 依赖,还需要导入 pagehelper-spring-boot-autoconfigure 依赖

<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>5.3.2</version>
</dependency>
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
</dependency>

Pagehelper这个mybatis插件网上有很多使用的教程,但使用时往往会有个别一直报错的,或者项目起不来的,主要还是版本问题,之前忽略了这个问题,找了各种方法,在maven里引入pagehelper的依赖时,总是项目起不来,网上有很多资料,就是没有找到对症下药的解决方法,琢磨了挺久,无意间在一篇文章上提到版本不兼容问题,于是找了各种版本做对应,最终实现此功能,主要是springboot、mybatis、和Pagehelper插件的版本要对应上,下面我贴出我的这三个的版本。

引用:Pagehelper分页插件-Mybatis

注意 Java 代码中是否格式正确,以下格式

先调用 PageHelper.startPage(page, limit); 进行设置页码和条数,然后进行开始执行查询,最后用 PageInfo 类以这种类的数据格式进行返回。

本地 jar 包导入时

需要导入 pagehelper 和 jsqlparser 和 pagehelper-spring-boot-autoconfigure 这几个 jar 包,且版本差异要比较小,否则可能不能生效

在 maven 中添加以下内容,打包时添加 jar 包

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <!--打包jar-->
                <directory>src/lib</directory>
                <targetPath>/BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>

    </build>

参考链接:【解决】PageHelper 分页不生效

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们