Forráskód Böngészése

开放 IdWorker 雪花算法自定义机器 ID

hubin 6 éve
szülő
commit
d012ec80b9

+ 1 - 1
build.gradle

@@ -49,7 +49,7 @@ ext {
 
 allprojects{
     group = 'com.baomidou'
-    version = '2.3.1'
+    version = '2.3.2'
 }
 
 

+ 30 - 0
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/spring/boot/starter/GlobalConfig.java

@@ -86,6 +86,14 @@ public class GlobalConfig {
      * 缓存 Sql 解析初始化
      */
     private Boolean sqlParserCache;
+    /**
+     * 机器 ID 部分
+     */
+    private Long workerId;
+    /**
+     * 数据标识 ID 部分
+     */
+    private Long datacenterId;
 
     public Integer getIdType() {
         return idType;
@@ -191,6 +199,22 @@ public class GlobalConfig {
         this.sqlParserCache = sqlParserCache;
     }
 
+    public Long getWorkerId() {
+        return workerId;
+    }
+
+    public void setWorkerId(Long workerId) {
+        this.workerId = workerId;
+    }
+
+    public Long getDatacenterId() {
+        return datacenterId;
+    }
+
+    public void setDatacenterId(Long datacenterId) {
+        this.datacenterId = datacenterId;
+    }
+
     public GlobalConfiguration convertGlobalConfiguration() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
         GlobalConfiguration globalConfiguration = new GlobalConfiguration();
         if (StringUtils.isNotEmpty(this.getIdentifierQuote())) {
@@ -229,6 +253,12 @@ public class GlobalConfig {
         if (StringUtils.checkValNotNull(this.getCapitalMode())) {
             globalConfiguration.setCapitalMode(this.getCapitalMode());
         }
+        if (StringUtils.checkValNotNull(this.getWorkerId())) {
+            globalConfiguration.setWorkerId(this.getWorkerId());
+        }
+        if (StringUtils.checkValNotNull(this.getDatacenterId())) {
+            globalConfiguration.setDatacenterId(this.getDatacenterId());
+        }
         if (null != this.getSqlParserCache()) {
             globalConfiguration.setSqlParserCache(this.getSqlParserCache());
         }

+ 7 - 6
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.java

@@ -116,9 +116,16 @@ public class MybatisPlusAutoConfiguration {
         if (StringUtils.hasText(this.properties.getConfigLocation())) {
             factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
         }
+        GlobalConfiguration globalConfig;
+        if (!ObjectUtils.isEmpty(this.properties.getGlobalConfig())) {
+            globalConfig = this.properties.getGlobalConfig().convertGlobalConfiguration();
+        } else {
+            globalConfig = new GlobalConfiguration();
+        }
         MybatisConfiguration configuration = this.properties.getConfiguration();
         if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
             configuration = new MybatisConfiguration();
+            configuration.init(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
         }
         if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
             for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
@@ -152,12 +159,6 @@ public class MybatisPlusAutoConfiguration {
         if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
             factory.setMapperLocations(this.properties.resolveMapperLocations());
         }
-        GlobalConfiguration globalConfig;
-        if (!ObjectUtils.isEmpty(this.properties.getGlobalConfig())) {
-            globalConfig = this.properties.getGlobalConfig().convertGlobalConfiguration();
-        } else {
-            globalConfig = new GlobalConfiguration();
-        }
         //注入填充器
         if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class, false,
             false).length > 0) {

+ 14 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/MybatisConfiguration.java

@@ -23,6 +23,7 @@ import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSession;
 
 import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.toolkit.IdWorker;
 
 /**
  * <p>
@@ -52,6 +53,17 @@ public class MybatisConfiguration extends Configuration {
         logger.debug("Mybatis-plus init success.");
     }
 
+    /**
+     * 配置初始化
+     */
+    public MybatisConfiguration init(Long workerId, Long datacenterId) {
+        // 初始化 Sequence
+        if (null != workerId && null != datacenterId) {
+            IdWorker.initSequence(workerId, datacenterId);
+        }
+        return this;
+    }
+
     /**
      * <p>
      * MybatisPlus 加载 SQL 顺序:
@@ -68,13 +80,13 @@ public class MybatisConfiguration extends Configuration {
         if (GlobalConfigUtils.isRefresh(ms.getConfiguration())) {
             /*
              * 支持是否自动刷新 XML 变更内容,开发环境使用【 注:生产环境勿用!】
-			 */
+             */
             this.mappedStatements.remove(ms.getId());
         } else {
             if (this.mappedStatements.containsKey(ms.getId())) {
                 /*
                  * 说明已加载了xml中的节点; 忽略mapper中的SqlProvider数据
-				 */
+                 */
                 logger.error("mapper[" + ms.getId() + "] is ignored, because it's exists, maybe from xml file");
                 return;
             }

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java

@@ -416,7 +416,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
                 LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
             }
             // TODO 使用自定义配置
-            configuration = new MybatisConfiguration();
+            configuration = new MybatisConfiguration().init(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
             if (this.configurationProperties != null) {
                 configuration.setVariables(this.configurationProperties);
             }

+ 23 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/entity/GlobalConfiguration.java

@@ -42,6 +42,14 @@ import com.baomidou.mybatisplus.toolkit.StringUtils;
  */
 public class GlobalConfiguration implements Serializable {
 
+    /**
+     * 机器 ID 部分
+     */
+    private Long workerId;
+    /**
+     * 数据标识 ID 部分
+     */
+    private Long datacenterId;
     /**
      * 逻辑删除全局值
      */
@@ -117,6 +125,21 @@ public class GlobalConfiguration implements Serializable {
         this.sqlInjector = sqlInjector;
     }
 
+    public Long getWorkerId() {
+        return workerId;
+    }
+
+    public void setWorkerId(Long workerId) {
+        this.workerId = workerId;
+    }
+
+    public Long getDatacenterId() {
+        return datacenterId;
+    }
+
+    public void setDatacenterId(Long datacenterId) {
+        this.datacenterId = datacenterId;
+    }
 
     public IKeyGenerator getKeyGenerator() {
         return keyGenerator;

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

@@ -31,14 +31,26 @@ public class IdWorker {
     /**
      * 主机和进程的机器码
      */
-    private static final Sequence worker = new Sequence();
+    private static Sequence WORKER = new Sequence();
 
     public static long getId() {
-        return worker.nextId();
+        return WORKER.nextId();
     }
 
     public static String getIdStr() {
-        return String.valueOf(worker.nextId());
+        return String.valueOf(WORKER.nextId());
+    }
+
+    /**
+     * <p>
+     * 有参构造器
+     * </p>
+     *
+     * @param workerId     工作机器 ID
+     * @param datacenterId 序列号
+     */
+    public static void initSequence(long workerId, long datacenterId) {
+        WORKER = new Sequence(workerId, datacenterId);
     }
 
     /**