Przeglądaj źródła

批量新增修改

Caratacus 8 lat temu
rodzic
commit
8ce8c3ff4b

+ 28 - 4
mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java

@@ -15,13 +15,13 @@
  */
  */
 package com.baomidou.framework.service;
 package com.baomidou.framework.service;
 
 
+import com.baomidou.mybatisplus.mapper.Wrapper;
+import com.baomidou.mybatisplus.plugins.Page;
+
 import java.io.Serializable;
 import java.io.Serializable;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
-import com.baomidou.mybatisplus.mapper.Wrapper;
-import com.baomidou.mybatisplus.plugins.Page;
-
 /**
 /**
  * <p>
  * <p>
  * 顶级 Service
  * 顶级 Service
@@ -61,12 +61,36 @@ public interface IService<T> {
 	 *
 	 *
 	 * @param entityList
 	 * @param entityList
 	 *            实体对象列表
 	 *            实体对象列表
-	 * @param entityList
+	 * @param batchSize
 	 *
 	 *
 	 * @return boolean
 	 * @return boolean
 	 */
 	 */
 	boolean insertBatch(List<T> entityList, int batchSize);
 	boolean insertBatch(List<T> entityList, int batchSize);
 
 
+	/**
+	 * <p>
+	 * 批量修改插入
+	 * </p>
+	 *
+	 * @param entityList
+	 *            实体对象列表
+	 * @return boolean
+	 */
+	boolean insertOrUpdateBatch(List<T> entityList);
+
+	/**
+	 * <p>
+	 * 批量修改插入
+	 * </p>
+	 *
+	 * @param entityList
+	 *            实体对象列表
+	 * @param batchSize
+	 *
+	 * @return boolean
+	 */
+	boolean insertOrUpdateBatch(List<T> entityList, int batchSize);
+
 	/**
 	/**
 	 * <p>
 	 * <p>
 	 * 根据 ID 删除
 	 * 根据 ID 删除

+ 24 - 2
mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java

@@ -67,7 +67,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 	 * <p>
 	 * <p>
 	 * SQL 构建方法
 	 * SQL 构建方法
 	 * </p>
 	 * </p>
-	 * 
+	 *
 	 * @param sql
 	 * @param sql
 	 *            SQL 语句
 	 *            SQL 语句
 	 * @param args
 	 * @param args
@@ -136,10 +136,32 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 	}
 	}
 
 
 	public boolean insertBatch(List<T> entityList) {
 	public boolean insertBatch(List<T> entityList) {
+		return insertBatch(entityList, 30);
+	}
+
+	public boolean insertOrUpdateBatch(List<T> entityList) {
+		return insertOrUpdateBatch(entityList, 30);
+	}
+
+	public boolean insertOrUpdateBatch(List<T> entityList, int batchSize) {
 		if (CollectionUtil.isEmpty(entityList)) {
 		if (CollectionUtil.isEmpty(entityList)) {
 			throw new IllegalArgumentException("Error: entityList must not be empty");
 			throw new IllegalArgumentException("Error: entityList must not be empty");
 		}
 		}
-		return insertBatch(entityList, 30);
+		try {
+			SqlSession batchSqlSession = sqlSessionBatch();
+			int size = entityList.size();
+			for (int i = 0; i < size; i++) {
+				insertOrUpdate(entityList.get(i));
+				if (i % batchSize == 0) {
+					batchSqlSession.flushStatements();
+				}
+			}
+			batchSqlSession.flushStatements();
+		} catch (Exception e) {
+			logger.warn("Error: Cannot execute insertOrUpdateBatch Method. Cause:" + e);
+			return false;
+		}
+		return true;
 	}
 	}
 
 
 	/**
 	/**