liulin
2024-09-03 ee5baccab552762e9d57fb3fab04cd099dae018d
src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java
@@ -1,35 +1,42 @@
package com.lunhan.xxx.repository;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.batch.BatchSqlSession;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lunhan.xxx.common.PagerResult;
import com.lunhan.xxx.common.enums.ELogger;
import com.lunhan.xxx.common.enums.EResultCode;
import com.lunhan.xxx.common.enums.EYesOrNo;
import com.lunhan.xxx.common.exceptions.BusinessException;
import com.lunhan.xxx.common.util.LoggerUtil;
import com.lunhan.xxx.entity.dto.SearchBasicDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.ResultHandler;
import org.slf4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Table;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
/**
 * 数据访问 实现类
 *
 * @param <T> 数据实体
 */
public class BasicMapper<T> {
    protected BaseMapper<T> DB;
public abstract class BasicMapperImpl<T, M extends BasicMapper<T>> implements BasicMapper<T> {
    protected M DB;
    protected Class<T> clazz;
    protected String tableName;
@@ -38,16 +45,17 @@
    /**
     * 构造方法
     */
    public BasicMapper(BaseMapper<T> db) {
    public BasicMapperImpl(M db) {
        this.clazz = (Class<T>) (
                (ParameterizedType) getClass().getGenericSuperclass()
        ).getActualTypeArguments()[0];
        this.DB = db;
        Table annotation = clazz.getAnnotation(Table.class);
        TableName annotation = clazz.getAnnotation(TableName.class);
        if (null == annotation) {
            this.tableName = StringUtils.camelToUnderline(clazz.getSimpleName().replace("PO", ""));
            return;
        }
        this.tableName = annotation.name();
        this.tableName = annotation.value();
    }
    public LambdaQueryWrapper<T> query() {
@@ -58,90 +66,499 @@
        return Wrappers.update();
    }
    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
    public int insert(T entity) {
        return DB.insert(entity);
    }
    /**
     * 根据 ID 删除
     *
     * @param id 主键
     */
    public int deleteById(Serializable id) {
        return DB.deleteById(id);
    }
    /**
     * 根据 ID 删除
     *
     * @param useFill 是否填充
     * @param obj     主键或实体
     * @since 3.5.7
     */
    public int deleteById(Object obj, boolean useFill) {
        return DB.deleteById(obj, useFill);
    }
    /**
     * 根据实体(ID)删除
     *
     * @param entity 实体对象
     * @since 3.4.4
     */
    public int deleteById(T entity) {
        return DB.deleteById(entity);
    }
    public int delete(Wrapper<T> queryWrapper) {
    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    public int deleteByMap(Map<String, Object> columnMap) {
        return DB.deleteByMap(columnMap);
    }
    /**
     * 根据 entity 条件,删除记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    public int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.delete(queryWrapper);
    }
    public int updateById(T entity) {
    /**
     * 删除(根据ID或实体 批量删除)
     *
     * @param idList 主键列表或实体列表(不能为 null 以及 empty)
     * @deprecated 3.5.7 {@link #deleteByIds(Collection)}
     */
    @Deprecated
    public int deleteBatchIds(@Param(Constants.COLL) Collection<?> idList) {
        return DB.deleteBatchIds(idList);
    }
    /**
     * 删除(根据ID或实体 批量删除)
     *
     * @param idList 主键列表或实体列表(不能为 null 以及 empty)
     * @since 3.5.7
     */
    public int deleteByIds(@Param(Constants.COLL) Collection<?> idList) {
        return DB.deleteByIds(idList);
    }
    /**
     * 删除(根据ID或实体 批量删除)
     * <p>
     * 普通删除: DELETE FROM h2user WHERE id IN ( ? , ? )
     * </p>
     * <p>
     * 逻辑删除: UPDATE h2user SET deleted=1 WHERE id IN ( ? , ? ) AND deleted=0
     * </p>
     * <p>
     * 逻辑删除(填充): UPDATE h2user SET delete_user = 'xxx', deleted=1 WHERE id IN ( ? , ? ) AND deleted=0
     *     <ul>注意:无论参数为id还是实体,填充参数只会以方法追加的et参数为准.<ul/>
     * </p>
     *
     * @param collections 主键列表或实体列表(不能为 null 以及 empty)
     * @param useFill     逻辑删除下是否填充
     * @since 3.5.7
     */
    public int deleteByIds(@Param(Constants.COLL) Collection<?> collections, boolean useFill) {
        return DB.deleteByIds(collections, useFill);
    }
    /**
     * 根据 ID 修改
     *
     * @param entity 实体对象
     */
    public int updateById(@Param(Constants.ENTITY) T entity) {
        return DB.updateById(entity);
    }
    public int update(T entity, Wrapper<T> updateWrapper) {
    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象 (set 条件值,可以为 null,当entity为null时,无法进行自动填充)
     * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    public int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper) {
        return DB.update(entity, updateWrapper);
    }
    /**
     * 根据 Wrapper 更新记录
     * <p>此方法无法进行自动填充,如需自动填充请使用{@link #update(Object, Wrapper)}</p>
     *
     * @param updateWrapper {@link UpdateWrapper} or {@link LambdaUpdateWrapper}
     * @since 3.5.4
     */
    public int update(@Param(Constants.WRAPPER) Wrapper<T> updateWrapper) {
        return DB.update(updateWrapper);
    }
    /**
     * 根据 ID 查询
     *
     * @param id 主键
     */
    public T selectById(Serializable id) {
        return DB.selectById(id);
    }
    public List<T> selectBatchIds(Collection<? extends Serializable> idList) {
    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键列表(不能为 null 以及 empty)
     */
    public List<T> selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList) {
        return DB.selectBatchIds(idList);
    }
    public void selectBatchIds(Collection<? extends Serializable> idList, ResultHandler<T> resultHandler) {
    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList        idList 主键列表(不能为 null 以及 empty)
     * @param resultHandler resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public void selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList, ResultHandler<T> resultHandler) {
        DB.selectBatchIds(idList, resultHandler);
    }
    public Long selectCount(Wrapper<T> queryWrapper) {
    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    public List<T> selectByMap(Map<String, Object> columnMap) {
        return DB.selectByMap(columnMap);
    }
    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap     表字段 map 对象
     * @param resultHandler resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public void selectByMap(Map<String, Object> columnMap, ResultHandler<T> resultHandler) {
        DB.selectByMap(columnMap, resultHandler);
    }
    /**
     * 根据 entity 条件,查询一条记录
     * <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    public T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectOne(queryWrapper);
    }
    /**
     * 根据 entity 条件,查询一条记录,现在会根据{@code throwEx}参数判断是否抛出异常,如果为false就直接返回一条数据
     * <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     * @param throwEx      boolean 参数,为true如果存在多个结果直接抛出异常
     */
    public T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, boolean throwEx) {
        return DB.selectOne(queryWrapper, throwEx);
    }
    /**
     * 根据 Wrapper 条件,判断是否存在记录
     *
     * @param queryWrapper 实体对象封装操作类
     * @return 是否存在记录
     */
    public boolean exists(Wrapper<T> queryWrapper) {
        return DB.exists(queryWrapper);
    }
    /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    public Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectCount(queryWrapper);
    }
    public List<T> selectList(Wrapper<T> queryWrapper) {
    /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    public List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectList(queryWrapper);
    }
    public void selectList(Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) {
    /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper  实体对象封装操作类(可以为 null)
     * @param resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public void selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) {
        DB.selectList(queryWrapper, resultHandler);
    }
    public List<T> selectList(IPage<T> page, Wrapper<T> queryWrapper) {
    /**
     * 根据 entity 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     * @since 3.5.3.2
     */
    public List<T> selectList(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectList(page, queryWrapper);
    }
    public void selectList(IPage<T> page, Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) {
    /**
     * 根据 entity 条件,查询全部记录(并翻页)
     *
     * @param page          分页查询条件
     * @param queryWrapper  实体对象封装操作类(可以为 null)
     * @param resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public void selectList(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) {
        DB.selectList(page, queryWrapper, resultHandler);
    }
    public List<Map<String, Object>> selectMaps(Wrapper<T> queryWrapper) {
    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类
     */
    public List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectMaps(queryWrapper);
    }
    public void selectMaps(Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) {
    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper  实体对象封装操作类
     * @param resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public void selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) {
        DB.selectMaps(queryWrapper, resultHandler);
    }
    public List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper) {
    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     * @since 3.5.3.2
     */
    public List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectMaps(page, queryWrapper);
    }
    public void selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) {
    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page          分页查询条件
     * @param queryWrapper  实体对象封装操作类
     * @param resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public void selectMaps(IPage<? extends Map<String, Object>> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) {
        DB.selectMaps(page, queryWrapper, resultHandler);
    }
    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    public <E> List<E> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectObjs(queryWrapper);
    }
    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper  实体对象封装操作类(可以为 null)
     * @param resultHandler 结果处理器 {@link ResultHandler}
     * @since 3.5.4
     */
    public <E> void selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<E> resultHandler) {
        DB.selectObjs(queryWrapper, resultHandler);
    }
    /**
     * 根据 entity 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    public <P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectPage(page, queryWrapper);
    }
    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     */
    public <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        return DB.selectMapsPage(page, queryWrapper);
    }
    /**
     * 主键存在更新记录,否插入一条记录
     *
     * @param entity 实体对象 (不能为空)
     * @since 3.5.7
     */
    public boolean insertOrUpdate(T entity) {
        return DB.insertOrUpdate(entity);
    }
    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     * @since 3.5.7
     */
    public List<BatchResult> insert(Collection<T> entityList) {
        return DB.insert(entityList);
    }
    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     * @since 3.5.7
     */
    public List<BatchResult> insert(Collection<T> entityList, int batchSize) {
        return DB.insert(entityList, batchSize);
    }
    /**
     * 根据ID 批量更新
     *
     * @param entityList 实体对象集合
     * @since 3.5.7
     */
    public List<BatchResult> updateById(Collection<T> entityList) {
        return DB.updateById(entityList);
    }
    /**
     * 根据ID 批量更新
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     * @since 3.5.7
     */
    public List<BatchResult> updateById(Collection<T> entityList, int batchSize) {
        return DB.updateById(entityList, batchSize);
    }
    /**
     * 批量修改或插入
     *
     * @param entityList 实体对象集合
     * @since 3.5.7
     */
    public List<BatchResult> insertOrUpdate(Collection<T> entityList) {
        return DB.insertOrUpdate(entityList);
    }
    /**
     * 批量修改或插入
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     * @since 3.5.7
     */
    public List<BatchResult> insertOrUpdate(Collection<T> entityList, int batchSize) {
        return DB.insertOrUpdate(entityList, batchSize);
    }
    /**
     * 批量修改或插入
     *
     * @param entityList 实体对象集合
     * @since 3.5.7
     */
    public List<BatchResult> insertOrUpdate(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate) {
        return DB.insertOrUpdate(entityList, insertPredicate);
    }
    /**
     * 批量修改或插入
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     * @since 3.5.7
     */
    public List<BatchResult> insertOrUpdate(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate, int batchSize) {
        return DB.insertOrUpdate(entityList, insertPredicate, batchSize);
    }
    /**
     * 批量新增
     *
     * @param list 数据实体列表
     */
    public Boolean addList(List<T> list) {
        List<BatchResult> batchResult = DB.insert(list);
        // TODO 判断成功
        return Boolean.TRUE;
    }
    /**
     * 批量更新
     *
     * @param list 数据实体列表
     */
    public Boolean modifyList(List<T> list) {
        List<BatchResult> batchResult = DB.updateById(list);
        // TODO 判断成功
        return Boolean.TRUE;
    }
    /**
     * 批量查询其他实体列表
     *
     * @param queryWrapper 查询条件
     */
    public <E> List<E> selectListObject(Wrapper<T> queryWrapper) {
        return DB.selectObjs(queryWrapper);
    }
    /**
     * 批量查询其他实体列表
     *
     * @param queryWrapper  查询条件
     * @param resultHandler 查询结果处理器
     */
    public <E> void selectListObject(Wrapper<T> queryWrapper, ResultHandler<E> resultHandler) {
        DB.selectObjs(queryWrapper, resultHandler);
    }
    /**
     * 根据条件获取数据实体列表
     * 根据主键查询单个数据实体
     *
     * @param queryWrapper 查询条件
     * @param id 主键值
     */
    @Transactional(readOnly = true)
    protected List<T> getList(Wrapper<T>... queryWrapper) {
        return DB.selectList(queryWrapper.length > 0 ? queryWrapper[0] : null);
    public T get(Serializable id) {
        return DB.selectById(id);
    }
    /**
@@ -149,8 +566,7 @@
     *
     * @param queryWrapper 查询条件
     */
    @Transactional(readOnly = true)
    protected T get(Wrapper<T> queryWrapper) {
    public T get(Wrapper<T> queryWrapper) {
        String method = "BasicDao.Wrapper<T> queryWrapper";
        try {
            return DB.selectOne(queryWrapper);
@@ -163,6 +579,21 @@
        }
    }
    /**
     * 根据主键批量获取
     *
     * @param listId 主键列表
     */
    public <E extends Serializable> List<T> getList(List<E> listId) {
        return DB.selectBatchIds(listId);
    }
    /**
     * 获取全量数据(慎重)
     */
    public List<T> getList() {
        return DB.selectList(null);
    }
    /**
     * 根据传入sql查询数据
@@ -183,12 +614,12 @@
    }
    /**
     * 根据主键id删除数据
     * 根据主键删除数据
     *
     * @param id 主键id
     * @param id 主键
     * @return 是否成功
     */
    public Boolean remove(Long id) {
    public Boolean remove(Serializable id) {
        int rowCount = DB.deleteById(id);
        if (rowCount > 0) {
            return Boolean.TRUE;
@@ -197,12 +628,12 @@
    }
    /**
     * 根据主键id删除数据
     * 根据主键删除数据
     *
     * @param listId id列表
     * @return 是否成功
     */
    public Boolean remove(List<Long> listId) {
    public <E extends Serializable> Boolean remove(List<E> listId) {
        int rowCount = DB.deleteByIds(listId);
        if (rowCount > 0) {
            return Boolean.TRUE;
@@ -216,7 +647,7 @@
     *
     * @param queryWrapper 查询条件
     */
    protected Boolean remove(Wrapper<T> queryWrapper) {
    public Boolean remove(Wrapper<T> queryWrapper) {
        int rowCount = DB.delete(queryWrapper);
        if (rowCount > 0) {
            return Boolean.TRUE;
@@ -225,12 +656,12 @@
    }
    /**
     * 根据主键id逻辑删除数据(逻辑删除)
     * 根据主键逻辑删除数据(逻辑删除)
     *
     * @param id 主键id
     * @param id 主键
     * @return 是否成功
     */
    public Boolean deleteLogic(Long id) {
    public Boolean deleteLogic(Serializable id) {
        UpdateWrapper<T> where = Wrappers.update();
        where.set("is_delete", EYesOrNo.YES.getValue());
        where.eq("id", id);
@@ -247,7 +678,7 @@
     * @param listId id列表
     * @return 是否成功
     */
    public Boolean deleteLogic(List<Long> listId) {
    public <E extends Serializable> Boolean deleteLogic(List<E> listId) {
        UpdateWrapper<T> where = Wrappers.update();
        where.set("is_delete", EYesOrNo.YES.getValue());
        where.in("id", listId);
@@ -257,4 +688,12 @@
        }
        return Boolean.FALSE;
    }
    /**
     * 分页查询
     *
     * @param search 查询参数
     */
    @Override
    public abstract PagerResult<T> search(SearchBasicDTO search);
}