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.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 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 数据实体 */ public abstract class BasicMapperImpl> implements BasicMapper { protected M DB; protected Class clazz; protected String tableName; protected static final Logger logger = LoggerUtil.get(ELogger.SQL_LOG); /** * 构造方法 */ public BasicMapperImpl(M db) { this.clazz = (Class) ( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0]; this.DB = db; TableName annotation = clazz.getAnnotation(TableName.class); if (null == annotation) { this.tableName = StringUtils.camelToUnderline(clazz.getSimpleName().replace("PO", "")); return; } this.tableName = annotation.value(); } public LambdaQueryWrapper query() { return Wrappers.lambdaQuery(); } public UpdateWrapper update() { 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); } /** * 根据 columnMap 条件,删除记录 * * @param columnMap 表字段 map 对象 */ public int deleteByMap(Map columnMap) { return DB.deleteByMap(columnMap); } /** * 根据 entity 条件,删除记录 * * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ public int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.delete(queryWrapper); } /** * 删除(根据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或实体 批量删除) *

* 普通删除: DELETE FROM h2user WHERE id IN ( ? , ? ) *

*

* 逻辑删除: UPDATE h2user SET deleted=1 WHERE id IN ( ? , ? ) AND deleted=0 *

*

* 逻辑删除(填充): UPDATE h2user SET delete_user = 'xxx', deleted=1 WHERE id IN ( ? , ? ) AND deleted=0 *

    注意:无论参数为id还是实体,填充参数只会以方法追加的et参数为准.
      *

      * * @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); } /** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,可以为 null,当entity为null时,无法进行自动填充) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ public int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper) { return DB.update(entity, updateWrapper); } /** * 根据 Wrapper 更新记录 *

      此方法无法进行自动填充,如需自动填充请使用{@link #update(Object, Wrapper)}

      * * @param updateWrapper {@link UpdateWrapper} or {@link LambdaUpdateWrapper} * @since 3.5.4 */ public int update(@Param(Constants.WRAPPER) Wrapper updateWrapper) { return DB.update(updateWrapper); } /** * 根据 ID 查询 * * @param id 主键 */ public T selectById(Serializable id) { return DB.selectById(id); } /** * 查询(根据ID 批量查询) * * @param idList 主键列表(不能为 null 以及 empty) */ public List selectBatchIds(@Param(Constants.COLL) Collection idList) { return DB.selectBatchIds(idList); } /** * 查询(根据ID 批量查询) * * @param idList idList 主键列表(不能为 null 以及 empty) * @param resultHandler resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectBatchIds(@Param(Constants.COLL) Collection idList, ResultHandler resultHandler) { DB.selectBatchIds(idList, resultHandler); } /** * 查询(根据 columnMap 条件) * * @param columnMap 表字段 map 对象 */ public List selectByMap(Map columnMap) { return DB.selectByMap(columnMap); } /** * 查询(根据 columnMap 条件) * * @param columnMap 表字段 map 对象 * @param resultHandler resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectByMap(Map columnMap, ResultHandler resultHandler) { DB.selectByMap(columnMap, resultHandler); } /** * 根据 entity 条件,查询一条记录 *

      查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常

      * * @param queryWrapper 实体对象封装操作类(可以为 null) */ public T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectOne(queryWrapper); } /** * 根据 entity 条件,查询一条记录,现在会根据{@code throwEx}参数判断是否抛出异常,如果为false就直接返回一条数据 *

      查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常

      * * @param queryWrapper 实体对象封装操作类(可以为 null) * @param throwEx boolean 参数,为true如果存在多个结果直接抛出异常 */ public T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper, boolean throwEx) { return DB.selectOne(queryWrapper, throwEx); } /** * 根据 Wrapper 条件,判断是否存在记录 * * @param queryWrapper 实体对象封装操作类 * @return 是否存在记录 */ public boolean exists(Wrapper queryWrapper) { return DB.exists(queryWrapper); } /** * 根据 Wrapper 条件,查询总记录数 * * @param queryWrapper 实体对象封装操作类(可以为 null) */ public Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectCount(queryWrapper); } /** * 根据 entity 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */ public List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectList(queryWrapper); } /** * 根据 entity 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper, ResultHandler resultHandler) { DB.selectList(queryWrapper, resultHandler); } /** * 根据 entity 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类(可以为 null) * @since 3.5.3.2 */ public List selectList(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectList(page, queryWrapper); } /** * 根据 entity 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类(可以为 null) * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectList(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper, ResultHandler resultHandler) { DB.selectList(page, queryWrapper, resultHandler); } /** * 根据 Wrapper 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类 */ public List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectMaps(queryWrapper); } /** * 根据 Wrapper 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类 * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper, ResultHandler> resultHandler) { DB.selectMaps(queryWrapper, resultHandler); } /** * 根据 Wrapper 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 * @since 3.5.3.2 */ public List> selectMaps(IPage> page, @Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectMaps(page, queryWrapper); } /** * 根据 Wrapper 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectMaps(IPage> page, @Param(Constants.WRAPPER) Wrapper queryWrapper, ResultHandler> resultHandler) { DB.selectMaps(page, queryWrapper, resultHandler); } /** * 根据 Wrapper 条件,查询全部记录 *

      注意: 只返回第一个字段的值

      * * @param queryWrapper 实体对象封装操作类(可以为 null) */ public List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectObjs(queryWrapper); } /** * 根据 Wrapper 条件,查询全部记录 *

      注意: 只返回第一个字段的值

      * * @param queryWrapper 实体对象封装操作类(可以为 null) * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ public void selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper, ResultHandler resultHandler) { DB.selectObjs(queryWrapper, resultHandler); } /** * 根据 entity 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类(可以为 null) */ public

      > P selectPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper) { return DB.selectPage(page, queryWrapper); } /** * 根据 Wrapper 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */ public

      >> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper 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 insert(Collection entityList) { return DB.insert(entityList); } /** * 插入(批量) * * @param entityList 实体对象集合 * @param batchSize 插入批次数量 * @since 3.5.7 */ public List insert(Collection entityList, int batchSize) { return DB.insert(entityList, batchSize); } /** * 根据ID 批量更新 * * @param entityList 实体对象集合 * @since 3.5.7 */ public List updateById(Collection entityList) { return DB.updateById(entityList); } /** * 根据ID 批量更新 * * @param entityList 实体对象集合 * @param batchSize 插入批次数量 * @since 3.5.7 */ public List updateById(Collection entityList, int batchSize) { return DB.updateById(entityList, batchSize); } /** * 批量修改或插入 * * @param entityList 实体对象集合 * @since 3.5.7 */ public List insertOrUpdate(Collection entityList) { return DB.insertOrUpdate(entityList); } /** * 批量修改或插入 * * @param entityList 实体对象集合 * @param batchSize 插入批次数量 * @since 3.5.7 */ public List insertOrUpdate(Collection entityList, int batchSize) { return DB.insertOrUpdate(entityList, batchSize); } /** * 批量修改或插入 * * @param entityList 实体对象集合 * @since 3.5.7 */ public List insertOrUpdate(Collection entityList, BiPredicate insertPredicate) { return DB.insertOrUpdate(entityList, insertPredicate); } /** * 批量修改或插入 * * @param entityList 实体对象集合 * @param batchSize 插入批次数量 * @since 3.5.7 */ public List insertOrUpdate(Collection entityList, BiPredicate insertPredicate, int batchSize) { return DB.insertOrUpdate(entityList, insertPredicate, batchSize); } /** * 批量新增 * * @param list 数据实体列表 */ public Boolean addList(List list) { List batchResult = DB.insert(list); // TODO 判断成功 return Boolean.TRUE; } /** * 批量更新 * * @param list 数据实体列表 */ public Boolean modifyList(List list) { List batchResult = DB.updateById(list); // TODO 判断成功 return Boolean.TRUE; } /** * 批量查询其他实体列表 * * @param queryWrapper 查询条件 */ public List selectListObject(Wrapper queryWrapper) { return DB.selectObjs(queryWrapper); } /** * 批量查询其他实体列表 * * @param queryWrapper 查询条件 * @param resultHandler 查询结果处理器 */ public void selectListObject(Wrapper queryWrapper, ResultHandler resultHandler) { DB.selectObjs(queryWrapper, resultHandler); } /** * 根据主键查询单个数据实体 * * @param id 主键值 */ public T get(Serializable id) { return DB.selectById(id); } /** * 条件查询单个数据实体 * * @param queryWrapper 查询条件 */ public T get(Wrapper queryWrapper) { String method = "BasicDao.Wrapper queryWrapper"; try { return DB.selectOne(queryWrapper); } catch (EmptyResultDataAccessException e) { logger.error(method, e); return null; } catch (Exception e) { logger.error(method + ", 发生异常", e); throw new BusinessException(EResultCode.ERROR, "BasicDao.get(SqlBuilder builder)发生异常." + e.getMessage()); } } /** * 根据主键批量获取 * * @param listId 主键列表 */ public List getList(List listId) { return DB.selectBatchIds(listId); } /** * 获取全量数据(慎重) */ public List getList() { return DB.selectList(null); } /** * 根据传入sql查询数据 * * @param queryWrapper 查询条件 */ public List> getMap(Wrapper queryWrapper) { return DB.selectMaps(queryWrapper); } /** * 查询记录条数 * * @param queryWrapper 查询条件 */ public Long getCount(Wrapper queryWrapper) { return DB.selectCount(queryWrapper); } /** * 根据主键删除数据 * * @param id 主键 * @return 是否成功 */ public Boolean remove(Serializable id) { int rowCount = DB.deleteById(id); if (rowCount > 0) { return Boolean.TRUE; } return Boolean.FALSE; } /** * 根据主键删除数据 * * @param listId id列表 * @return 是否成功 */ public Boolean remove(List listId) { int rowCount = DB.deleteByIds(listId); if (rowCount > 0) { return Boolean.TRUE; } return Boolean.FALSE; } /** * 根据条件删除数据 * * @param queryWrapper 查询条件 */ public Boolean remove(Wrapper queryWrapper) { int rowCount = DB.delete(queryWrapper); if (rowCount > 0) { return Boolean.TRUE; } return Boolean.FALSE; } /** * 根据主键逻辑删除数据(逻辑删除) * * @param id 主键 * @return 是否成功 */ public Boolean deleteLogic(Serializable id) { UpdateWrapper where = Wrappers.update(); where.set("is_delete", EYesOrNo.YES.getValue()); where.eq("id", id); int rowCount = DB.update(where); if (rowCount > 0) { return Boolean.TRUE; } return Boolean.FALSE; } /** * 根据逐渐删除数据(逻辑删除) * * @param listId id列表 * @return 是否成功 */ public Boolean deleteLogic(List listId) { UpdateWrapper where = Wrappers.update(); where.set("is_delete", EYesOrNo.YES.getValue()); where.in("id", listId); int rowCount = DB.update(where); if (rowCount > 0) { return Boolean.TRUE; } return Boolean.FALSE; } /** * 分页查询 * * @param search 查询参数 */ @Override public abstract PagerResult search(SearchBasicDTO search); }