瀏覽代碼

新增批量insert

周浩 9 年之前
父節點
當前提交
1e52c23b08

+ 1 - 1
hsweb-web-dao-interface/src/main/java/org/hsweb/web/dao/GenericMapper.java

@@ -20,7 +20,7 @@ public interface GenericMapper<Po, Pk> {
      * @return 添加后生成的主键
      * @throws Exception 异常信息
      */
-    void insert(InsertParam<Po> param) throws Exception;
+    int insert(InsertParam<Po> param) throws Exception;
 
     /**
      * 根据条件删除数据

+ 32 - 6
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/AbstractServiceImpl.java

@@ -5,7 +5,6 @@ import org.hsweb.web.bean.po.GenericPo;
 import org.hsweb.web.bean.valid.ValidResults;
 import org.hsweb.web.core.exception.NotFoundException;
 import org.hsweb.web.core.exception.ValidationException;
-import org.hsweb.web.core.utils.RandomUtil;
 import org.hsweb.web.dao.GenericMapper;
 import org.hsweb.web.service.GenericService;
 import org.slf4j.Logger;
@@ -15,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.Validator;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
@@ -52,25 +52,51 @@ public abstract class AbstractServiceImpl<Po, PK> implements GenericService<Po,
             primaryKey = (PK) ((GenericPo) data).getId();
         }
         tryValidPo(data);
-        getMapper().insert(new InsertParam<>(data));
+        getMapper().insert(InsertParam.build(data));
         return primaryKey;
     }
 
+    public List<PK> batchInsert(List<Po> data, boolean skipFail) throws Exception {
+        List<PK> pkList = new ArrayList<>();
+        List<Po> insertData = new ArrayList<>();
+        //build
+        for (Po po : data) {
+            if (data instanceof GenericPo) {
+                if (((GenericPo) data).getId() == null)
+                    ((GenericPo) data).setId(GenericPo.createUID());
+                PK primaryKey = (PK) ((GenericPo) data).getId();
+                try {
+                    tryValidPo(po);
+                    insertData.add(po);
+                    pkList.add(primaryKey);
+                } catch (ValidationException e) {
+                    if (!skipFail) throw e;
+                    else if (logger.isWarnEnabled()) {
+                        logger.warn("data validate fail:{}", e);
+                    }
+                }
+
+            }
+        }
+        getMapper().insert((InsertParam) InsertParam.build(insertData));
+        return pkList;
+    }
+
     @Override
     public int delete(PK pk) throws Exception {
-        return getMapper().delete(new DeleteParam().where("primaryKey", pk));
+        return getMapper().delete(DeleteParam.build().where("id", pk));
     }
 
     @Override
     public int update(Po data) throws Exception {
-        return getMapper().update(new UpdateParam<>(data));
+        return getMapper().update(UpdateParam.build(data));
     }
 
     @Override
     public int update(List<Po> data) throws Exception {
         int i = 0;
         for (Po po : data) {
-            i += getMapper().update(new UpdateParam<>(po));
+            i += getMapper().update(UpdateParam.build(po));
         }
         return i;
     }
@@ -83,7 +109,7 @@ public abstract class AbstractServiceImpl<Po, PK> implements GenericService<Po,
 
     @Transactional(readOnly = true)
     public List<Po> select() throws Exception {
-        return this.getMapper().select(new QueryParam().noPaging());
+        return this.getMapper().select(QueryParam.build().noPaging());
     }
 
     @Override

+ 5 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/user/UserServiceImpl.java

@@ -69,6 +69,11 @@ public class UserServiceImpl extends AbstractServiceImpl<User, String> implement
         return id;
     }
 
+    @Override
+    public List<String> batchInsert(List<User> data, boolean skipFail) throws Exception {
+        throw new UnsupportedOperationException("不支持此操作");
+    }
+
     @Override
     public int update(User data) throws Exception {
         tryValidPo(data);

+ 15 - 0
hsweb-web-service-interface/src/main/java/org/hsweb/web/service/GenericService.java

@@ -31,6 +31,20 @@ public interface GenericService<Po, Pk> {
      */
     Pk insert(Po data) throws Exception;
 
+    default List<Pk> batchInsert(List<Po> data) throws Exception {
+        return batchInsert(data, false);
+    }
+
+    /**
+     * 批量添加数据
+     *
+     * @param data     数据集合
+     * @param skipFail 是否跳过验证失败的的数据
+     * @return 添加后产生的主键集合
+     * @throws Exception 异常信息
+     */
+    List<Pk> batchInsert(List<Po> data, boolean skipFail) throws Exception;
+
     /**
      * 根据主键删除记录
      *
@@ -98,6 +112,7 @@ public interface GenericService<Po, Pk> {
 
     /**
      * 查询只返回单个结果
+     *
      * @param param 查询条件
      * @return 单个结果
      * @throws Exception