From af410d1cde67d4656c9922ff4d846cac0740e3b2 Mon Sep 17 00:00:00 2001 From: liulin <lin.liu@88.com> Date: 星期日, 24 十一月 2024 19:35:08 +0800 Subject: [PATCH] 定时任务改为quartz --- src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java | 322 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 290 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java b/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java index 3e35d2c..4a33f2f 100644 --- a/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java +++ b/src/main/java/com/lunhan/xxx/repository/BasicMapperImpl.java @@ -1,10 +1,14 @@ 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; @@ -13,25 +17,26 @@ 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 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 abstract class BasicMapperImpl<T> implements 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; @@ -40,16 +45,17 @@ /** * 构造方法 */ - public BasicMapperImpl(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() { @@ -70,6 +76,26 @@ } /** + * 根据 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 实体对象 @@ -80,12 +106,65 @@ } /** + * 根据 columnMap 条件,删除记录 + * + * @param columnMap 表字段 map 对象 + */ + public int deleteByMap(Map<String, Object> columnMap) { + return DB.deleteByMap(columnMap); + } + + /** * 根据 entity 条件,删除记录 * * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ - public int delete(Wrapper<T> queryWrapper) { + public int delete(@Param(Constants.WRAPPER) Wrapper<T> 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或实体 批量删除) + * <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); } /** @@ -93,7 +172,7 @@ * * @param entity 实体对象 */ - public int updateById(T entity) { + public int updateById(@Param(Constants.ENTITY) T entity) { return DB.updateById(entity); } @@ -103,14 +182,25 @@ * @param entity 实体对象 (set 条件值,可以为 null,当entity为null时,无法进行自动填充) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ - public int update(T entity, Wrapper<T> updateWrapper) { + 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 主键ID + * @param id 主键 */ public T selectById(Serializable id) { return DB.selectById(id); @@ -119,21 +209,72 @@ /** * 查询(根据ID 批量查询) * - * @param idList 主键ID列表(不能为 null 以及 empty) + * @param idList 主键列表(不能为 null 以及 empty) */ - public List<T> selectBatchIds(Collection<? extends Serializable> idList) { + public List<T> selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList) { return DB.selectBatchIds(idList); } /** * 查询(根据ID 批量查询) * - * @param idList idList 主键ID列表(不能为 null 以及 empty) + * @param idList idList 主键列表(不能为 null 以及 empty) * @param resultHandler resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ - public void selectBatchIds(Collection<? extends Serializable> idList, ResultHandler<T> resultHandler) { + public void selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList, ResultHandler<T> resultHandler) { DB.selectBatchIds(idList, resultHandler); + } + + /** + * 查询(根据 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); } /** @@ -141,7 +282,7 @@ * * @param queryWrapper 实体对象封装操作类(可以为 null) */ - public Long selectCount(Wrapper<T> queryWrapper) { + public Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) { return DB.selectCount(queryWrapper); } @@ -150,7 +291,7 @@ * * @param queryWrapper 实体对象封装操作类(可以为 null) */ - public List<T> selectList(Wrapper<T> queryWrapper) { + public List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) { return DB.selectList(queryWrapper); } @@ -161,7 +302,7 @@ * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ - public void selectList(Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) { + public void selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) { DB.selectList(queryWrapper, resultHandler); } @@ -172,8 +313,8 @@ * @param queryWrapper 实体对象封装操作类(可以为 null) * @since 3.5.3.2 */ - public List<T> selectList(IPage<T> page, Wrapper<T> queryWrapper) { - return DB.selectList(queryWrapper); + public List<T> selectList(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) { + return DB.selectList(page, queryWrapper); } /** @@ -184,16 +325,17 @@ * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ - public void selectList(IPage<T> page, Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) { + public void selectList(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) { DB.selectList(page, queryWrapper, resultHandler); } + /** * 根据 Wrapper 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类 */ - public List<Map<String, Object>> selectMaps(Wrapper<T> queryWrapper) { + public List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) { return DB.selectMaps(queryWrapper); } @@ -204,7 +346,7 @@ * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ - public void selectMaps(Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) { + public void selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) { DB.selectMaps(queryWrapper, resultHandler); } @@ -215,7 +357,7 @@ * @param queryWrapper 实体对象封装操作类 * @since 3.5.3.2 */ - public List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper) { + public List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) { return DB.selectMaps(page, queryWrapper); } @@ -227,7 +369,7 @@ * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ - public void selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) { + 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); } @@ -237,7 +379,7 @@ * * @param queryWrapper 实体对象封装操作类(可以为 null) */ - public <E> List<E> selectObjs(Wrapper<T> queryWrapper) { + public <E> List<E> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) { return DB.selectObjs(queryWrapper); } @@ -249,8 +391,123 @@ * @param resultHandler 结果处理器 {@link ResultHandler} * @since 3.5.4 */ - public <E> void selectObjs(Wrapper<T> queryWrapper, ResultHandler<E> resultHandler) { + 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); } @@ -324,6 +581,7 @@ /** * 根据主键批量获取 + * * @param listId 主键列表 */ public <E extends Serializable> List<T> getList(List<E> listId) { @@ -356,9 +614,9 @@ } /** - * 根据主键id删除数据 + * 根据主键删除数据 * - * @param id 主键id + * @param id 主键 * @return 是否成功 */ public Boolean remove(Serializable id) { @@ -370,7 +628,7 @@ } /** - * 根据主键id删除数据 + * 根据主键删除数据 * * @param listId id列表 * @return 是否成功 @@ -398,9 +656,9 @@ } /** - * 根据主键id逻辑删除数据(逻辑删除) + * 根据主键逻辑删除数据(逻辑删除) * - * @param id 主键id + * @param id 主键 * @return 是否成功 */ public Boolean deleteLogic(Serializable id) { -- Gitblit v1.9.3