詳解MyBatis Plus中分頁插件的使用
MyBatis Plus分頁插件使用
MyBatis Plus中使用分頁插件也很簡單:
首先編寫配置類:
@Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { // 構(gòu)造攔截器 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 添加分頁插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
大功告成!現(xiàn)在來測試一下吧:
@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
也可以很容易的獲取分頁相關(guān)的數(shù)據(jù):
程序?qū)嵗?/p>
@Test void contextLoads() { Page<User> page = new Page<>(1, 2); userMapper.selectPage(page, null); // 獲取當(dāng)前頁的記錄 List<User> records = page.getRecords(); records.forEach(System.out::println); // 獲取當(dāng)前頁的頁碼 long current = page.getCurrent(); System.out.println(current); // 獲取分頁的總頁碼數(shù) long size = page.getSize(); System.out.println(size); // 判斷是否有下一頁 boolean b = page.hasNext(); System.out.println(b); // 判斷是否有上一頁 boolean b1 = page.hasPrevious(); System.out.println(b1); }
自定義分頁功能
首先,定義一個mapper接口,返回一個Page對象:
Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
實現(xiàn)mapper接口:
<select id="selectPageVo" resultType="User"> select id,name,age,email from user where age = #{age} </select>
我們使用了類型別名,不要忘記在配置類中開啟掃描類型別名所在的包:
mybatis-plus: ... # 配置類型別名對應(yīng)的包 type-aliases-package: com.klza.pojo
現(xiàn)在來測試一下吧:
@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中分頁插件的使用的文章就介紹到這了,更多相關(guān)MyBatis Plus分頁插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實現(xiàn)代碼
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09SpringBoot結(jié)合dev-tool實現(xiàn)IDEA項目熱部署的流程步驟
這篇文章主要給大家介紹了SpringBoot結(jié)合dev-tool實現(xiàn)IDEA項目熱部署的流程步驟,文章通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)有一定的幫助,需要的朋友可以參考下2023-10-10java大話之創(chuàng)建型設(shè)計模式教程示例
這篇文章主要為大家介紹了java大話之創(chuàng)建型設(shè)計模式教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02