解決分頁插件pagehelper在SpringBoot不起作用的問題
分頁插件pagehelper在SpringBoot不起作用
在springBoot中使用分頁插件pagehelper之后,在前端將數(shù)據(jù)拿出來的時(shí)候發(fā)現(xiàn)分頁插件并沒有起到作用?
檢查之后發(fā)現(xiàn)在springBoot中如果需要使用分頁插件需要添加pagehelper-spring-boot-starter的jar包才可生效。
springBoot版本:2.7.0
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>PageHelper在SpringBoot中的應(yīng)用
1.POM文件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2. pageHelper攔截器的了解
PageHelper就是實(shí)現(xiàn)了一個(gè)MyBatis的自定義攔截器
//@Intercepts:標(biāo)識(shí)該類是一個(gè)自定義攔截器
//@Signature:指明我們需要攔截的接口和方法
/** type :mybatis 攔截器默認(rèn)可攔截的類型只有四種,即四種接口類型 Executor、StatementHandler、ParameterHandler 和 ResultSetHandler
* 對(duì)于我們的自定義攔截器必須使用 mybatis 提供的注解來指明我們要攔截的是四類中的哪一個(gè)類接口
* method : 對(duì)應(yīng)接口中的哪類方法
args :指明參數(shù)類型,從而確定是哪一個(gè)方法
*/
@Intercepts({@Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class PageInterceptor implements Interceptor {
protected Cache<String, MappedStatement> msCountMap = null;
private Dialect dialect;
private String default_dialect_class = "com.github.pagehelper.PageHelper";
private Field additionalParametersField;
private String countSuffix = "_COUNT";
3.配置文件中添加攔截器
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- com.github.pagehelper為PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置參數(shù),參數(shù)介紹參考官方文檔 -->
<!-- <property name="param1" value="value1"/>-->
</plugin>
</plugins>
</configuration>
4.pageHelper使用
/*doSelectPageInfo方法解釋 : 是PageHelper.startPage()函數(shù)返回的默認(rèn)Page實(shí)例內(nèi)置的函數(shù),該函數(shù)可以用以Lambda的形式通過額外的Function來進(jìn)行查詢而不需要再進(jìn)行多余的PageInfo與List轉(zhuǎn)換,而doSelectPageInfo的參數(shù)則是PageHelper內(nèi)置的Function(ISelect)接口用以達(dá)到轉(zhuǎn)換PageInfo的目的
*/
PageInfo</*返回類型*/> objectPageInfo = PageHelper.startPage( /*參數(shù)頁碼, 參數(shù)頁面長(zhǎng)度*/)
.doSelectPageInfo(() -> /*mysql數(shù)據(jù)查詢:mapper.XXXX(參數(shù))*/);
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java多線程通訊之wait,notify的區(qū)別詳解
這篇文章主要介紹了Java多線程通訊之wait,notify的區(qū)別詳解,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Spring內(nèi)存緩存Caffeine的基本使用教程分享
Caffeine作為當(dāng)下本地緩存的王者被大量的應(yīng)用再實(shí)際的項(xiàng)目中,可以有效的提高服務(wù)吞吐率、qps,降低rt,本文就來簡(jiǎn)單介紹下Caffeine的使用姿勢(shì)吧2023-03-03
Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志
這篇文章主要介紹了Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請(qǐng)求日志的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決
這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
java實(shí)現(xiàn)大數(shù)加法(BigDecimal)的實(shí)例代碼
之前寫過用vector、string實(shí)現(xiàn)大數(shù)加法,現(xiàn)在用java的BigDecimal類,代碼簡(jiǎn)單很多。但是在online-judge上,java的代碼運(yùn)行時(shí)間和內(nèi)存大得多2013-10-10
SpringBoot @ConfigurationProperties注解的簡(jiǎn)單使用
即便現(xiàn)在簡(jiǎn)化了配置,但是一個(gè)獨(dú)立的配置文件總是易于理解而且使人安心的。Spring在構(gòu)建完項(xiàng)目后,會(huì)默認(rèn)在resources文件夾下創(chuàng)建一個(gè)application.properties文件,application.yml也是一樣的效果。@ConfigurationProperties可以獲取配置文件中的數(shù)據(jù),將其注入類。2021-05-05

