Browse Source

IdGenerator传递实体,过时IdWorker,StringUtils部分方法.

nieqiuqiu 5 years ago
parent
commit
25d715bdf1

+ 4 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+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.*;
@@ -174,13 +175,14 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
         // 填充主键
         if (isInsert && !StringUtils.isEmpty(tableInfo.getKeyProperty())
             && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 3) {
+            IdGenerator idGenerator = GlobalConfigUtils.getGlobalConfig(tableInfo.getConfiguration()).getIdGenerator();
             Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
             /* 自定义 ID */
             if (StringUtils.checkValNull(idValue)) {
                 if (tableInfo.getIdType() == IdType.ID_WORKER) {
-                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
+                    metaObject.setValue(tableInfo.getKeyProperty(), idGenerator.nextId(parameterObject));
                 } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
-                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
+                    metaObject.setValue(tableInfo.getKeyProperty(), String.valueOf(idGenerator.nextId(parameterObject)));
                 } else if (tableInfo.getIdType() == IdType.UUID) {
                     metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                 }

+ 9 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisSqlSessionFactoryBuilder.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
+import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
 import com.baomidou.mybatisplus.core.injector.SqlRunnerInjector;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import org.apache.ibatis.exceptions.ExceptionFactory;
@@ -80,13 +81,17 @@ public class MybatisSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
     public SqlSessionFactory build(Configuration config) {
         MybatisConfiguration configuration = (MybatisConfiguration) config;
         GlobalConfig globalConfig = configuration.getGlobalConfig();
-        if (globalConfig.getIdGenerator() != null) {
-            IdWorker.setIdGenerator(globalConfig.getIdGenerator());
-        } else {
+        IdGenerator idGenerator = globalConfig.getIdGenerator();
+        if (globalConfig.getIdGenerator() == null) {
             if (null != globalConfig.getWorkerId() && null != globalConfig.getDatacenterId()) {
-                IdWorker.setIdGenerator(new DefaultIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId()));
+                idGenerator = new DefaultIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
+            } else {
+                idGenerator = new DefaultIdGenerator();
             }
+            globalConfig.setIdGenerator(idGenerator);
         }
+        //TODO 这里只是为了兼容下,并没多大重要,方法标记过时了.
+        IdWorker.setIdGenerator(idGenerator);
         if (globalConfig.isEnableSqlRunner()) {
             new SqlRunnerInjector().inject(configuration);
         }

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultIdGenerator.java

@@ -36,7 +36,7 @@ public class DefaultIdGenerator implements IdGenerator {
     }
 
     @Override
-    public long nextId() {
+    public long nextId(Object entity) {
         return sequence.nextId();
     }
 }

+ 2 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/IdGenerator.java

@@ -26,7 +26,8 @@ public interface IdGenerator {
     /**
      * 生成Id
      *
+     * @param entity 实体
      * @return id
      */
-    long nextId();
+    long nextId(Object entity);
 }

+ 8 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/IdWorker.java

@@ -35,6 +35,7 @@ public class IdWorker {
     /**
      * 主机和进程的机器码
      */
+    @Deprecated
     private static IdGenerator ID_GENERATOR = new DefaultIdGenerator();
 
     /**
@@ -42,12 +43,14 @@ public class IdWorker {
      */
     public static final DateTimeFormatter MILLISECOND = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
 
+    @Deprecated
     public static long getId() {
-        return ID_GENERATOR.nextId();
+        return ID_GENERATOR.nextId(new Object());
     }
 
+    @Deprecated
     public static String getIdStr() {
-        return String.valueOf(ID_GENERATOR.nextId());
+        return String.valueOf(ID_GENERATOR.nextId(new Object()));
     }
 
     /**
@@ -60,7 +63,9 @@ public class IdWorker {
     /**
      * 时间 ID = Time + ID
      * <p>例如:可用于商品订单 ID</p>
+     * @deprecated 3.1.2  spring应用可以通过@IdGenerator,非spring应用请自行控制IdGenerator的实例化
      */
+    @Deprecated
     public static String getTimeId() {
         return getMillisecond() + getId();
     }
@@ -83,6 +88,7 @@ public class IdWorker {
      *
      * @param idGenerator id 生成器
      */
+    @Deprecated
     public static void setIdGenerator(IdGenerator idGenerator) {
         ID_GENERATOR = idGenerator;
     }

+ 22 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java

@@ -17,7 +17,6 @@ package com.baomidou.mybatisplus.core.toolkit;
 
 import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
 import com.baomidou.mybatisplus.core.toolkit.support.BiIntFunction;
-import jdk.internal.vm.annotation.ForceInline;
 
 import java.util.Collection;
 import java.util.regex.Matcher;
@@ -70,7 +69,6 @@ public final class StringUtils {
      * @param params format 参数
      * @return format 后的
      */
-    @ForceInline
     public static String format(String target, Object... params) {
         return String.format(target, params);
     }
@@ -81,8 +79,24 @@ public final class StringUtils {
      * @param cs 需要判断字符串
      * @return 判断结果
      */
+    @Deprecated
     public static boolean isEmpty(CharSequence cs) {
-        return null == cs || 0 == cs.length();
+        return isBlank(cs);
+    }
+
+    public static boolean isBlank(final CharSequence cs) {
+        if (cs == null) {
+            return true;
+        }
+        int l = cs.length();
+        if (l > 0) {
+            for (int i = 0; i < l; i++) {
+                if (!Character.isWhitespace(cs.charAt(i))) {
+                    return false;
+                }
+            }
+        }
+        return true;
     }
 
     /**
@@ -91,11 +105,15 @@ public final class StringUtils {
      * @param cs 需要判断字符串
      * @return 判断结果
      */
-    @ForceInline
+    @Deprecated
     public static boolean isNotEmpty(final CharSequence cs) {
         return !isEmpty(cs);
     }
 
+    public static boolean isNotBlank(final CharSequence cs) {
+        return !isBlank(cs);
+    }
+
     /**
      * 猜测方法属性对应的 Getter 名称,具体规则请参考 JavaBeans 规范
      *