詳解MyBatis Plus中分頁(yè)插件的使用
MyBatis Plus分頁(yè)插件使用
MyBatis Plus中使用分頁(yè)插件也很簡(jiǎn)單:
首先編寫配置類:
@Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { // 構(gòu)造攔截器 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 添加分頁(yè)插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
大功告成!現(xiàn)在來(lái)測(cè)試一下吧:
@Test void contextLoads() { Page<User> page = new Page<>(1, 2); userMapper.selectPage(page, null); System.out.println(page); }
==> Preparing: SELECT COUNT(*) AS total FROM user WHERE is_delete = 0
==> Parameters:
<== Columns: total
<== Row: 3
<== Total: 1
==> Preparing: SELECT id,name,age,email,is_delete FROM user WHERE is_delete=0 LIMIT ?
==> Parameters: 2(Long)
<== Columns: id, name, age, email, is_delete
<== Row: 2, hello, 33, 111@qq.com, 0
<== Row: 3, hello, 18, 34567@qq.com, 0
<== Total: 2
也可以很容易的獲取分頁(yè)相關(guān)的數(shù)據(jù):
程序?qū)嵗?/p>
@Test void contextLoads() { Page<User> page = new Page<>(1, 2); userMapper.selectPage(page, null); // 獲取當(dāng)前頁(yè)的記錄 List<User> records = page.getRecords(); records.forEach(System.out::println); // 獲取當(dāng)前頁(yè)的頁(yè)碼 long current = page.getCurrent(); System.out.println(current); // 獲取分頁(yè)的總頁(yè)碼數(shù) long size = page.getSize(); System.out.println(size); // 判斷是否有下一頁(yè) boolean b = page.hasNext(); System.out.println(b); // 判斷是否有上一頁(yè) boolean b1 = page.hasPrevious(); System.out.println(b1); }
自定義分頁(yè)功能
首先,定義一個(gè)mapper接口,返回一個(gè)Page對(duì)象:
Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
實(shí)現(xiàn)mapper接口:
<select id="selectPageVo" resultType="User"> select id,name,age,email from user where age = #{age} </select>
我們使用了類型別名,不要忘記在配置類中開啟掃描類型別名所在的包:
mybatis-plus: ... # 配置類型別名對(duì)應(yīng)的包 type-aliases-package: com.klza.pojo
現(xiàn)在來(lái)測(cè)試一下吧:
@Test void contextLoads() { Page<User> page = new Page<>(1, 2); Page<User> userPage = userMapper.selectPageVo(page, 18); System.out.println(userPage); }
==> Preparing: SELECT COUNT(*) AS total FROM user WHERE age = ?
==> Parameters: 18(Integer)
<== Columns: total
<== Row: 2
<== Total: 1
==> Preparing: select id,name,age,email from user where age = ? LIMIT ?
==> Parameters: 18(Integer), 2(Long)
<== Columns: id, name, age, email
<== Row: 3, hello, 18, 34567@qq.com
<== Row: 4, hello, 18, 34567@qq.com
<== Total: 2
到此這篇關(guān)于詳解MyBatis Plus中分頁(yè)插件的使用的文章就介紹到這了,更多相關(guān)MyBatis Plus分頁(yè)插件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09MyBatis-Plus?分頁(yè)查詢的實(shí)現(xiàn)示例
本文主要介紹了MyBatis-Plus?分頁(yè)查詢的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03springboot整合security和vue的實(shí)踐
本文主要介紹了springboot整合security和vue的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09SpringBoot結(jié)合dev-tool實(shí)現(xiàn)IDEA項(xiàng)目熱部署的流程步驟
這篇文章主要給大家介紹了SpringBoot結(jié)合dev-tool實(shí)現(xiàn)IDEA項(xiàng)目熱部署的流程步驟,文章通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)有一定的幫助,需要的朋友可以參考下2023-10-10java大話之創(chuàng)建型設(shè)計(jì)模式教程示例
這篇文章主要為大家介紹了java大話之創(chuàng)建型設(shè)計(jì)模式教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Java 給PDF簽名時(shí)添加可信時(shí)間戳的方法
這篇文章主要介紹了Java 給PDF簽名時(shí)添加可信時(shí)間戳,關(guān)于jar導(dǎo)入的問(wèn)題,本文給大家?guī)?lái)兩種方法,一種是手動(dòng)導(dǎo)入另一種是maven配置導(dǎo)入,需要的朋友可以參考下2021-07-07