Procházet zdrojové kódy

修改批量删除

hmy před 1 rokem
rodič
revize
57fc02cee1

+ 35 - 0
src/main/java/cn/flowbb/framework/mybatis/core/mapper/BaseMapperX.java

@@ -1,17 +1,29 @@
 package cn.flowbb.framework.mybatis.core.mapper;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
 
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 import com.baomidou.mybatisplus.extension.toolkit.Db;
+import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 import com.github.yulichang.base.MPJBaseMapper;
 import com.github.yulichang.interfaces.MPJBaseJoin;
 import com.github.yulichang.wrapper.MPJLambdaWrapper;
@@ -20,6 +32,7 @@ import cn.flowbb.framework.mybatis.common.pojo.PageParam;
 import cn.flowbb.framework.mybatis.common.pojo.PageResult;
 import cn.flowbb.framework.mybatis.common.pojo.SortablePageParam;
 import cn.flowbb.framework.mybatis.common.pojo.SortingField;
+import cn.flowbb.framework.mybatis.core.util.DbX;
 import cn.flowbb.framework.mybatis.core.util.MyBatisUtils;
 import cn.hutool.core.collection.CollUtil;
 
@@ -32,6 +45,7 @@ import cn.hutool.core.collection.CollUtil;
 public interface BaseMapperX<T> extends MPJBaseMapper<T> {
 
 	default PageResult<T> selectPage(SortablePageParam pageParam, @Param("ew") Wrapper<T> queryWrapper) {
+
 		return selectPage(pageParam, pageParam.getSortingFields(), queryWrapper);
 	}
 
@@ -241,4 +255,25 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> {
 		return Db.saveOrUpdateBatch(collection);
 	}
 
+	default UpdateWrapper<T> getUpdateWrapper(T t) {
+		return new UpdateWrapper<>();
+	}
+
+	@SuppressWarnings("deprecation")
+	default boolean updateBatchByColumn(Collection<T> entityList) {
+		if (CollectionUtils.isEmpty(entityList)) {
+			return false;
+		}
+		final Log log = LogFactory.getLog(this.getClass());
+		Class<T> entityClass = DbX.getEntityClass(entityList);
+		TableInfo tableInfo = DbX.getTableInfo(entityClass);
+		Class<?> mapperClass = ClassUtils.toClassConfident(tableInfo.getCurrentNamespace());
+		String sqlStatement = SqlHelper.getSqlStatement(mapperClass, SqlMethod.UPDATE);
+		return SqlHelper.executeBatch(entityClass, log, entityList, 1000, (sqlSession, entity) -> {
+			Map<String, Object> param = new HashMap<>();
+			param.put(Constants.ENTITY, entity);
+			param.put(Constants.WRAPPER, getUpdateWrapper(entity));
+			sqlSession.update(sqlStatement, param);
+		});
+	}
 }

+ 149 - 0
src/main/java/cn/flowbb/framework/mybatis/core/util/DbX.java

@@ -0,0 +1,149 @@
+package cn.flowbb.framework.mybatis.core.util;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+import org.apache.ibatis.binding.MapperMethod;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
+
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.baomidou.mybatisplus.core.enums.SqlMethod;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.Assert;
+import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Constants;
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
+import com.baomidou.mybatisplus.extension.toolkit.Db;
+import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
+
+public class DbX {
+	private static final Log log = LogFactory.getLog(Db.class);
+
+	/**
+	 * 从entity中尝试获取实体类型
+	 *
+	 * @param entity 实体
+	 * @param <T>    实体类型
+	 * @return 实体类型
+	 */
+	@SuppressWarnings("unchecked")
+	public static <T> Class<T> getEntityClass(T entity) {
+
+		return (Class<T>) entity.getClass();
+	}
+
+	/**
+	 * 从集合中获取实体类型
+	 *
+	 * @param entityList 实体集合
+	 * @param <T>        实体类型
+	 * @return 实体类型
+	 */
+	public static <T> Class<T> getEntityClass(Collection<T> entityList) {
+		Class<T> entityClass = null;
+		for (T entity : entityList) {
+			if (entity != null && entity.getClass() != null) {
+				entityClass = getEntityClass(entity);
+				break;
+			}
+		}
+		Assert.notNull(entityClass, "error: can not get entityClass from entityList");
+		return entityClass;
+	}
+
+	/**
+	 * 获取表信息,获取不到报错提示
+	 *
+	 * @param entityClass 实体类
+	 * @param <T>         实体类型
+	 * @return 对应表信息
+	 */
+	public static <T> TableInfo getTableInfo(Class<T> entityClass) {
+		return Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(
+				() -> ExceptionUtils.mpe("error: can not find TableInfo from Class: \"%s\".", entityClass.getName()));
+	}
+
+	/**
+	 * 根据ID 批量更新
+	 *
+	 * @param entityList 实体对象集合
+	 * @param batchSize  更新批次数量
+	 */
+	@SuppressWarnings("deprecation")
+	public static <T> boolean updateBatchById(Collection<T> entityList, int batchSize) {
+		Class<T> entityClass = getEntityClass(entityList);
+		TableInfo tableInfo = getTableInfo(entityClass);
+		String sqlStatement = SqlHelper.getSqlStatement(ClassUtils.toClassConfident(tableInfo.getCurrentNamespace()),
+				SqlMethod.UPDATE_BY_ID);
+		return SqlHelper.executeBatch(entityClass, log, entityList, batchSize, (sqlSession, entity) -> {
+			MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
+			param.put(Constants.ENTITY, entity);
+			sqlSession.update(sqlStatement, param);
+		});
+	}
+
+	@SuppressWarnings("deprecation")
+	public static <T> boolean updateBatchByColumn(Collection<T> entityList) {
+		if (CollectionUtils.isEmpty(entityList)) {
+			return false;
+		}
+		Class<T> entityClass = getEntityClass(entityList);
+		TableInfo tableInfo = getTableInfo(entityClass);
+		Class<?> mapperClass = ClassUtils.toClassConfident(tableInfo.getCurrentNamespace());
+
+		String sqlStatement = SqlHelper.getSqlStatement(mapperClass, SqlMethod.UPDATE);
+		return SqlHelper.executeBatch(entityClass, log, entityList, 1000, (sqlSession, entity) -> {
+			UpdateWrapper<?> updateWrapper = new UpdateWrapper<>();
+//			updateWrapper.eq(a, idCard);
+			Map<String, Object> param = new HashMap<>();
+			param.put(Constants.ENTITY, entity);
+			param.put(Constants.WRAPPER, updateWrapper);
+			sqlSession.update(sqlStatement, param);
+		});
+
+	}
+
+//	return executeBatch(sqlSessionFactory, log, list, batchSize, (sqlSession, entity) -> {
+//        if (predicate.test(sqlSession, entity)) {
+//            sqlSession.insert(sqlStatement, entity);
+//        } else {
+//            consumer.accept(sqlSession, entity);
+//        }
+//    });
+	/**
+	 * 批量修改插入
+	 *
+	 * @param entityList 实体对象集合
+	 * @param batchSize  每次的数量
+	 */
+	@SuppressWarnings("deprecation")
+	public static <T> boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
+		if (CollectionUtils.isEmpty(entityList)) {
+			return false;
+		}
+		Class<T> entityClass = getEntityClass(entityList);
+		TableInfo tableInfo = getTableInfo(entityClass);
+		Class<?> mapperClass = ClassUtils.toClassConfident(tableInfo.getCurrentNamespace());
+		String keyProperty = tableInfo.getKeyProperty();
+		Assert.notEmpty(keyProperty,
+				"error: can not execute. because can not find column for primary key from entity!");
+		return SqlHelper.saveOrUpdateBatch(entityClass, mapperClass, log, entityList, batchSize,
+				(sqlSession, entity) -> {
+					Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
+					return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession
+							.selectList(SqlHelper.getSqlStatement(mapperClass, SqlMethod.SELECT_BY_ID), entity));
+				}, (sqlSession, entity) -> {
+					MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
+					param.put(Constants.ENTITY, entity);
+					sqlSession.update(SqlHelper.getSqlStatement(mapperClass, SqlMethod.UPDATE_BY_ID), param);
+				});
+	}
+}