MybatisPlus通用增强BaseMapper,新增通过ID更新实体全部字段或只更新非空字段方法

通用扩展BaseMapper

作用:继承MybatisPlus原有的BaseMapper,功能增强。比如:批量查询、批量更新、查询单个或多个VO对象…有了这些方法Service层就不需要去继承MybatisPlus的IService了,减少代码侵入。

代码自取:

package xxx.common.mybatis.core.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.zxsl.core.exception.NotFoundException;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 自定义Mapper接口, 实现自定义扩展
 * @param <T> table实体 泛型
 * @author Boven
 * @since 2024-01-13
 */
@SuppressWarnings("unchecked")
public interface BaseMapperExt<T> extends BaseMapper<T> {
   
   

    Log log = LogFactory.getLog(BaseMapperExt.class);

    default Class<T> currentModelClass() {
   
   
        return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapperExt.class, 0);
    }


    // -------------------------- 插入 --------------------------

    /**
     * 批量插入
     */
    default boolean insertBatch(Collection<T> entityList) {
   
   
        return Db.saveBatch(entityList);
    }

    /**
     * 批量插入(包含限制条数)
     */
    default boolean insertBatch(Collection<T> entityList, int batchSize) {
   
   
        return Db.saveBatch(entityList, batchSize);
    }


    /**
     * 插入或更新(包含限制条数)
     */
    default boolean insertOrUpdate(T entity) {
   
   
        return Db.saveOrUpdate(entity);
    }

    /**
     * 批量插入或更新
     */
    default boolean insertOrUpdateBatch(Collection<T> entityList) {
   
   
        return Db.saveOrUpdateBatch(entityList);
    }

    /**
     * 批量插入或更新(包含限制条数)
     */
    default boolean insertOrUpdateBatch(Collection<T> entityList, int batchSize) {
   
   
        return Db.saveOrUpdateBatch(entityList, batchSize);
    }


    // -------------------------- 更新 --------------------------

    /**
     * 更新所有字段,包括空值字段
     * @see xxx.common.mybatis.methods.UpdateAlwaysById
     */
    int updateAlwaysById(@Param(Constants.ENTITY) T entity);

    /**
     * 更新非空字段
     * @see xxx.common.mybatis.methods.UpdateNotNullById
     */
    int updateNotNullById(@Param(Constants.ENTITY) T entity);

    /**
     * 更新非空且非空白字符串字段
     * @see xxx.common.mybatis.methods.UpdateNotEmptyById
     */
    int updateNotEmptyById(@Param(Constants.ENTITY) T entity);

    /**
     * 批量更新
     */
    default boolean updateBatchById(Collection<T> entityList) {
   
   
        return Db.updateBatchById(entityList);
    }

    /**
     * 批量更新(包含限制条数)
     */
    default boolean updateBatchById(Collection<T> entityList, int batchSize) {
   
   
        return Db.updateBatchById(entityList, batchSize);
    }


    // -------------------------- 查询 --------------------------

    /**
     * 根据 ID 查询
     */
    default T selectByIdValid(Serializable id) {
   
   
        T obj = this.selectById(id);
        if (ObjectUtil.isNull(obj))
            throw new NotFoundException();
        return obj;
    }

    default <C> C selectVoById(Serializable id, Class<C> voClass) {
   
   
        T obj = this.selectById(id);
        if (ObjectUtil.isNull(obj)) {
   
   
            return null;
        }
        // return MapstructUtils.convert(obj, voClass);
        return BeanUtil.copyProperties(obj, voClass);
        // return BeanUtils.copyProperties(obj, voClass);
        // return BeanCopyUtils.copy(obj, voClass);
    }

    default <C> List<C> selectVoBatchIds(Collection<? extends Serializable> idList, Class<C> voClass) {
   
   
        List<T> list = this.selectBatchIds(idList);
        if (CollUtil.isEmpty(list)) {
   
   
            return CollUtil.newArrayList();
        }
        // return BeanCopyUtils.copyList(list, voClass);
        return BeanUtil.copyToList(list, voClass);
    }


    /**
     * 查询(根据 columnMap 条件)
     */
    default <C> List<C> selectVoByMap(Map<String, Object> map, Class<C> voClass) {
   
   
        List<T> list = this.selectByMap(map);
        if (CollUtil.isEmpty(list)) {
   
   
            return CollUtil.newArrayList();
        }
        return BeanUtil.copyToList(list, voClass);
        // return BeanCopyUtils.copyList(list, voClass);
    }

    /**
     * 根据 entity 条件,查询一条记录
     */
    default <C> C selectVoOne(Wrapper<T> wrapper, Class<C> voClass) {
   
   
        T obj = this.selectO
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值