mybatis-plus分頁查詢?nèi)N方法小結(jié)
一、前期準備表
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, '藍米粥', '女', 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 {
/**
* 新增分頁攔截器,并設(shè)置數(shù)據(jù)庫類型為mysql
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}二、使用selectPage
1、Service
//分頁參數(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種分頁查詢的寫法
1、xml
<select id="getPageStudentTwo" resultType="com.example.demo.entity.base.SchoolStudent">
select * from school_student
</select>2、Mapper
說明:
- 1、mybatis-plus中分頁接口需要包含一個IPage類型的參數(shù)。
- 2、多個實體參數(shù),需要添加@Param參數(shù)注解,方便在xml中配置sql時獲取參數(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插件分頁查詢
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頁,10條內(nèi)容,默認查詢總數(shù)count
PageHelper.startPage(current, size);
List<SchoolStudent> list = this.list();
//用PageInfo對結(jié)果進行包裝
PageInfo page = new PageInfo(list);
return page;
}3、結(jié)果
這是控制臺打印的查詢語句,大家發(fā)現(xiàn)最后的LIMIT 函數(shù)沒,正常來說mybatis-plus里是沒有寫的,是pagehelper加上去。我頓時覺得,對于一個初級程序員的我來說,還有好多要學(xué)的。
PageHelper.startPage(pageNum, pageSize)這個地方設(shè)置的兩個值,pagehelper會在你執(zhí)行查詢語句的時候幫你加上去,也就是LIMIT 的兩個參數(shù),第一個參數(shù)是LIMIT 的起始下標,pagehelper會根據(jù)pageNum和pageSize自動給你算出;第二個參數(shù)是LIMIT的 數(shù)據(jù)量,也就是pageSize。而且我發(fā)現(xiàn),pagehelper會執(zhí)行兩遍你寫的查詢語句,第一遍會進行count(0),查出總條數(shù),第二遍就會利用你設(shè)置的參數(shù)幫你分頁查詢出pageSize條數(shù)據(jù)。
我之前想先進行樹排序后再進行分頁的想法,在使用pagehelper時是行不通的,因為會影響pagehelper的自動分頁。因此我得出在進行pagehelper分頁的時候不可以給查詢出的數(shù)據(jù)進行其他排序操作(查詢語句中寫order by是可以的),這可能就是pagehelper的局限之處,不過我相信應(yīng)該有解決辦法,等我找到了再分享出來。

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

