|
|
@@ -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);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|