⭐MybatisPlus学习⭐(二)CRUD全套详解
        【摘要】 ⭐MybatisPlus学习⭐(二)CRUD全套详解
    
    
    
    MybatiPlus学习笔记(二)😁
Code皮皮虾 一个沙雕而又有趣的憨憨少年,和大多数小伙伴们一样喜欢听歌、游戏,当然除此之外还有写作的兴趣,emm…,日子还很长,让我们结伴一起走下去吧🌈
欢迎各位小伙伴们关注我的公众号:JavaCodes,名称虽带Java但涉及范围可不止Java领域噢😁,后台回复面试资料还能领取大厂面试真题、视频等等,期待您的关注❤
1、 Mapper CRUD 接口
说明:
- 通用 CRUD 封装BaseMapper 接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
 - 泛型 T 为任意实体对象
 - 参数 Serializable 为任意类型主键 Mybatis-Plus 不推荐使用复合主键约定每一张表都有自己的唯一 id 主键
 - 对象 Wrapper 为 条件构造器
 
1.1、Insert
// 插入一条记录
int insert(T entity);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| T | entity | 实体对象 | 
Employee employee = new Employee();
employee.setLastName("皮皮虾");
employee.setAge(21);
employee.setEmail("qq@qq.com");
employee.setGender(1);
employeeMapper.insert(employee);
 

1.2、Delete
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Wrapper<T> | wrapper | 实体对象封装操作类(可以为 null) | 
| Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) | 
| Serializable | id | 主键ID | 
| Map<String, Object> | columnMap | 表字段 map 对象 | 






1.3、Update
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| T | entity | 实体对象 (set 条件值,可为 null) | 
| Wrapper<T> | updateWrapper | 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) | 


1.4、Select
// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Serializable | id | 主键ID | 
| Wrapper<T> | queryWrapper | 实体对象封装操作类(可以为 null) | 
| Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) | 
| Map<String, Object> | columnMap | 表字段 map 对象 | 
| IPage<T> | page | 分页查询条件(可以为 RowBounds.DEFAULT) | 


 





 
 

2、Service CRUD 接口
==说明==:
- 用 Service CRUD 封装IService 接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
 - 泛型 T 为任意实体对象
 - 建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类
 - 对象 Wrapper 为 条件构造器
 
==创建Service和实现类==
 
EmployeeService
public interface EmployeeService extends IService<Employee> {
}
 EmployeeImpl
@Service
public class EmployeeImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
 
   2.1、Save
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| T | entity | 实体对象 | 
| Collection<T> | entityList | 实体对象集合 | 
| int | batchSize | 插入批次数量 | 


 



 
2.2、SaveOrUpdate
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| T | entity | 实体对象 | 
| Wrapper<T> | updateWrapper | 实体对象封装操作类 UpdateWrapper | 
| Collection<T> | entityList | 实体对象集合 | 
| int | batchSize | 插入批次数量 | 


 

 
2.3、Remove
// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Wrapper<T> | queryWrapper | 实体包装类 QueryWrapper | 
| Serializable | id | 主键ID | 
| Map<String, Object> | columnMap | 表字段 map 对象 | 
| Collection<? extends Serializable> | idList | 主键ID列表 | 

 

 
 



 
2.4、Update
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Wrapper<T> | queryWrapper | 实体包装类 QueryWrapper | 
| T | entity | 实体对象 | 
| Collection<T> | entityList | 实体对象集合 | 
| int | batchSize | 更新批次数量 | 


 
 
2.5、Get
// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Serializable | id | 实体包装类 主键ID | 
| T | entity | 实体对象 | 
| Collection<T> | entityList | 实体对象集合 | 
| Function<? super Object, V> | mapper | 转换函数 | 
| Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper | 

 
 
 
2.6、List
// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Collection<? extends Serializable> | idList | 主键ID列表 | 
| Map<?String, Object> | columnMap | 表字段 map 对象 | 
| Collection<T> | entityList | 实体对象集合 | 
| Function<? super Object, V> | mapper | 转换函数 | 
| Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper | 

 
 
 
 
 
2.7、Count
// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);
 ==参数说明==
| 类型 | 参数名 | 描述 | 
|---|---|---|
| Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper | 


 
 
 
最后
我是 Code皮皮虾,一个热爱分享知识的 皮皮虾爱好者,未来的日子里会不断更新出对大家有益的博文,期待大家的关注!!!
创作不易,如果这篇博文对各位有帮助,希望各位小伙伴可以==一键三连哦!==,感谢支持,我们下次再见~~~
 
分享大纲

            【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)