Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 初步使用
  • 主键策略
  • 自动填充
  • 乐观锁
  • 基本查询
    • 多个id批量查询
    • 条件查询
    • 分页查询
      • 添加分页插件
  • 删除操作
  • 使用 Wrapper 查询
  • Framework-MybatisPlus
Shetengteng
2021-11-24

基本查询

# 多个id批量查询

// 批量查询
@Test
public void testBatchSelectById(){
    // SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id IN ( ? , ? , ? ) 
    List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
    System.out.println(users);
}
1
2
3
4
5
6
7
  • 实际的查询语句,IN操作

# 条件查询

@Test
public void testSelectByParamMap() {
    // SELECT id,name,age,email,create_time,update_time,version FROM user WHERE name = ? AND age = ?
    Map<String, Object> columnMap = new HashMap<>();
    columnMap.put("name", "stt");
    columnMap.put("age", 22);
    List<User> users = userMapper.selectByMap(columnMap);
    System.out.println(users);
}
1
2
3
4
5
6
7
8
9

# 分页查询

  • MyBatis Plus自带分页插件

# 添加分页插件

  • 在 MybatisPlusConfig 类中添加拦截器
@Bean
public PaginationInterceptor paginationInterceptor(){
    return new PaginationInterceptor();
}
1
2
3
4
  • 测试方式1:返回指定的Bean
@Test
public void testPageSelect() {
    // 传入当前页,每页显示的条数
    Page<User> page = new Page(1, 3);
    Page<User> re = userMapper.selectPage(page, null);
    System.out.println(re.getPages()); // 总页数
    System.out.println(re.getTotal()); // 总记录数
    System.out.println(re.hasNext()); // 是否有下一页
    System.out.println(re.hasPrevious()); // 是否有上一页
    System.out.println(re.getCurrent()); // 当前页
    System.out.println(re.getRecords()); // 详细记录列表
}
1
2
3
4
5
6
7
8
9
10
11
12
  • 测试方式2:返回Map类型
// 通过返回值是map的page查询
@Test
public void testSelectMapsPage() {
    //Page不需要泛型
    Page<Map<String, Object>> page = new Page<>(1, 5);
    Page<Map<String, Object>> pageParam = userMapper.selectMapsPage(page, null);
    List<Map<String, Object>> records = pageParam.getRecords();
    records.forEach(System.out::println);
    System.out.println(pageParam.getCurrent());
    System.out.println(pageParam.getPages());
    System.out.println(pageParam.getSize());
    System.out.println(pageParam.getTotal());
    System.out.println(pageParam.hasNext());
    System.out.println(pageParam.hasPrevious());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last Updated: 2022/01/16, 11:29:51
乐观锁
删除操作

← 乐观锁 删除操作→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式