MybatisPlus实现分页查询
使用MybatisPlus进行查询操作的一些用法:
1、list集合
//多个id批量查询
@Test
void testSelect1(){
//这里的selectBatchIds方法 需要传入一个id的集合参数
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
users.forEach(System.out::println);
}
2、map的方法
@Test
void testSelectByMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "Jone");
map.put("age", 18);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
3、分页查询
(1)创建配置类
/** * 旧版分页插件 * @return */
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
//paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
(2)测试分页插件,编写分页代码
直接new page对象,传入两个参数,当前页和每页显示记录数
调用map实现分页
//分页查询测试
@Test
public void testPage(){
//1、创建page对象
//传入两个参数: 当前页和每页显示记录数
Page<User> userPage = new Page<>(1,2);
//调用分页查询的方法
//调用分页查询过程中,底层封装
//把分页所有数据封装到page对象里面
//两个参数:第一个参数是page对象,第二个wrapper查询的条件
userMapper.selectPage(userPage, null);
// 通过page对象获取分页数据
System.out.println("当前页:"+userPage.getCurrent()); //当前页
System.out.println("每页数据内容:"+userPage.getRecords()); //每页数据内容
System.out.println("每页显示记录:"+userPage.getSize()); //每页显示记录
System.out.println("总记录数:"+userPage.getTotal()); //总记录数
System.out.println("总页数:"+userPage.getPages()); //总页数
System.out.println("是否有下一页:"+userPage.hasNext()); //是否有下一页
System.out.println("是否有上一页:"+userPage.hasPrevious()); //是否有上一页
}
还没有评论,来说两句吧...