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中分頁接口需要包含一個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)容,默認(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é)果
這是控制臺打印的查詢語句,大家發(fā)現(xiàn)最后的LIMIT 函數(shù)沒,正常來說mybatis-plus里是沒有寫的,是pagehelper加上去。我頓時覺得,對于一個初級程序員的我來說,還有好多要學(xué)的。
PageHelper.startPage(pageNum, pageSize)這個地方設(shè)置的兩個值,pagehelper會在你執(zhí)行查詢語句的時候幫你加上去,也就是LIMIT 的兩個參數(shù),第一個參數(shù)是LIMIT 的起始下標(biāo),pagehelper會根據(jù)pageNum和pageSize自動給你算出;第二個參數(shù)是LIMIT的 數(shù)據(jù)量,也就是pageSize。而且我發(fā)現(xiàn),pagehelper會執(zhí)行兩遍你寫的查詢語句,第一遍會進(jìn)行count(0),查出總條數(shù),第二遍就會利用你設(shè)置的參數(shù)幫你分頁查詢出pageSize條數(shù)據(jù)。
我之前想先進(jìn)行樹排序后再進(jìn)行分頁的想法,在使用pagehelper時是行不通的,因為會影響pagehelper的自動分頁。因此我得出在進(jìn)行pagehelper分頁的時候不可以給查詢出的數(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分頁查詢的實現(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)文章
java實現(xiàn)微信公眾平臺發(fā)送模板消息的示例代碼
這篇文章主要介紹了java實現(xiàn)微信公眾平臺發(fā)送模板消息的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Java程序員的10道常見的XML面試問答題(XML術(shù)語詳解)
包括web開發(fā)人員的Java面試在內(nèi)的各種面試中,XML面試題在各種編程工作的面試中很常見。XML是一種成熟的技術(shù),經(jīng)常作為從一個平臺到其他平臺傳輸數(shù)據(jù)的標(biāo)準(zhǔn)2014-04-04Java字節(jié)與字符流永久存儲json數(shù)據(jù)
本篇文章給大家詳細(xì)講述了Java字節(jié)與字符流永久存儲json數(shù)據(jù)的方法,以及代碼分享,有興趣的參考學(xué)習(xí)下。2018-02-02