浏览代码

!90 扩展id 生成方式
Merge pull request !90 from mlovewt/3.0

青苗 5 年之前
父节点
当前提交
644d7ad333

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

@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.core.incrementer;
+
+import com.baomidou.mybatisplus.core.toolkit.Sequence;
+
+/**
+ * 默认 主键 生成器
+ *
+ * @author sd-wangtaicheng@sdcncsi.com.cn
+ * @date 2019/10/15
+ */
+public class DefaultIdGenerator implements IdGenerator {
+    private static Sequence WORKER;
+
+    public DefaultIdGenerator() {
+        WORKER = new Sequence();
+    }
+
+    public DefaultIdGenerator(long workerId, long dataCenterId) {
+        WORKER = new Sequence(workerId, dataCenterId);
+    }
+
+    @Override
+    public long nextId() {
+        return WORKER.nextId();
+    }
+}

+ 32 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/incrementer/IdGenerator.java

@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.core.incrementer;
+
+/**
+ * Id 生成器接口
+ * 可以自定义id生成方式,如 baidu 的UidGenerator,美团的leaf 等
+ *
+ * @author sd-wangtaicheng@sdcncsi.com.cn
+ * @date 2019/10/15
+ */
+public interface IdGenerator {
+    /**
+     * 生成Id
+     *
+     * @return id
+     */
+    long nextId();
+}

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

@@ -15,6 +15,9 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
+import com.baomidou.mybatisplus.core.incrementer.DefaultIdGenerator;
+import com.baomidou.mybatisplus.core.incrementer.IdGenerator;
+
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.UUID;
@@ -32,7 +35,7 @@ public class IdWorker {
     /**
      * 主机和进程的机器码
      */
-    private static Sequence WORKER = new Sequence();
+    private static IdGenerator ID_GENERATOR = new DefaultIdGenerator();
 
     /**
      * 毫秒格式化时间
@@ -40,11 +43,11 @@ public class IdWorker {
     public static final DateTimeFormatter MILLISECOND = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
 
     public static long getId() {
-        return WORKER.nextId();
+        return ID_GENERATOR.nextId();
     }
 
     public static String getIdStr() {
-        return String.valueOf(WORKER.nextId());
+        return String.valueOf(ID_GENERATOR.nextId());
     }
 
     /**
@@ -66,10 +69,19 @@ public class IdWorker {
      * 有参构造器
      *
      * @param workerId     工作机器 ID
-     * @param datacenterId 序列号
+     * @param dataCenterId 序列号
+     */
+    public static void initSequence(long workerId, long dataCenterId) {
+        ID_GENERATOR = new DefaultIdGenerator(workerId, dataCenterId);
+    }
+
+    /**
+     * 自定义id 生成方式
+     *
+     * @param idGenerator id 生成器
      */
-    public static void initSequence(long workerId, long datacenterId) {
-        WORKER = new Sequence(workerId, datacenterId);
+    public static void setIdGenerator(IdGenerator idGenerator) {
+        ID_GENERATOR = idGenerator;
     }
 
     /**