Kaynağa Gözat

简单批量插入 通用接口

shiqian 4 yıl önce
ebeveyn
işleme
1786b95517

+ 22 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/dto/InsertListDto.java

@@ -0,0 +1,22 @@
+package com.boman.domain.dto;
+
+import com.alibaba.fastjson.JSONObject;
+import lombok.Data;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * @author shiqian
+ * @date 2021年06月30日 16:42
+ **/
+@Data
+public class InsertListDto {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(InsertListDto.class);
+
+    private String tableName;
+
+    private List<JSONObject> dataList;
+}

+ 13 - 0
boman-web-core/src/main/java/com/boman/web/core/controller/CommonController.java

@@ -3,6 +3,7 @@ package com.boman.web.core.controller;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.domain.dto.AjaxResult;
 import com.boman.domain.dto.FormDataDto;
+import com.boman.domain.dto.InsertListDto;
 import com.boman.web.core.service.common.ICommonService;
 import org.springframework.web.bind.annotation.*;
 
@@ -87,4 +88,16 @@ public class CommonController {
         return commonService.getNewest(tableName);
     }
 
+
+    /**
+     * 功能描述: 批量插入数据
+     *
+     * @param dto dto
+     * @return int
+     */
+    @PostMapping("insertList")
+    public int insertList(@RequestBody InsertListDto dto) {
+        return commonService.insertList(dto);
+    }
+
 }

+ 38 - 0
boman-web-core/src/main/java/com/boman/web/core/mapper/StandardlyMapper.java

@@ -1,6 +1,7 @@
 package com.boman.web.core.mapper;
 
 import com.alibaba.fastjson.JSONObject;
+import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.web.core.utils.ColumnUtils;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.StringUtils;
@@ -62,6 +63,10 @@ public interface StandardlyMapper {
     )
     int insert(@Param("tableName") String var1, @Param("model") JSONObject var2);
 
+    /** {@link SqlProvider#insertList(Map)} */
+    @InsertProvider(type = SqlProvider.class,method = "insertList")
+    int insertList(@Param("tableName") String tableName, @Param("dataList") List<JSONObject> dataList);
+
     @InsertProvider(
             type = SqlProvider.class,
             method = "inserts"
@@ -341,6 +346,39 @@ public interface StandardlyMapper {
             }
         }
 
+        public String insertList(Map<String, Object> para) {
+            List<JSONObject> dataList = (List<JSONObject>) para.get("dataList");
+            String tableName = (String) para.get("tableName");
+            if (isNullOrEmpty(tableName)) {
+                throw new IllegalArgumentException("tableName 无效");
+            }
+
+            if (ObjectUtils.isEmpty(dataList)) {
+                throw new IllegalArgumentException("需要插入的数据为空");
+            }
+
+            StringBuilder stringBuilder = new StringBuilder();
+            stringBuilder.append("insert into ").append(tableName).append(" ( ");
+            JSONObject keySet = dataList.get(0);
+            stringBuilder.append(String.join(", ", keySet.keySet()));
+            stringBuilder.append(" ) ").append("values ");
+
+            for (JSONObject data : dataList) {
+                StringBuilder values = new StringBuilder();
+                values.append(" ( ");
+                for (Map.Entry<String, Object> entry : data.entrySet()) {
+                    values.append("#{model.").append(entry.getKey()).append("}, ");
+                }
+
+                String beforeLast = StringUtils.substringBeforeLast(values.toString(), ", ");
+                stringBuilder.append(beforeLast).append(" ) ");
+            }
+
+            String sqlStr = stringBuilder.toString();
+            LOGGER.info("批量新增的sql语句为: {}, \r\n 新增的数据为: {}", sqlStr, dataList);
+            return sqlStr;
+        }
+
 
         public String updateByIdList(Map<String, Object> para) {
             String tableName = (String) para.get("tableName");

+ 18 - 0
boman-web-core/src/main/java/com/boman/web/core/service/common/CommonServiceImpl.java

@@ -5,7 +5,9 @@ import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
 import com.boman.domain.GenTable;
 import com.boman.domain.dto.FormDataDto;
+import com.boman.domain.dto.InsertListDto;
 import com.boman.web.core.service.TableServiceCmdService;
+import com.boman.web.core.service.save.IBaseSaveService;
 import com.boman.web.core.service.select.IBaseSelectService;
 import com.boman.web.core.utils.IdUtils;
 import org.springframework.stereotype.Service;
@@ -29,6 +31,8 @@ public class CommonServiceImpl implements ICommonService {
     private RedisService redisService;
     @Resource
     private TableServiceCmdService cmdService;
+    @Resource
+    private IBaseSaveService saveService;
 
     /**
      * 功能描述: 根据表名和id查找
@@ -105,4 +109,18 @@ public class CommonServiceImpl implements ICommonService {
         requireNonNull(tableName, "tableName is empty");
         return selectService.getNewest(tableName);
     }
+
+    /**
+     * 功能描述: 批量插入数据 默认插入的是多对多的关联表,因此未处理create_by等字段
+     *
+     * @param dto dto
+     * @return int
+     */
+    @Override
+    public int insertList(InsertListDto dto) {
+        String tableName = requireNonNull(dto.getTableName(), "tableName is empty");
+        List<JSONObject> dataList = requireNonNull(dto.getDataList(), "dataList is empty");
+
+        return saveService.insertList(tableName, dataList);
+    }
 }

+ 9 - 0
boman-web-core/src/main/java/com/boman/web/core/service/common/ICommonService.java

@@ -2,6 +2,7 @@ package com.boman.web.core.service.common;
 
 import com.alibaba.fastjson.JSONObject;
 import com.boman.domain.dto.FormDataDto;
+import com.boman.domain.dto.InsertListDto;
 
 import java.util.List;
 
@@ -56,4 +57,12 @@ public interface ICommonService {
      * @return com.alibaba.fastjson.JSONObject
      */
     JSONObject getNewest(String tableName);
+
+    /**
+     * 功能描述: 批量插入数据
+     *
+     * @param dto dto
+     * @return int
+     */
+    int insertList(InsertListDto dto);
 }

+ 27 - 1
boman-web-core/src/main/java/com/boman/web/core/service/save/BaseSaveServiceImpl.java

@@ -1,15 +1,25 @@
 package com.boman.web.core.service.save;
 
 import com.alibaba.fastjson.JSONObject;
+import com.boman.common.redis.RedisKey;
+import com.boman.common.redis.service.RedisService;
+import com.boman.domain.GenTable;
+import com.boman.domain.GenTableColumn;
 import com.boman.domain.constant.FormDataConstant;
+import com.boman.domain.constant.TableNameConst;
 import com.boman.web.core.domain.RowResult;
-import com.boman.web.core.domain.TableContext;
 import com.boman.web.core.mapper.StandardlyMapper;
+import com.boman.web.core.utils.IdUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.Resource;
+import java.util.List;
+
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
+
 /**
  * @author shiqian
  * @description
@@ -22,6 +32,8 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
 
     @Autowired
     private StandardlyMapper mapper;
+    @Resource
+    private RedisService redisService;
     /**
      * 功能描述: 保存一行
      *
@@ -41,4 +53,18 @@ public class BaseSaveServiceImpl implements IBaseSaveService {
         return RowResult.error("失败");
     }
 
+    @Override
+    public int insertList(String tableName, List<JSONObject> datList) {
+        requireNonNull(tableName, "insertList tableName is empty");
+        requireNonNull(datList, "insertList datList is empty");
+
+        String pkName = IdUtils.getPkName(tableName);
+
+        for (JSONObject data : datList) {
+            Long maxId = IdUtils.getMaxId(tableName, pkName);
+            data.put(TableNameConst.ID, maxId);
+        }
+
+        return mapper.insertList(tableName, datList);
+    }
 }

+ 4 - 0
boman-web-core/src/main/java/com/boman/web/core/service/save/IBaseSaveService.java

@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject;
 import com.boman.web.core.domain.RowResult;
 import com.boman.web.core.domain.TableContext;
 
+import java.util.List;
+
 /**
  * @author shiqian
  * @description
@@ -22,4 +24,6 @@ public interface IBaseSaveService {
      */
     RowResult insertRow(String tableName, JSONObject commitData);
 
+    int insertList(String tableName, List<JSONObject> datList);
+
 }

+ 11 - 0
boman-web-core/src/main/java/com/boman/web/core/utils/IdUtils.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.boman.common.core.utils.SpringUtils;
 import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
+import com.boman.domain.GenTable;
 import com.boman.domain.GenTableColumn;
 import com.boman.web.core.mapper.StandardlyMapper;
 import org.apache.commons.collections.CollectionUtils;
@@ -57,6 +58,16 @@ public class IdUtils {
         throw new IllegalArgumentException("获取主键失败");
     }
 
+
+    public static String getPkName(String tableName) {
+        requireNonNull(tableName, "getPkName tableName is empty");
+        RedisService redisService = SpringUtils.getBean(RedisService.class);
+        GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName.trim());
+        requireNonNull(genTable, String.format("表名为: %s 的表不存在缓存", tableName));
+        List<GenTableColumn> columnList = genTable.getColumns();
+        return getPkName(columnList);
+    }
+
     public static void putIfNotContains(JSONArray jsonArray, String input) {
         if (isEmpty(jsonArray)) {
             jsonArray = new JSONArray();