Mybatis-Plus 之BaseMapper 方法详解
一、源码解析:
/** * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能 * 这个 Mapper 支持 id 泛型*/ public interface BaseMapper{ /** * 插入一条记录 * @param entity * 实体对象 * @return int */ Integer insert(T entity); /** * 根据 ID 删除 * @param id * 主键ID * @return int */ Integer deleteById(Serializable id); /** * 根据 columnMap 条件,删除记录 * @param columnMap * 表字段 map 对象 * @return int */ Integer deleteByMap(@Param("cm") Map columnMap); /** * 根据 entity 条件,删除记录 * @param wrapper * 实体对象封装操作类(可以为 null) * @return int */ Integer delete(@Param("ew") Wrapper wrapper); /** * 删除(根据ID 批量删除) * @param idList * 主键ID列表 * @return int */ Integer deleteBatchIds(List<? extends Serializable> idList); /** * 根据 ID 修改 * @param entity * 实体对象 * @return int */ Integer updateById(T entity); /** * 根据 whereEntity 条件,更新记录 * @param entity * 实体对象 * @param wrapper * 实体对象封装操作类(可以为 null) * @return */ Integer update(@Param("et") T entity, @Param("ew") Wrapper wrapper); /** * 根据 ID 查询 * @param id * 主键ID * @return T */ T selectById(Serializable id); /** * 查询(根据ID 批量查询) * @param idList * 主键ID列表 * @return List */ List selectBatchIds(List<? extends Serializable> idList); /** * 查询(根据 columnMap 条件) * @param columnMap * 表字段 map 对象 * @return List */ List selectByMap(@Param("cm") Map columnMap); /** * 根据 entity 条件,查询一条记录 * @param entity * 实体对象 * @return T */ T selectOne(@Param("ew") T entity); /** * 根据 Wrapper 条件,查询总记录数 * @param wrapper * 实体对象 * @return int */ Integer selectCount(@Param("ew") Wrapper wrapper); /** * 根据 entity 条件,查询全部记录 * @param wrapper * 实体对象封装操作类(可以为 null) * @return List */ List selectList(@Param("ew") Wrapper wrapper); /** * 根据 Wrapper 条件,查询全部记录 * @param wrapper * 实体对象封装操作类(可以为 null) * @return List */ List
二、方法应用:
1 public class TestMapper { 2 3 @Resource 4 private UserMapper userMapper; 5 6 /** 7 * 获取所有用户 8 */ 9 @Test 10 public void selectList(){ 11 ListuserList = userMapper.selectList(null); 12 userList.forEach(System.out::println); 13 } 14 15 /** 16 * 根据指定条件 查询符合条件的用户 (selectList传参数的查询形式) 17 */ 18 @Test 19 public void selectAllList(){ 20 QueryWrapper queryWrapper = new QueryWrapper<>(); 21 queryWrapper.ge("age",23); 22 List userList = userMapper.selectList(queryWrapper); 23 userList.forEach(System.out::println); 24 } 25 26 /** 27 * 根据 id 查询指定用户 28 */ 29 @Test 30 public void selectById(){ 31 User user = userMapper.selectById(1); 32 System.out.println(user); 33 } 34 35 /** 36 * 根据 ID 批量查询用户 37 */ 38 @Test 39 public void selectBatchIds(){ 40 List integers = Arrays.asList(1,2,3); 41 List userList = userMapper.selectBatchIds(integers); 42 userList.forEach(System.out::println); 43 } 44 45 /** 46 * 根据 Map搜索条件查询指定条件下的用户 47 */ 48 @Test 49 public void selectByMap(){ 50 Map map = new HashMap<>(); 51 map.put("name","Tom"); 52 List userList = userMapper.selectByMap(map); 53 userList.forEach(System.out::println); 54 } 55 56 /** 57 * wrapper 查询一条数据 58 */ 59 @Test 60 public void selectOne(){ 61 QueryWrapper queryWrapper = new QueryWrapper<>(); 62 queryWrapper.eq("name","Tom"); 63 User user = userMapper.selectOne(queryWrapper); 64 System.out.println(user); 65 } 66 67 /** 68 * 根据指定条件查询符合条件的记录数 69 */ 70 @Test 71 public void selectCount(){ 72 QueryWrapper queryWrapper = new QueryWrapper<>(); 73 queryWrapper.gt("age",20); 74 Integer count = userMapper.selectCount(queryWrapper); 75 System.out.println(count); 76 } 77 78 79 80 /** 81 * 根据指定条件查询用户 82 */ 83 @Test 84 public void selectMaps(){ 85 QueryWrapper queryWrapper = new QueryWrapper<>(); 86 queryWrapper.ge("age",23); 87 List
publicclassTestMapper{
@Autowiredprivate UserMapper userMapper;
/**
* 获取所有用户
*/
@TestpublicvoidselectList(){
List userList=userMapper.selectList(null);
userList.forEach(System.out::println);}
/**
* 根据指定条件 查询符合条件的用户 (selectList传参数的查询形式)
*/
@TestpublicvoidselectAllList(){
QueryWrapper queryWrapper=newQueryWrapper<>();
queryWrapper.ge("age",23);
List userList=userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);}
/**
*根据 id 查询指定用户
*/
@TestpublicvoidselectById(){
User user=userMapper.selectById(1);
System.out.println(user);}
/**
* 根据 ID 批量查询用户
*/
@TestpublicvoidselectBatchIds(){
List integers=Arrays.asList(1,2,3);
List userList=userMapper.selectBatchIds(integers);
userList.forEach(System.out::println);}
/**
* 根据 Map搜索条件查询指定条件下的用户
*/
@TestpublicvoidselectByMap(){
Map map=newHashMap<>();
map.put("name","Tom");
List userList=userMapper.selectByMap(map);
userList.forEach(System.out::println);}
/**
* wrapper 查询一条数据
*/
@TestpublicvoidselectOne(){
QueryWrapper queryWrapper=newQueryWrapper<>();
queryWrapper.eq("name","Tom");
User user=userMapper.selectOne(queryWrapper);
System.out.println(user);}
/**
* 根据指定条件查询符合条件的记录数
*/
@TestpublicvoidselectCount(){
QueryWrapper queryWrapper=newQueryWrapper<>();
queryWrapper.gt("age",20);
Integer count=userMapper.selectCount(queryWrapper);
System.out.println(count);}
/**
* 根据指定条件查询用户
*/
@TestpublicvoidselectMaps(){
QueryWrapper queryWrapper=newQueryWrapper<>();
queryWrapper.ge("age",23);
List
(以上是对网上查阅的资料进行了总结,不保证一定正确,见谅……)