Service

Service

  • 通用的 Service SRUD 封装了 IService 接口,进一步封装了 CRUD 操作,为了和 Mapper 区分,Service 采用 get 查询单行,remove 删除、list 查询集合,page 分页前缀命名的方式。

  • 泛型 T 为任意实体对象

  • 对象 Wrapper 为条件构造器。

Save

// 插入一条记录
boolean save(T entity);

// 批量插入
boolean saveBatch(Coolection<T> entityList);

// 批量插入,batchSize 插入批次数量
boolean saveBatch(Coolection<T> entityList, int batchSize)

SaveOrUpdate

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

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

// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);

// 批量修改插入,batchSize 插入批次数量
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

Remove

// 根据 queryWrapper 设置的条件,删除记录
boolean remove(Wrapper<T> queryWrapper);

// 根据 ID 删除
boolean removeById(Serializable id);

// 根据 columnMap 条件,删除记录,columnMap 表字段 map 对象
boolean removeByMap(Map<String, Object> columnMap);

// 删除(根据ID 批量删除),idList 主键 ID 列表
boolean removeByIds(Collection<? extends Serializable> idList);

Update

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);

// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);

// 根据 ID 选择修改
boolean updateById(T entity);

// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);

// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);

Get

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

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

// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);

// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);

// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

List

// 查询所有
List<T> list();

// 查询列表
List<T> list(Wrapper<T> queryWrapper);

// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);

// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);

// 查询所有列表
List<Map<String, Object>> listMaps();

// 查询列表
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);

// 查询全部记录
List<Object> listObjs();

// 查询全部记录
<V> List<V> listObjs(Function<? super Object, V> mapper);

// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);

// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

Page

// 无条件分页查询
IPage<T> page(IPage<T> page);

// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);

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

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

Count

// 查询总记录数
long count();

// 根据 Wrapper 条件,查询总记录数
long count(Wrapper<T> queryWrapper);
public interface IUserService extends IService<User> {
  // 拓展业务中的自定义方法
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

最后更新于

这有帮助吗?