diff --git a/src/main/java/com/glxp/api/dao/BaseMapperPlus.java b/src/main/java/com/glxp/api/dao/BaseMapperPlus.java index 59eb52bd1..3d3a64bce 100644 --- a/src/main/java/com/glxp/api/dao/BaseMapperPlus.java +++ b/src/main/java/com/glxp/api/dao/BaseMapperPlus.java @@ -18,10 +18,7 @@ import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; import java.io.Serializable; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; /** * 自定义 Mapper 接口, 实现 自定义扩展 @@ -242,8 +239,39 @@ public interface BaseMapperPlus extends BaseMapper { * @param entityList 实体类列表 * @return 影响条数 */ + default boolean insertIgnoreBatchs(List entityList) { + return insertIgnoreBatchs(entityList, DEFAULT_BATCH_SIZE); + } + + default boolean insertIgnoreBatchs(List entityList, int batchSize) { + + try { + int size = entityList.size(); + int idxLimit = Math.min(DEFAULT_BATCH_SIZE, size); + int i = 1; + //保存单批提交的数据集合 + List oneBatchList = new ArrayList<>(); + for (Iterator var7 = entityList.iterator(); var7.hasNext(); ++i) { + T element = var7.next(); + oneBatchList.add(element); + if (i == idxLimit) { + + this.insertIgnoreBatch(oneBatchList); + //每次提交后需要清空集合数据 + oneBatchList.clear(); + idxLimit = Math.min(idxLimit + batchSize, size); + } + } + } catch (Exception e) { + log.error("insertIgnoreBatch fail", e); + return false; + } + return true; + } + int insertIgnoreBatch(List entityList); + /** * 替换数据 * replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样; @@ -262,5 +290,36 @@ public interface BaseMapperPlus extends BaseMapper { * @param entityList 实体类列表 * @return 影响条数 */ + default boolean replaceBatchs(List entityList) { + return this.replaceBatchs(entityList, DEFAULT_BATCH_SIZE); + } + + default boolean replaceBatchs(List entityList, int batchSize) { + + try { + int size = entityList.size(); + int idxLimit = Math.min(DEFAULT_BATCH_SIZE, size); + int i = 1; + //保存单批提交的数据集合 + List oneBatchList = new ArrayList<>(); + for (Iterator var7 = entityList.iterator(); var7.hasNext(); ++i) { + T element = var7.next(); + oneBatchList.add(element); + if (i == idxLimit) { + + this.replaceBatch(oneBatchList); + //每次提交后需要清空集合数据 + oneBatchList.clear(); + idxLimit = Math.min(idxLimit + batchSize, size); + } + } + } catch (Exception e) { + log.error("insertIgnoreBatch fail", e); + return false; + } + return true; + } + int replaceBatch(List entityList); + } diff --git a/src/main/java/com/glxp/api/service/CustomService.java b/src/main/java/com/glxp/api/service/CustomService.java index 6f5771cab..f7e0cfe2f 100644 --- a/src/main/java/com/glxp/api/service/CustomService.java +++ b/src/main/java/com/glxp/api/service/CustomService.java @@ -6,7 +6,7 @@ import java.util.List; public interface CustomService extends IService { int insertIgnore(T entity); - int insertIgnoreBatch(List entityList); + boolean insertIgnoreBatch(List entityList); int replace(T entity); - int replaceBatch(List entityList); + boolean replaceBatch(List entityList); } diff --git a/src/main/java/com/glxp/api/service/CustomServiceImpl.java b/src/main/java/com/glxp/api/service/CustomServiceImpl.java index fbe971939..e180c9058 100644 --- a/src/main/java/com/glxp/api/service/CustomServiceImpl.java +++ b/src/main/java/com/glxp/api/service/CustomServiceImpl.java @@ -13,8 +13,8 @@ public class CustomServiceImpl, T> extends ServiceImpl entityList) { - return baseMapper.insertIgnoreBatch(entityList); + public boolean insertIgnoreBatch(List entityList) { + return baseMapper.insertIgnoreBatchs(entityList); } @Override @@ -23,7 +23,7 @@ public class CustomServiceImpl, T> extends ServiceImpl entityList) { - return baseMapper.replaceBatch(entityList); + public boolean replaceBatch(List entityList) { + return baseMapper.replaceBatchs(entityList); } }