mybatis-plus分頁查詢?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 {
/**
* 新增分頁攔截器,并設(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中分頁接口需要包含一個(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插件分頁查詢
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)容,默認(rèn)查詢總數(shù)count
PageHelper.startPage(current, size);
List<SchoolStudent> list = this.list();
//用PageInfo對結(jié)果進(jìn)行包裝
PageInfo page = new PageInfo(list);
return page;
}3、結(jié)果
這是控制臺(tái)打印的查詢語句,大家發(fā)現(xiàn)最后的LIMIT 函數(shù)沒,正常來說mybatis-plus里是沒有寫的,是pagehelper加上去。我頓時(shí)覺得,對于一個(gè)初級程序員的我來說,還有好多要學(xué)的。
PageHelper.startPage(pageNum, pageSize)這個(gè)地方設(shè)置的兩個(gè)值,pagehelper會(huì)在你執(zhí)行查詢語句的時(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í)行兩遍你寫的查詢語句,第一遍會(huì)進(jìn)行count(0),查出總條數(shù),第二遍就會(huì)利用你設(shè)置的參數(shù)幫你分頁查詢出pageSize條數(shù)據(jù)。
我之前想先進(jìn)行樹排序后再進(jìn)行分頁的想法,在使用pagehelper時(shí)是行不通的,因?yàn)闀?huì)影響pagehelper的自動(dòng)分頁。因此我得出在進(jìn)行pagehelper分頁的時(shí)候不可以給查詢出的數(shù)據(jù)進(jìn)行其他排序操作(查詢語句中寫order by是可以的),這可能就是pagehelper的局限之處,不過我相信應(yīng)該有解決辦法,等我找到了再分享出來。

到此這篇關(guān)于mybatis-plus分頁查詢?nèi)N方法小結(jié)的文章就介紹到這了,更多相關(guān)mybatis-plus分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus分頁查詢的實(shí)現(xiàn)實(shí)例
- SpringBoot使用mybatis-plus分頁查詢無效的問題解決
- SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁查詢功能
- mybatis-plus多表分頁查詢最佳實(shí)現(xiàn)方法(非常簡單)
- Mybatis-plus分頁查詢不生效問題排查全過程
- 如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能
- 一文搞懂Mybatis-plus的分頁查詢操作
- MyBatis-Plus?分頁查詢的實(shí)現(xiàn)示例
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能
- mybatis-plus分頁查詢的實(shí)現(xiàn)示例
- mybatis-plus 實(shí)現(xiàn)分頁查詢的示例代碼
相關(guān)文章
java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java程序員的10道常見的XML面試問答題(XML術(shù)語詳解)
包括web開發(fā)人員的Java面試在內(nèi)的各種面試中,XML面試題在各種編程工作的面試中很常見。XML是一種成熟的技術(shù),經(jīng)常作為從一個(gè)平臺(tái)到其他平臺(tái)傳輸數(shù)據(jù)的標(biāo)準(zhǔn)2014-04-04
Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)
本篇文章給大家詳細(xì)講述了Java字節(jié)與字符流永久存儲(chǔ)json數(shù)據(jù)的方法,以及代碼分享,有興趣的參考學(xué)習(xí)下。2018-02-02
Java設(shè)計(jì)模式之監(jiān)聽器模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之監(jiān)聽器模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了java設(shè)計(jì)模式中監(jiān)聽器模式的概念、原理及相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-02-02
java實(shí)現(xiàn)基于Tcp的socket聊天程序
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)基于Tcp的socket聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Spring 開發(fā)之組件賦值的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring 開發(fā)之組件賦值的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

