- 導入分頁插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
- 使用分頁查詢
@Test
public void testPageQuery(){
//設置分頁參數
Page<User> page = new Page<>(1, 5);
userMapper.selectPage(page, null);
//獲取分頁數據
List<User> list = page.getRecords();
list.forEach(System.out::println);
System.out.println("當前頁:"+page.getCurrent());
System.out.println("每頁顯示的條數:"+page.getSize());
System.out.println("總記錄數:"+page.getTotal());
System.out.println("總頁數:"+page.getPages());
System.out.println("是否有上一頁:"+page.hasPrevious());
System.out.println("是否有下一頁:"+page.hasNext());
}
- 自定義的mapper方法使用分頁
方法
//傳入參數攜帶Ipage接口
//返回結果為IPage
IPage<User> selectPageVo(IPage<?> page, Integer id);
<select id="selectPageVo" resultType="xxx.xxx.xxx.User">
SELECT * FROM user WHERE id > #{id}
</select>
@Test
public void testQuick(){
IPage page = new Page(1,2);
userMapper.selectPageVo(page,2);
long current = page.getCurrent();
System.out.println("current = " + current);
long pages = page.getPages();
System.out.println("pages = " + pages);
long total = page.getTotal();
System.out.println("total = " + total);
List records = page.getRecords();
System.out.println("records = " + records);
}