查询操作和分页

查询操作,比较简单,但方式比较多,更具需要自行选择

@Test//简单查询
void testSelect(){
user user = userMapper.selectById(1L);//serializable 可序列化的
userMapper.selectBatchIds(Arrays.asList(1L,8L));
System.out.println(user);
}
@Test
void testSelectByBatchIds(){
List<user> users = userMapper.selectBatchIds(Arrays.asList(1L, 8L));
users.forEach((x)->{
System.out.println(x);
});
}
@Test //根据条件查询之一 map
void testSelectMap(){
HashMap<String, Object> map = new HashMap<>();
//条件在map中填写
map.put("name","小落");
List<user> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}

分页查询

mybatis-plus中内置了分页组件

怎么使用分页组件呢?

  1. 增加配置组件
// 旧版
@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;
}
  1. 测试分页组件
@Test
void pageTest(){
//page:1.当前页 2.页面大小
IPage<user> Page = new Page<>(2,5);
System.out.println(userMapper.selectPage(Page, null));
System.out.println("Page.getCurrent()"+Page.getCurrent());//返回当前页面
System.out.println("Page.getPages()"+Page.getPages()); //返回总页数
System.out.println("Page.getRecords()"+Page.getRecords()); //返回分页后查询的记录
System.out.println("Page.getTotal()"+Page.getTotal());//返回总用户数目
System.out.println("Page.getSize()"+Page.getSize());//返回页面大小
}

就步骤来讲,真的简便了许多

posted @   小罗要有出息  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示