浏览代码

优化异常抛出

miemie 6 年之前
父节点
当前提交
69aa0266f2

+ 11 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Assert.java

@@ -75,6 +75,17 @@ public final class Assert {
         isTrue(object != null, message, params);
     }
 
+    /**
+     * 断言这个 value 不为 empty
+     * 为 empty 则抛异常
+     *
+     * @param value   字符串
+     * @param message 消息
+     */
+    public static void notEmpty(String value, String message, Object... params) {
+        isTrue(StringUtils.isNotEmpty(value), message, params);
+    }
+
     /**
      * 断言这个 collection 不为 empty
      * 为 empty 则抛异常

+ 29 - 37
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -103,16 +103,16 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     /**
      * 批量插入
      *
-     * @param entityList
-     * @param batchSize
-     * @return
+     * @param entityList 实体类集合
+     * @param batchSize  每次提交的量
+     * @return 执行是否成功
      */
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean saveBatch(Collection<T> entityList, int batchSize) {
-        int i = 0;
         String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
+            int i = 0;
             for (T anEntityList : entityList) {
                 batchSqlSession.insert(sqlStatement, anEntityList);
                 if (i >= 1 && i % batchSize == 0) {
@@ -139,12 +139,11 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         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());
-                return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
-            } else {
-                throw ExceptionUtils.mpe("Error:  Can not execute. Could not find @TableId.");
-            }
+            Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
+            String keyProperty = tableInfo.getKeyProperty();
+            Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
+            Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
+            return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
         }
         return false;
     }
@@ -152,31 +151,28 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
-        if (CollectionUtils.isEmpty(entityList)) {
-            throw new IllegalArgumentException("Error: entityList must not be empty");
-        }
+        Assert.notEmpty(entityList, "error: entityList must not be empty");
         Class<?> cls = currentModelClass();
         TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
-        int i = 0;
+        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
+        String keyProperty = tableInfo.getKeyProperty();
+        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
-            for (T anEntityList : entityList) {
-                if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
-                    Object idVal = ReflectionKit.getMethodValue(cls, anEntityList, tableInfo.getKeyProperty());
-                    if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
-                        batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), anEntityList);
-                    } else {
-                        MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
-                        param.put(Constants.ENTITY, anEntityList);
-                        batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
-                    }
-                    //不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
-                    if (i >= 1 && i % batchSize == 0) {
-                        batchSqlSession.flushStatements();
-                    }
-                    i++;
+            int i = 0;
+            for (T entity : entityList) {
+                Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty);
+                if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
+                    batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity);
                 } else {
-                    throw ExceptionUtils.mpe("Error:  Can not execute. Could not find @TableId.");
+                    MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
+                    param.put(Constants.ENTITY, entity);
+                    batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
+                }
+                //不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
+                if (i >= 1 && i % batchSize == 0) {
+                    batchSqlSession.flushStatements();
                 }
+                i++;
             }
             batchSqlSession.flushStatements();
         }
@@ -192,9 +188,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean removeByMap(Map<String, Object> columnMap) {
-        if (ObjectUtils.isEmpty(columnMap)) {
-            throw ExceptionUtils.mpe("removeByMap columnMap is empty.");
-        }
+        Assert.notEmpty(columnMap, "error: columnMap must not be empty");
         return SqlHelper.delBool(baseMapper.deleteByMap(columnMap));
     }
 
@@ -225,12 +219,10 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean updateBatchById(Collection<T> entityList, int batchSize) {
-        if (CollectionUtils.isEmpty(entityList)) {
-            throw new IllegalArgumentException("Error: entityList must not be empty");
-        }
-        int i = 0;
+        Assert.notEmpty(entityList, "error: entityList must not be empty");
         String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
+            int i = 0;
             for (T anEntityList : entityList) {
                 MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
                 param.put(Constants.ENTITY, anEntityList);