فهرست منبع

修改记录全部字段的方法有缺少,我这里补充了一下:
1.insertOrUpdateAllColumn
2.insertOrUpdateAllColumnBatch(2个方法)
3.updateAllColumnBatchById(2个方法)

bj_renyong 8 سال پیش
والد
کامیت
1e00211f35

+ 45 - 0
src/main/java/com/baomidou/mybatisplus/service/IService.java

@@ -94,6 +94,23 @@ public interface IService<T> {
      */
     boolean insertOrUpdateBatch(List<T> entityList, int batchSize);
 
+    /**
+     * <p>
+     * 批量修改或插入全部字段
+     * </p>
+     * @param entityList 实体对象列表
+     * @return boolean
+     */
+    boolean insertOrUpdateAllColumnBatch(List<T> entityList);
+
+    /**
+     * 批量修改或插入全部字段
+     * @param entityList 实体对象列表
+     * @param batchSize
+     * @return boolean
+     */
+    boolean insertOrUpdateAllColumnBatch(List<T> entityList, int batchSize);
+
     /**
      * <p>
      * 根据 ID 删除
@@ -186,6 +203,27 @@ public interface IService<T> {
      */
     boolean updateBatchById(List<T> entityList, int batchSize);
 
+    /**
+     * <p>
+     * 根据ID 批量更新全部字段
+     * </p>
+     *
+     * @param entityList 实体对象列表
+     * @return boolean
+     */
+    boolean updateAllColumnBatchById(List<T> entityList);
+
+    /**
+     * <p>
+     * 根据ID 批量更新全部字段
+     * </p>
+     *
+     * @param entityList 实体对象列表
+     * @param batchSize  更新批次数量
+     * @return boolean
+     */
+    boolean updateAllColumnBatchById(List<T> entityList, int batchSize);
+
     /**
      * <p>
      * TableId 注解存在更新记录,否插入一条记录
@@ -196,6 +234,13 @@ public interface IService<T> {
      */
     boolean insertOrUpdate(T entity);
 
+    /**
+     * 插入或修改一条记录的全部字段
+     * @param entity 实体对象
+     * @return boolean
+     */
+    boolean insertOrUpdateAllColumn(T entity);
+
     /**
      * <p>
      * 根据 ID 查询

+ 71 - 3
src/main/java/com/baomidou/mybatisplus/service/impl/ServiceImpl.java

@@ -136,7 +136,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
             throw new MybatisPlusException("Error: Cannot execute insertBatch Method. Cause", e);
         }
         return true;
-
     }
 
     /**
@@ -169,6 +168,28 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         return false;
     }
 
+    @Transactional
+    public boolean insertOrUpdateAllColumn(T entity) {
+        if (null != entity) {
+            Class<?> cls = entity.getClass();
+            TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
+            if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
+                Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
+                if (StringUtils.checkValNull(idVal)) {
+                    return insertAllColumn(entity);
+                } else {
+                    /*
+                     * 更新成功直接返回,失败执行插入逻辑
+					 */
+                    return updateAllColumnById(entity) || insertAllColumn(entity);
+                }
+            } else {
+                throw new MybatisPlusException("Error:  Can not execute. Could not find @TableId.");
+            }
+        }
+        return false;
+    }
+
     @Transactional
     public boolean insertOrUpdateBatch(List<T> entityList) {
         return insertOrUpdateBatch(entityList, 30);
@@ -176,13 +197,38 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
     @Transactional
     public boolean insertOrUpdateBatch(List<T> entityList, int batchSize) {
+        return insertOrUpdateBatch(entityList, batchSize,true);
+    }
+
+    @Transactional
+    public boolean insertOrUpdateAllColumnBatch(List<T> entityList){
+        return insertOrUpdateBatch(entityList, 30,false);
+    }
+
+    @Transactional
+    public boolean insertOrUpdateAllColumnBatch(List<T> entityList, int batchSize){
+        return insertOrUpdateBatch(entityList, batchSize,false);
+    }
+
+    /**
+     * 批量插入修改
+     * @param entityList 实体对象列表
+     * @param batchSize 批量刷新个数
+     * @param selective 是否滤掉空字段
+     * @return boolean
+     */
+    private boolean insertOrUpdateBatch(List<T> entityList, int batchSize, boolean selective){
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int size = entityList.size();
             for (int i = 0; i < size; i++) {
-                insertOrUpdate(entityList.get(i));
+                if(selective) {
+                    insertOrUpdate(entityList.get(i));
+                }else {
+                    insertOrUpdateAllColumn(entityList.get(i));
+                }
                 if (i >= 1 && i % batchSize == 0) {
                     batchSqlSession.flushStatements();
                 }
@@ -239,12 +285,34 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
 
     @Transactional
     public boolean updateBatchById(List<T> entityList, int batchSize) {
+        return updateBatchById(entityList, batchSize, true);
+    }
+
+    @Transactional
+    public boolean updateAllColumnBatchById(List<T> entityList){
+        return updateAllColumnBatchById(entityList, 30);
+    }
+
+    @Transactional
+    public boolean updateAllColumnBatchById(List<T> entityList, int batchSize){
+        return updateBatchById(entityList, batchSize, false);
+    }
+
+    /**
+     * 根据主键ID进行批量修改
+     * @param entityList 实体对象列表
+     * @param batchSize 批量刷新个数
+     * @param selective 是否滤掉空字段
+     * @return boolean
+     */
+    private boolean updateBatchById(List<T> entityList, int batchSize, boolean selective) {
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int size = entityList.size();
-            String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
+            SqlMethod sqlMethod = selective ? SqlMethod.UPDATE_BY_ID : SqlMethod.UPDATE_ALL_COLUMN_BY_ID;
+            String sqlStatement = sqlStatement(sqlMethod);
             for (int i = 0; i < size; i++) {
                 MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
                 param.put("et", entityList.get(i));