Browse Source

新增批次操作

jobob 8 years ago
parent
commit
352a080a0d

+ 21 - 0
mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java

@@ -22,6 +22,8 @@ import java.io.Serializable;
 import java.util.List;
 import java.util.Map;
 
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+
 /**
  * <p>
  * 顶级 Service
@@ -65,6 +67,25 @@ public interface IService<T, PK> {
 	 */
 	boolean insertBatch(List<T> entityList);
 
+	/**
+	 * <p>
+	 * 插入(批量)( batchSize 批次提交)
+	 * </p>
+	 * 
+	 * @param txManager
+	 *            数据源事务管理器
+	 * @param entityList
+	 *            实体对象列表
+	 * @param batchSize
+	 *            批次数量
+	 * @return boolean 
+	 * <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" >
+	 *     <constructor-arg index="0" ref="sqlSessionFactory"/>
+	 *     <constructor-arg index="1" value="BATCH"/>
+	 * </bean>
+	 */
+	boolean insertBatch(DataSourceTransactionManager txManager, List<T> entityList, int batchSize);
+
 	/**
 	 * <p>
 	 * 插入(批量)(选择字段, null 字段不插入)

+ 30 - 14
mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java

@@ -21,6 +21,11 @@ import java.util.Map;
 import java.util.logging.Logger;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.transaction.TransactionException;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
 
 import com.baomidou.framework.service.IService;
 import com.baomidou.mybatisplus.annotations.IdType;
@@ -115,28 +120,39 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 
 	public boolean insertBatch(List<T> entityList) {
 		if (null == entityList) {
-			return false;
+			throw new IllegalArgumentException("entityList must not be empty");
 		}
-		int total = entityList.size();
-		if (total <= 10) {
-			/*
-			 * 设置 10 条内批量,超过循环提交,防止 SQL 超长。
-			 */
-			return retBool(baseMapper.insertBatch(entityList));
+		return retBool(baseMapper.insertBatch(entityList));
+	}
+
+	public boolean insertBatch(DataSourceTransactionManager txManager, List<T> entityList, int batchSize) {
+		if (null == entityList) {
+			throw new IllegalArgumentException("entityList must not be empty");
 		}
-		int result = 0;
-		for (T t : entityList) {
-			result = baseMapper.insert(t);
-			if (result <= 0) {
-				break;
+		TransactionDefinition def = new DefaultTransactionDefinition();
+		TransactionStatus status = txManager.getTransaction(def);
+		try {
+			int counter = 0;
+			for (T t : entityList) {
+				counter++;
+				baseMapper.insert(t);
+				if (counter % batchSize == 0) {
+					txManager.commit(status);
+					status = txManager.getTransaction(def);
+				}
 			}
+			txManager.commit(status);
+		} catch (TransactionException e) {
+			txManager.rollback(status);
+			e.printStackTrace();
+			return false;
 		}
-		return retBool(result);
+		return true;
 	}
 
 	public boolean insertBatchSelective(List<T> entityList) {
 		if (null == entityList) {
-			return false;
+			throw new IllegalArgumentException("entityList must not be empty");
 		}
 		int result = 0;
 		for (T t : entityList) {