查询操作
- 通过id查询单个用户
@Test//通过id查询单个用户
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
- 通过id查询多个用户
@Test//通过id查询多个用户
public void testSelectBatchIds(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
users.forEach(System.out::println);
//System.out.println(users);
}
- 条件查询 通过map封装
@Test//通过条件查询之一 map
public void testMap(){
HashMap<String, Object> map = new HashMap<>();
//自定义要查询的
map.put("name","root");
map.put("age",20);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
分页查询
分页在网站的使用十分之多!
1、原始的limit分页
2、pageHelper第三方插件
3、MybatisPlus其实也内置了分页插件!
如何使用:
1、配置拦截器组件
- config - MyBatisPlusConfig
//分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
2、直接使用page对象即可
@Test//测试分页查询
public void testPage(){
//参数一current:当前页 参数二size:页面大小
//使用了分页插件之后,所有的分页操作都变得简单了
Page<User> page = new Page<>(2,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
System.out.println("总页数==>"+page.getTotal());
}