Mybatis-Plus通用Mapper CRUD之select

mybatis-plus框架提供了很多查询方法:

/**
 * 根据 ID 查询
 *
 * @param id 主键ID
 */
T selectById(Serializable id);
 
/**
 * 查询(根据ID 批量查询)
 *
 * @param idList 主键ID列表(不能为 null 以及 empty)
 */
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
 
/**
 * 查询(根据 columnMap 条件)
 *
 * @param columnMap 表字段 map 对象
 */
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
 
/**
 * 根据 entity 条件,查询一条记录
 *
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 Wrapper 条件,查询总记录数
 *
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 entity 条件,查询全部记录
 *
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 Wrapper 条件,查询全部记录
 *
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 Wrapper 条件,查询全部记录
 * <p>注意:只返回第一个字段的值</p>
 *
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 entity 条件,查询全部记录(并翻页)
 *
 * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
<E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 
/**
 * 根据 Wrapper 条件,查询全部记录(并翻页)
 *
 * @param page 分页查询条件
 * @param queryWrapper 实体对象封装操作类
 */
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

我们先讲这个selectById,selectBatchIds,selectByMap方法,后面讲条件构造器以及分页再讲;

@Test
public void selectById(){
  Department department = departmentMapper.selectById(1);
  System.out.println(department);
}
 
@Test
public void selectBatchIds(){
  List<Integer> idList=new ArrayList<>();
  idList.add(1);
  idList.add(2);
  idList.add(3);
  List<Department> departmentList = departmentMapper.selectBatchIds(idList);
  System.out.println(departmentList);
}
 
@Test
public void selectByMap(){
  Map<String,Object> columnMap=new HashMap<>();
  columnMap.put("name","测试");
  columnMap.put("remark","xx");
  List<Department> departmentList = departmentMapper.selectByMap(columnMap);
  System.out.println(departmentList);
}

 

 

------------------------------------------------------------------------------------------------------------------------------

作者: java1234_小锋

出处:https://www.cnblogs.com/java688/p/13542770.html

版权:本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。

------------------------------------------------------------------------------------------------------------------------------

posted @ 2020-08-26 14:32  java锋哥  阅读(1867)  评论(0编辑  收藏  举报