nieqiuqiu 5 rokov pred
rodič
commit
f7a7bf53cc

+ 12 - 13
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java

@@ -16,7 +16,6 @@
 package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
@@ -160,19 +159,19 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
      */
     protected static void populateKeys(TableInfo tableInfo, MetaObject metaObject, Object parameterObject) {
         // 填充主键
-        if (!StringUtils.isBlank(tableInfo.getKeyProperty())
-            && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {
-            IdGenerator idGenerator = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getIdGenerator(tableInfo.getIdType());
-            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
-            /* 自定义 ID */
-            if (StringUtils.checkValNull(idValue)) {
-                // 应该只有数值型和字符串的区别了.
-                if (Number.class.isAssignableFrom(tableInfo.getKeyType())) {
-                    metaObject.setValue(tableInfo.getKeyProperty(), idGenerator.generate(parameterObject));
-                } else {
-                    metaObject.setValue(tableInfo.getKeyProperty(), idGenerator.generate(parameterObject).toString());
+        if (StringUtils.isNotBlank(tableInfo.getKeyProperty()) && null != tableInfo.getIdType()) {
+            GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getIdGenerator(tableInfo.getIdType()).ifPresent(idGenerator -> {
+                Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
+                /* 自定义 ID */
+                if (StringUtils.checkValNull(idValue)) {
+                    // 应该只有数值型和字符串的区别了.
+                    if (Number.class.isAssignableFrom(tableInfo.getKeyType())) {
+                        metaObject.setValue(tableInfo.getKeyProperty(), idGenerator.generate(parameterObject));
+                    } else {
+                        metaObject.setValue(tableInfo.getKeyProperty(), idGenerator.generate(parameterObject).toString());
+                    }
                 }
-            }
+            });
         }
     }
 

+ 3 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/config/GlobalConfig.java

@@ -35,6 +35,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import java.io.Serializable;
 import java.util.Collections;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentSkipListSet;
@@ -127,8 +128,8 @@ public class GlobalConfig implements Serializable {
         return this;
     }
 
-    public IdGenerator getIdGenerator(IdType idType) {
-        return Collections.unmodifiableMap(idGeneratorMap).get(String.valueOf(idType.getKey()));
+    public Optional<IdGenerator> getIdGenerator(IdType idType) {
+        return Optional.ofNullable(Collections.unmodifiableMap(idGeneratorMap).get(String.valueOf(idType.getKey())));
     }
 
     /**