mybatis-plus分頁(yè)查詢?nèi)N方法小結(jié)
一、前期準(zhǔn)備表
CREATE TABLE `school_student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `sex` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (1, 'av峰峰', '男', 1); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (2, '盧本偉', '男', 12); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (3, '小米粥', '女', 13); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (4, '黃米粥', '女', 15); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (5, '藍(lán)米粥', '女', 11); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (6, '白米粥', '女', 17); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (7, '紅米粥', '女', 15); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (8, '橙米粥', '女', 16); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (9, '青米粥', '女', 13); INSERT INTO `avlicy`.`school_student`(`id`, `name`, `sex`, `age`) VALUES (10, '紫米粥', '女', 12);
1、配置類
@Configuration
//@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig {
/**
* 新增分頁(yè)攔截器,并設(shè)置數(shù)據(jù)庫(kù)類型為mysql
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}二、使用selectPage
1、Service
//分頁(yè)參數(shù)
Page<SchoolStudent> rowPage = new Page(page, pageSize);
//queryWrapper組裝查詢where條件
LambdaQueryWrapper<SchoolStudent> queryWrapper = new LambdaQueryWrapper<>();
rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
return rowPage;2、結(jié)果


三、使用2種分頁(yè)查詢的寫法
1、xml
<select id="getPageStudentTwo" resultType="com.example.demo.entity.base.SchoolStudent">
select * from school_student
</select>2、Mapper
說明:
- 1、mybatis-plus中分頁(yè)接口需要包含一個(gè)IPage類型的參數(shù)。
- 2、多個(gè)實(shí)體參數(shù),需要添加@Param參數(shù)注解,方便在xml中配置sql時(shí)獲取參數(shù)值。 注意這里我雖然加了@Param但是我并沒有使用
Page<SchoolStudent> getPageStudentTwo(Page<SchoolStudent> rowPage,@Param("schoolStudent") SchoolStudent schoolStudent);3、第一種寫法
@Override
public IPage<SchoolStudent> getPageStudentTwo(Integer current, Integer size) {
Page<SchoolStudent> rowPage = new Page(current, size);
SchoolStudent schoolStudent = new SchoolStudent();
rowPage = this.baseMapper.getPageStudentTwo(rowPage, schoolStudent);
return rowPage;
}4、第一種結(jié)果

5、第二種寫法
@Override
public IPage<SchoolStudent> getPageStudentThree(Integer current, Integer size) {
SchoolStudent schoolStudent = new SchoolStudent();
Page pageStudentTwo = this.baseMapper.getPageStudentTwo(new Page(current, size), schoolStudent);
return pageStudentTwo;
}6、第二種結(jié)果

四、使用PageHelper插件分頁(yè)查詢
1、依賴
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.5</version>
</dependency>2、代碼
@Override
public PageInfo<SchoolStudent> getPageStudentFour(Integer current, Integer size) {
//獲取第1頁(yè),10條內(nèi)容,默認(rèn)查詢總數(shù)count
PageHelper.startPage(current, size);
List<SchoolStudent> list = this.list();
//用PageInfo對(duì)結(jié)果進(jìn)行包裝
PageInfo page = new PageInfo(list);
return page;
}3、結(jié)果
這是控制臺(tái)打印的查詢語(yǔ)句,大家發(fā)現(xiàn)最后的LIMIT 函數(shù)沒,正常來說mybatis-plus里是沒有寫的,是pagehelper加上去。我頓時(shí)覺得,對(duì)于一個(gè)初級(jí)程序員的我來說,還有好多要學(xué)的。
PageHelper.startPage(pageNum, pageSize)這個(gè)地方設(shè)置的兩個(gè)值,pagehelper會(huì)在你執(zhí)行查詢語(yǔ)句的時(shí)候幫你加上去,也就是LIMIT 的兩個(gè)參數(shù),第一個(gè)參數(shù)是LIMIT 的起始下標(biāo),pagehelper會(huì)根據(jù)pageNum和pageSize自動(dòng)給你算出;第二個(gè)參數(shù)是LIMIT的 數(shù)據(jù)量,也就是pageSize。而且我發(fā)現(xiàn),pagehelper會(huì)執(zhí)行兩遍你寫的查詢語(yǔ)句,第一遍會(huì)進(jìn)行count(0),查出總條數(shù),第二遍就會(huì)利用你設(shè)置的參數(shù)幫你分頁(yè)查詢出pageSize條數(shù)據(jù)。
我之前想先進(jìn)行樹排序后再進(jìn)行分頁(yè)的想法,在使用pagehelper時(shí)是行不通的,因?yàn)闀?huì)影響pagehelper的自動(dòng)分頁(yè)。因此我得出在進(jìn)行pagehelper分頁(yè)的時(shí)候不可以給查詢出的數(shù)據(jù)進(jìn)行其他排序操作(查詢語(yǔ)句中寫order by是可以的),這可能就是pagehelper的局限之處,不過我相信應(yīng)該有解決辦法,等我找到了再分享出來。

到此這篇關(guān)于mybatis-plus分頁(yè)查詢?nèi)N方法小結(jié)的文章就介紹到這了,更多相關(guān)mybatis-plus分頁(yè)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus分頁(yè)查詢的實(shí)現(xiàn)實(shí)例
- SpringBoot使用mybatis-plus分頁(yè)查詢無效的問題解決
- SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能
- mybatis-plus多表分頁(yè)查詢最佳實(shí)現(xiàn)方法(非常簡(jiǎn)單)
- Mybatis-plus分頁(yè)查詢不生效問題排查全過程
- 如何使用mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能
- 一文搞懂Mybatis-plus的分頁(yè)查詢操作
- MyBatis-Plus?分頁(yè)查詢的實(shí)現(xiàn)示例
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢功能
- mybatis-plus分頁(yè)查詢的實(shí)現(xiàn)示例
- mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢的示例代碼
相關(guān)文章
springboot @Valid注解對(duì)嵌套類型的校驗(yàn)功能
這篇文章主要介紹了springboot~@Valid注解對(duì)嵌套類型的校驗(yàn),主要介紹 @Valid在項(xiàng)目中的使用,需要的朋友可以參考下2018-05-05
SpringMVC+MyBatis 事務(wù)管理(實(shí)例)
本文先分析編程式注解事務(wù)和基于注解的聲明式事務(wù)。對(duì)SpringMVC+MyBatis 事務(wù)管理的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2017-08-08
解決maven?maven.compiler.source和maven.compiler.target的坑
這篇文章主要介紹了解決maven?maven.compiler.source和maven.compiler.target的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Maven項(xiàng)目分析剔除無用jar引用的方法步驟
這篇文章主要介紹了Maven項(xiàng)目分析剔除無用jar引用的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Maven將代碼及依賴打成一個(gè)Jar包的方式詳解(最新推薦)
這篇文章主要介紹了Maven將代碼及依賴打成一個(gè)Jar包的方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
springmvc后臺(tái)基于@ModelAttribute獲取表單提交的數(shù)據(jù)
這篇文章主要介紹了springmvc后臺(tái)基于@ModelAttribute獲取表單提交的數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
RabbitMQ的Direct Exchange模式實(shí)現(xiàn)的消息發(fā)布案例(示例代碼)
本文介紹了RabbitMQ的DirectExchange模式下的消息發(fā)布和消費(fèi)的實(shí)現(xiàn),詳細(xì)說明了如何在DirectExchange模式中進(jìn)行消息的發(fā)送和接收,以及消息處理的基本方法,感興趣的朋友跟隨小編一起看看吧2024-09-09

