فهرست منبع

sb 下自动注入 IdentifierGenerator 逻辑

miemie 5 سال پیش
والد
کامیت
9a7e1fecf7

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

@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
+import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
 import com.baomidou.mybatisplus.core.injector.ISqlInjector;
 import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
 import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Mapper;
@@ -67,6 +68,7 @@ import org.springframework.util.StringUtils;
 import javax.sql.DataSource;
 import javax.sql.DataSource;
 import java.util.List;
 import java.util.List;
 import java.util.Optional;
 import java.util.Optional;
+import java.util.function.Consumer;
 import java.util.stream.Stream;
 import java.util.stream.Stream;
 
 
 /**
 /**
@@ -200,25 +202,31 @@ public class MybatisPlusAutoConfiguration implements InitializingBean {
         // TODO 此处必为非 NULL
         // TODO 此处必为非 NULL
         GlobalConfig globalConfig = this.properties.getGlobalConfig();
         GlobalConfig globalConfig = this.properties.getGlobalConfig();
         // TODO 注入填充器
         // TODO 注入填充器
-        if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class, false, false).length > 0) {
-            MetaObjectHandler metaObjectHandler = this.applicationContext.getBean(MetaObjectHandler.class);
-            globalConfig.setMetaObjectHandler(metaObjectHandler);
-        }
+        this.getBeanThen(MetaObjectHandler.class, globalConfig::setMetaObjectHandler);
         // TODO 注入主键生成器
         // TODO 注入主键生成器
-        if (this.applicationContext.getBeanNamesForType(IKeyGenerator.class, false, false).length > 0) {
-            IKeyGenerator keyGenerator = this.applicationContext.getBean(IKeyGenerator.class);
-            globalConfig.getDbConfig().setKeyGenerator(keyGenerator);
-        }
+        this.getBeanThen(IKeyGenerator.class, i -> globalConfig.getDbConfig().setKeyGenerator(i));
         // TODO 注入sql注入器
         // TODO 注入sql注入器
-        if (this.applicationContext.getBeanNamesForType(ISqlInjector.class, false, false).length > 0) {
-            ISqlInjector iSqlInjector = this.applicationContext.getBean(ISqlInjector.class);
-            globalConfig.setSqlInjector(iSqlInjector);
-        }
+        this.getBeanThen(ISqlInjector.class, globalConfig::setSqlInjector);
+        // TODO 注入ID生成器
+        this.getBeanThen(IdentifierGenerator.class, globalConfig::setIdentifierGenerator);
         // TODO 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
         // TODO 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
         factory.setGlobalConfig(globalConfig);
         factory.setGlobalConfig(globalConfig);
         return factory.getObject();
         return factory.getObject();
     }
     }
 
 
+    /**
+     * 检查spring容器里是否有对应的bean,有则进行消费
+     *
+     * @param clazz    class
+     * @param consumer 消费
+     * @param <T>      泛型
+     */
+    private <T> void getBeanThen(Class<T> clazz, Consumer<T> consumer) {
+        if (this.applicationContext.getBeanNamesForType(ISqlInjector.class, false, false).length > 0) {
+            consumer.accept(this.applicationContext.getBean(clazz));
+        }
+    }
+
     // TODO 入参使用 MybatisSqlSessionFactoryBean
     // TODO 入参使用 MybatisSqlSessionFactoryBean
     private void applyConfiguration(MybatisSqlSessionFactoryBean factory) {
     private void applyConfiguration(MybatisSqlSessionFactoryBean factory) {
         // TODO 使用 MybatisConfiguration
         // TODO 使用 MybatisConfiguration

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

@@ -82,12 +82,20 @@ public class MybatisSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
     public SqlSessionFactory build(Configuration config) {
     public SqlSessionFactory build(Configuration config) {
         MybatisConfiguration configuration = (MybatisConfiguration) config;
         MybatisConfiguration configuration = (MybatisConfiguration) config;
         GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
         GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
-        if (null != globalConfig.getWorkerId() && null != globalConfig.getDatacenterId()) {
-            IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
-            globalConfig.setIdentifierGenerator(identifierGenerator);
-            //TODO 这里只是为了兼容下,并没多大重要,方法标记过时了.
-            IdWorker.setIdGenerator(identifierGenerator);
+        final IdentifierGenerator identifierGenerator;
+        if (globalConfig.getIdentifierGenerator() == null) {
+            if (null != globalConfig.getWorkerId() && null != globalConfig.getDatacenterId()) {
+                identifierGenerator = new DefaultIdentifierGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
+            } else {
+                identifierGenerator = new DefaultIdentifierGenerator();
+            }
+        } else {
+            identifierGenerator = globalConfig.getIdentifierGenerator();
         }
         }
+        globalConfig.setIdentifierGenerator(identifierGenerator);
+        //TODO 这里只是为了兼容下,并没多大重要,方法标记过时了.
+        IdWorker.setIdentifierGenerator(identifierGenerator);
+
         if (globalConfig.isEnableSqlRunner()) {
         if (globalConfig.isEnableSqlRunner()) {
             new SqlRunnerInjector().inject(configuration);
             new SqlRunnerInjector().inject(configuration);
         }
         }

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

@@ -18,7 +18,6 @@ package com.baomidou.mybatisplus.core.config;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
-import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
 import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
 import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
 import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
@@ -94,7 +93,7 @@ public class GlobalConfig implements Serializable {
     /**
     /**
      * 主键生成器
      * 主键生成器
      */
      */
-    private IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator();
+    private IdentifierGenerator identifierGenerator;
 
 
     @Data
     @Data
     public static class DbConfig {
     public static class DbConfig {

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

@@ -39,7 +39,7 @@ public class IdWorker {
      * @deprecated 3.3.0
      * @deprecated 3.3.0
      */
      */
     @Deprecated
     @Deprecated
-    private static IdentifierGenerator ID_GENERATOR = new DefaultIdentifierGenerator();
+    private static IdentifierGenerator IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator();
 
 
     /**
     /**
      * 毫秒格式化时间
      * 毫秒格式化时间
@@ -54,7 +54,18 @@ public class IdWorker {
      */
      */
     @Deprecated
     @Deprecated
     public static long getId() {
     public static long getId() {
-        return (long) ID_GENERATOR.nextId(new Object());
+        return getId(new Object());
+    }
+
+    /**
+     * 获取唯一ID
+     *
+     * @return id
+     * @deprecated 3.3.0
+     */
+    @Deprecated
+    public static long getId(Object entity) {
+        return IDENTIFIER_GENERATOR.nextId(entity).longValue();
     }
     }
 
 
     /**
     /**
@@ -65,7 +76,18 @@ public class IdWorker {
      */
      */
     @Deprecated
     @Deprecated
     public static String getIdStr() {
     public static String getIdStr() {
-        return String.valueOf(ID_GENERATOR.nextId(new Object()));
+        return getIdStr(new Object());
+    }
+
+    /**
+     * 获取唯一ID
+     *
+     * @return id
+     * @deprecated 3.3.0
+     */
+    @Deprecated
+    public static String getIdStr(Object entity) {
+        return IDENTIFIER_GENERATOR.nextId(entity).toString();
     }
     }
 
 
     /**
     /**
@@ -91,12 +113,12 @@ public class IdWorker {
      *
      *
      * @param workerId     工作机器 ID
      * @param workerId     工作机器 ID
      * @param dataCenterId 序列号
      * @param dataCenterId 序列号
-     * @see #setIdGenerator(IdentifierGenerator)
+     * @see #setIdentifierGenerator(IdentifierGenerator)
      * @deprecated 3.3.0
      * @deprecated 3.3.0
      */
      */
     @Deprecated
     @Deprecated
     public static void initSequence(long workerId, long dataCenterId) {
     public static void initSequence(long workerId, long dataCenterId) {
-        ID_GENERATOR = new DefaultIdentifierGenerator(workerId, dataCenterId);
+        IDENTIFIER_GENERATOR = new DefaultIdentifierGenerator(workerId, dataCenterId);
     }
     }
 
 
     /**
     /**
@@ -107,8 +129,8 @@ public class IdWorker {
      * @deprecated 3.3.0
      * @deprecated 3.3.0
      */
      */
     @Deprecated
     @Deprecated
-    public static void setIdGenerator(IdentifierGenerator identifierGenerator) {
-        ID_GENERATOR = identifierGenerator;
+    public static void setIdentifierGenerator(IdentifierGenerator identifierGenerator) {
+        IDENTIFIER_GENERATOR = identifierGenerator;
     }
     }
 
 
     /**
     /**
@@ -118,5 +140,4 @@ public class IdWorker {
         ThreadLocalRandom random = ThreadLocalRandom.current();
         ThreadLocalRandom random = ThreadLocalRandom.current();
         return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY);
         return new UUID(random.nextLong(), random.nextLong()).toString().replace(StringPool.DASH, StringPool.EMPTY);
     }
     }
-
 }
 }