文章目录
查询
根据 ID 查询
T selectById(Serializable id);
根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
查询单个数据,这个方法必须是需要添加Wrapper条件,并且保证返回的数据是有一条
查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
详解
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("last_name","詹姆斯");
userMapper.selectList(queryWrapper).forEach(System.out::println);
QueryWrapper 中还有很多的方法可以直接使用
查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
根据 Wrapper 条件,查询全部记录
这里的Maps跟上头的Map是两个概念,是将结果集封装到Map中进行输出的。
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
结果集是Object集合,封装的是所有数据的id主键
根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
详解
1.添加配置
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
2.测试类
Page<User> page = new Page<>(1, 2);
Page<User> result = userMapper.selectPage(page, null);
System.out.println(result.getSize());
System.out.println(result.getTotal());
result.getRecords().forEach(System.out::println);
根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
详解
1.添加配置
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
2.测试类
Page<Map<String,Object>> page = new Page<>(1, 2);
Page<Map<String, Object>> result = userMapper.selectMapsPage(page, null);
System.out.println(result.getSize());
System.out.println(result.getTotal());
result.getRecords().forEach(System.out::println);
根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);