MyBatis-Plus的Service CRUD接口

前缀命名方式区分Mapper层。

get 查询单行、remove 删除、list 查询集合、page 分页

友链:

📝官方文档:Service CRUD 接口

📝条件构造器Wrapper

📝代码自动生成器
生成entity、mapper、service、controller代码的同时,为实体类中的主键生成策略、自动填充、逻辑删除、Swagger注释添加注解。

📝配置插入时的主键生成策略

📝数据库操作创建时间、更新时间自动填充

📝配置逻辑删除

查询一条记录

根据 ID 查询:T getById(Serializable id);

条件查询wrapper。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper queryWrapper);

条件查询wrapper
T getOne(Wrapper queryWrapper, boolean throwEx);

条件查询wrapper
Map<String, Object> getMap(Wrapper queryWrapper);

条件查询wrapper
V getObj(Wrapper queryWrapper, Function<? super Object, V> mapper);

查询List

查询所有:List list();

批量查询:Collection listByIds(Collection<? extends Serializable> idList);

查询所有:List<Map<String, Object>> listMaps();

查询所有:List<Object> listObjs();

查询所有: List listObjs(Function<? super Object, V> mapper);

条件查询map
Collection listByMap(Map<String, Object> columnMap);

条件查询wrapper
List list(Wrapper queryWrapper);

查询条件wrapper
List<Map<String, Object>> listMaps(Wrapper queryWrapper);

条件查询wrapper
List<Object> listObjs(Wrapper<T> queryWrapper);

条件查询wrapper
List listObjs(Wrapper queryWrapper, Function<? super Object, V> mapper);

分页

无条件分页查询:IPage page(IPage page);

无条件分页查询:IPage<Map<String, Object>> pageMaps(IPage page);

条件分页查询:IPage page(IPage page, Wrapper queryWrapper);

条件分页查询:IPage<Map<String, Object>> pageMaps(IPage page, Wrapper queryWrapper);

统计

查询总记录数:int count();

条件查询总记录数wrapper
int count(Wrapper queryWrapper);

链式-查询

QueryChainWrapper query();

query().eq("column", value).one();

lambda链式查询(不支持 Kotlin)
LambdaQueryChainWrapper lambdaQuery();

lambdaQuery().eq(Entity::getId, value).list();

插入

选择字段,策略插入一条记录:boolean save(T entity);

批量插入:boolean saveBatch(Collection entityList);

批量插入:boolean saveBatch(Collection entityList, int batchSize);

public void insertUser(){
	User user = new User();
	user.setName("xiaoming");
	user.setPassword(123456);

	int result = userService.save(user); 
	System.out.println(result);  // 受影响的行数
	System.out.println(user); // 结果会自动回填
}

插入 / 更新

TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);

根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper updateWrapper);

批量修改插入
boolean saveOrUpdateBatch(Collection entityList);

批量修改插入
boolean saveOrUpdateBatch(Collection entityList, int batchSize);

更新

根据Id更新:boolean updateById(T entity);

批量更新:boolean updateBatchById(Collection entityList);

批量更新:boolean updateBatchById(Collection entityList, int batchSize);

条件更新wrapper(需要设置sqlset)
boolean update(Wrapper updateWrapper);

条件更新wrapper
boolean update(T entity, Wrapper updateWrapper);

删除

根据Id删除:boolean removeById(Serializable id);

批量删除:boolean removeByIds(Collection<? extends Serializable> idList);

条件删除wrapper:boolean remove(Wrapper queryWrapper);

条件删除map:boolean removeByMap(Map<String, Object> columnMap);

posted @ 2021-01-21 12:02  wattmelon  阅读(188)  评论(0编辑  收藏  举报