美文网首页
mybatis-plus IService源码整理

mybatis-plus IService源码整理

作者: 一介书生独醉江湖 | 来源:发表于2022-05-29 13:46 被阅读0次
# 下面是IService的源码摘录的;
# 我这里只是加了注释,稍加整理,加深印象,有助于自己使用的时候想起来;

一、保存(save)

# 保存 entity
    default boolean save(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().insert(entity));
    }

# 批量保存 entityList , 有事务回滚,默认批次数:1000
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveBatch(Collection<T> entityList) {
        return this.saveBatch(entityList, 1000);
    }

    boolean saveBatch(Collection<T> entityList, int batchSize);

二、删除(delete)

# 根据主键id删除
default boolean removeById(Serializable id) {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
    }
# 根据某列(某几列)的条件删除
    default boolean removeByMap(Map<String, Object> columnMap) {
        Assert.notEmpty(columnMap, "error: columnMap must not be empty", new Object[0]);
        return SqlHelper.retBool(this.getBaseMapper().deleteByMap(columnMap));
    }
# 根据Wrapper 组装的条件删除
    default boolean remove(Wrapper<T> queryWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().delete(queryWrapper));
    }
# 根据主键集合id删除
    default boolean removeByIds(Collection<? extends Serializable> idList) {
        return CollectionUtils.isEmpty(idList) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(idList));
    }

三、更新(update)

# 根据主键的id进行更新,传入的entity需要包含主键id,并有值
    default boolean updateById(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().updateById(entity));
    }

# 根据Wrapper 组装的条件进行更新,warpper中需要有set的值
# 可参考:Mybatis Plus LambdaUpdateWrapper 用法记录(https://www.jianshu.com/p/de7a60be1715)
    default boolean update(Wrapper<T> updateWrapper) {
        return this.update((Object)null, updateWrapper);
    }

# 根据Wrapper 组装的条件进行更新,entity是需要更新的对象
# 可参考:Mybatis Plus LambdaUpdateWrapper 用法记录(https://www.jianshu.com/p/de7a60be1715)
    default boolean update(T entity, Wrapper<T> updateWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

# 根据批量的主键id进行更新,传入的entityList中的每一个entity对象,需要包含主键id,并有值,默认批次数:1000
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean updateBatchById(Collection<T> entityList) {
        return this.updateBatchById(entityList, 1000);
    }

    boolean updateBatchById(Collection<T> entityList, int batchSize);

四、批量保存或更新(saveOrUpdate)

# 保存或更新entity , 主要看entity主键id的数据是否已经插入过,没有则保存,有则更新
   boolean saveOrUpdate(T entity);

# 根据wrapper条件保存或更新entity
    default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
        return this.update(entity, updateWrapper) || this.saveOrUpdate(entity);
    }

# 批量保存或更新 entityList , 有事务回滚,默认批次数:1000
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveOrUpdateBatch(Collection<T> entityList) {
        return this.saveOrUpdateBatch(entityList, 1000);
    }

    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

五、查询(get)

# 根据主键id进行查询
default T getById(Serializable id) {
        return this.getBaseMapper().selectById(id);
    }

# 根据主键id集合进行查询
    default List<T> listByIds(Collection<? extends Serializable> idList) {
        return this.getBaseMapper().selectBatchIds(idList);
    }

# 根据某列(某几列)的条件进行查询
    default List<T> listByMap(Map<String, Object> columnMap) {
        return this.getBaseMapper().selectByMap(columnMap);
    }

# 根据Wrapper组装条件进行查询 , 默认有多个 result 会抛出异常
    default T getOne(Wrapper<T> queryWrapper) {
        return this.getOne(queryWrapper, true);
    }

# 根据Wrapper组装条件进行查询 , throwEx 可以指定 有多个 result 是否抛出异常
    T getOne(Wrapper<T> queryWrapper, boolean throwEx);

# 根据Wrapper组装条件进行查询, 查询出的结果是Map数据结构
    Map<String, Object> getMap(Wrapper<T> queryWrapper);

# 根据Wrapper组装条件进行查询, mapper 为转换函数
    <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

# 查询总量,因为传入的条件是空的wrapper
    default int count() {
        return this.count(Wrappers.emptyWrapper());
    }

# 根据wrapper组装条件 查询数据数量
    default int count(Wrapper<T> queryWrapper) {
        return SqlHelper.retCount(this.getBaseMapper().selectCount(queryWrapper));
    }
# 根据wrapper组装条件查询数据集合
    default List<T> list(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectList(queryWrapper);
    }
# 查询所有数据集合
    default List<T> list() {
        return this.list(Wrappers.emptyWrapper());
    }
# 根据wrapper组装条件 分页查询
    default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }
# 分页查询所有数据
    default <E extends IPage<T>> E page(E page) {
        return this.page(page, Wrappers.emptyWrapper());
    }

六、其他(不常用)

public interface IService<T> {
    # 默认批次量为1000
    int DEFAULT_BATCH_SIZE = 1000;

    default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectMaps(queryWrapper);
    }

    default List<Map<String, Object>> listMaps() {
        return this.listMaps(Wrappers.emptyWrapper());
    }

    default List<Object> listObjs() {
        return this.listObjs(Function.identity());
    }

    default <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return this.listObjs(Wrappers.emptyWrapper(), mapper);
    }

    default List<Object> listObjs(Wrapper<T> queryWrapper) {
        return this.listObjs(queryWrapper, Function.identity());
    }

    default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
        return (List)this.getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
    }

    default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectMapsPage(page, queryWrapper);
    }

    default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
        return this.pageMaps(page, Wrappers.emptyWrapper());
    }

    BaseMapper<T> getBaseMapper();

    Class<T> getEntityClass();

    default QueryChainWrapper<T> query() {
        return ChainWrappers.queryChain(this.getBaseMapper());
    }

    default LambdaQueryChainWrapper<T> lambdaQuery() {
        return ChainWrappers.lambdaQueryChain(this.getBaseMapper());
    }

    default KtQueryChainWrapper<T> ktQuery() {
        return ChainWrappers.ktQueryChain(this.getBaseMapper(), this.getEntityClass());
    }

    default KtUpdateChainWrapper<T> ktUpdate() {
        return ChainWrappers.ktUpdateChain(this.getBaseMapper(), this.getEntityClass());
    }

    default UpdateChainWrapper<T> update() {
        return ChainWrappers.updateChain(this.getBaseMapper());
    }

    default LambdaUpdateChainWrapper<T> lambdaUpdate() {
        return ChainWrappers.lambdaUpdateChain(this.getBaseMapper());
    }
}

相关文章

网友评论

      本文标题:mybatis-plus IService源码整理

      本文链接:https://www.haomeiwen.com/subject/dnroprtx.html