首页 > 其他 > 详细

MyBatis-Plus的Service CRUD接口

时间:2021-01-21 12:56:42      阅读:71      评论:0      收藏:0      [点我收藏+]

前缀命名方式区分Mapper层。

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

查询一条记录

根据 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 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 listObjs(Wrapper 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);

MyBatis-Plus的Service CRUD接口

原文:https://www.cnblogs.com/wattmelon/p/14307272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!