瀏覽代碼

SnowflakeIdGenerator重命名为DefaultGenerator

聂秋秋 5 年之前
父節點
當前提交
83bee06d4c

+ 3 - 3
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.java

@@ -19,7 +19,7 @@ package com.baomidou.mybatisplus.autoconfigure;
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
-import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
+import com.baomidou.mybatisplus.core.incrementer.DefaultGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
@@ -257,9 +257,9 @@ public class MybatisPlusAutoConfiguration implements InitializingBean {
     public IdGenerator idGenerator() {
         GlobalConfig globalConfig = this.properties.getGlobalConfig();
         if (globalConfig.getWorkerId() != null && globalConfig.getDatacenterId() != null) {
-            return new SnowflakeIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
+            return new DefaultGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
         }
-        return new SnowflakeIdGenerator();
+        return new DefaultGenerator();
     }
 
     /**

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

@@ -16,7 +16,7 @@
 package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
-import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
+import com.baomidou.mybatisplus.core.incrementer.DefaultGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
 import com.baomidou.mybatisplus.core.injector.SqlRunnerInjector;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
@@ -84,9 +84,9 @@ public class MybatisSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
         IdGenerator idGenerator = globalConfig.getIdGenerator();
         if (globalConfig.getIdGenerator() == null) {
             if (null != globalConfig.getWorkerId() && null != globalConfig.getDatacenterId()) {
-                idGenerator = new SnowflakeIdGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
+                idGenerator = new DefaultGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
             } else {
-                idGenerator = new SnowflakeIdGenerator();
+                idGenerator = new DefaultGenerator();
             }
             globalConfig.setIdGenerator(idGenerator);
         }

+ 5 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/SnowflakeIdGenerator.java → mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/DefaultGenerator.java

@@ -18,24 +18,24 @@ package com.baomidou.mybatisplus.core.incrementer;
 import com.baomidou.mybatisplus.core.toolkit.Sequence;
 
 /**
- * 雪花算法ID生成器
+ * 默认ID生成器
  *
  * @author sd-wangtaicheng@sdcncsi.com.cn
  * @date 2019/10/15
  */
-public class SnowflakeIdGenerator implements IdGenerator {
+public class DefaultGenerator implements IdGenerator {
 
     private final Sequence sequence;
 
-    public SnowflakeIdGenerator() {
+    public DefaultGenerator() {
         this.sequence = new Sequence();
     }
 
-    public SnowflakeIdGenerator(long workerId, long dataCenterId) {
+    public DefaultGenerator(long workerId, long dataCenterId) {
         this.sequence = new Sequence(workerId, dataCenterId);
     }
 
-    public SnowflakeIdGenerator(Sequence sequence) {
+    public DefaultGenerator(Sequence sequence) {
         this.sequence = sequence;
     }
 

+ 6 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/IdGenerator.java

@@ -15,19 +15,20 @@
  */
 package com.baomidou.mybatisplus.core.incrementer;
 
+import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 
 /**
- * Id 生成器接口
- * 可以自定义id生成方式,如 baidu 的UidGenerator,美团的leaf 等
+ * Id生成器接口
  *
  * @author sd-wangtaicheng@sdcncsi.com.cn
  * @date 2019/10/15
+ * @since 3.2.1
  */
 public interface IdGenerator {
 
     /**
-     * 生成Id
+     * 生成Id {@link IdType#ID_WORKER}
      *
      * @param entity 实体
      * @return id
@@ -35,7 +36,7 @@ public interface IdGenerator {
     Number nextId(Object entity);
 
     /**
-     * 生成Id
+     * 生成Id {@link IdType#ID_WORKER_STR}
      *
      * @param entity 实体
      * @return id
@@ -45,7 +46,7 @@ public interface IdGenerator {
     }
 
     /**
-     * 获取uuid
+     * 获取uuid {@link IdType#UUID}
      *
      * @param entity 实体
      * @return uuid

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

@@ -16,7 +16,7 @@
 package com.baomidou.mybatisplus.core.toolkit;
 
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
-import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
+import com.baomidou.mybatisplus.core.incrementer.DefaultGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
 
 import java.time.LocalDateTime;
@@ -38,7 +38,7 @@ public class IdWorker {
      * @deprecated 3.2.1
      */
     @Deprecated
-    private static IdGenerator ID_GENERATOR = new SnowflakeIdGenerator();
+    private static IdGenerator ID_GENERATOR = new DefaultGenerator();
 
     /**
      * 毫秒格式化时间
@@ -94,7 +94,7 @@ public class IdWorker {
      */
     @Deprecated
     public static void initSequence(long workerId, long dataCenterId) {
-        ID_GENERATOR = new SnowflakeIdGenerator(workerId, dataCenterId);
+        ID_GENERATOR = new DefaultGenerator(workerId, dataCenterId);
     }
 
     /**

+ 2 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandlerTest.java

@@ -1,7 +1,7 @@
 package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
-import com.baomidou.mybatisplus.core.incrementer.SnowflakeIdGenerator;
+import com.baomidou.mybatisplus.core.incrementer.DefaultGenerator;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import lombok.AllArgsConstructor;
@@ -45,7 +45,7 @@ class MybatisDefaultParameterHandlerTest {
         MappedStatement mappedStatement;
         Configuration configuration = new MybatisConfiguration();
         StaticSqlSource staticSqlSource = new StaticSqlSource(configuration, " ***********");
-        GlobalConfigUtils.getGlobalConfig(configuration).setIdGenerator(new SnowflakeIdGenerator()).setMetaObjectHandler(new MetaObjectHandler() {
+        GlobalConfigUtils.getGlobalConfig(configuration).setIdGenerator(new DefaultGenerator()).setMetaObjectHandler(new MetaObjectHandler() {
             @Override
             public void insertFill(MetaObject metaObject) {
                 setFieldValByName("insertOperator", "咩咩", metaObject);

+ 2 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/idgenerator/CustomIdGenerator.java

@@ -21,6 +21,8 @@ public class CustomIdGenerator implements IdGenerator {
 
     @Override
     public Number nextId(Object entity) {
+        //可以将当前传入的class全类名来作为bizKey,或者提取参数来生成bizKey进行分布式Id调用生成.
+        String bizKey = entity.getClass().getName();
         TableInfo tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
         String name = (String) SystemMetaObject.forObject(entity).getValue("name");
         //long test