瀏覽代碼

optimization GlobalConfiguration, add GlobalConfigUtils.

Caratacus 8 年之前
父節點
當前提交
8913b5f7cd
共有 21 個文件被更改,包括 254 次插入224 次删除
  1. 3 3
      src/main/java/com/baomidou/mybatisplus/MybatisConfiguration.java
  2. 2 2
      src/main/java/com/baomidou/mybatisplus/MybatisDefaultParameterHandler.java
  3. 2 2
      src/main/java/com/baomidou/mybatisplus/MybatisMapperAnnotationBuilder.java
  4. 2 2
      src/main/java/com/baomidou/mybatisplus/MybatisMapperRegistry.java
  5. 4 3
      src/main/java/com/baomidou/mybatisplus/MybatisSessionFactoryBuilder.java
  6. 2 1
      src/main/java/com/baomidou/mybatisplus/entity/Column.java
  7. 4 180
      src/main/java/com/baomidou/mybatisplus/entity/GlobalConfiguration.java
  8. 5 4
      src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java
  9. 5 5
      src/main/java/com/baomidou/mybatisplus/mapper/SqlHelper.java
  10. 2 2
      src/main/java/com/baomidou/mybatisplus/mapper/SqlRunner.java
  11. 1 1
      src/main/java/com/baomidou/mybatisplus/plugins/PerformanceInterceptor.java
  12. 2 2
      src/main/java/com/baomidou/mybatisplus/plugins/SqlExplainInterceptor.java
  13. 3 3
      src/main/java/com/baomidou/mybatisplus/plugins/pagination/dialects/DB2Dialect.java
  14. 2 1
      src/main/java/com/baomidou/mybatisplus/spring/MybatisMapperRefresh.java
  15. 3 2
      src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java
  16. 200 0
      src/main/java/com/baomidou/mybatisplus/toolkit/GlobalConfigUtils.java
  17. 1 1
      src/main/java/com/baomidou/mybatisplus/toolkit/Sequence.java
  18. 6 6
      src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java
  19. 1 1
      src/test/java/com/baomidou/mybatisplus/test/EntityWrapperTest.java
  20. 3 2
      src/test/java/com/baomidou/mybatisplus/test/GlobalConfigurationTest.java
  21. 1 1
      src/test/java/com/baomidou/mybatisplus/test/mysql/UserMapperTest.java

+ 3 - 3
src/main/java/com/baomidou/mybatisplus/MybatisConfiguration.java

@@ -22,7 +22,7 @@ import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSession;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 
 /**
  * <p>
@@ -64,7 +64,7 @@ public class MybatisConfiguration extends Configuration {
     @Override
     public void addMappedStatement(MappedStatement ms) {
         logger.debug("addMappedStatement: " + ms.getId());
-        if (GlobalConfiguration.isRefresh(ms.getConfiguration())) {
+        if (GlobalConfigUtils.isRefresh(ms.getConfiguration())) {
             /*
              * 支持是否自动刷新 XML 变更内容,开发环境使用【 注:生产环境勿用!】
 			 */
@@ -84,7 +84,7 @@ public class MybatisConfiguration extends Configuration {
     @Override
     public void setDefaultScriptingLanguage(Class<?> driver) {
         if (driver == null) {
-			/* 设置自定义 driver */
+            /* 设置自定义 driver */
             driver = MybatisXMLLanguageDriver.class;
         }
         super.setDefaultScriptingLanguage(driver);

+ 2 - 2
src/main/java/com/baomidou/mybatisplus/MybatisDefaultParameterHandler.java

@@ -38,10 +38,10 @@ import org.apache.ibatis.type.TypeException;
 import org.apache.ibatis.type.TypeHandler;
 import org.apache.ibatis.type.TypeHandlerRegistry;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.entity.TableInfo;
 import com.baomidou.mybatisplus.enums.IdType;
 import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.IdWorker;
 import com.baomidou.mybatisplus.toolkit.MapUtils;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
@@ -105,7 +105,7 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
     protected static Object processBatch(MappedStatement ms, Object parameterObject) {
         boolean isFill = false;
         // 全局配置是否配置填充器
-        MetaObjectHandler metaObjectHandler = GlobalConfiguration.getMetaObjectHandler(ms.getConfiguration());
+        MetaObjectHandler metaObjectHandler = GlobalConfigUtils.getMetaObjectHandler(ms.getConfiguration());
         /* 只处理插入或更新操作 */
         if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
             isFill = true;

+ 2 - 2
src/main/java/com/baomidou/mybatisplus/MybatisMapperAnnotationBuilder.java

@@ -92,8 +92,8 @@ import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.TypeHandler;
 import org.apache.ibatis.type.UnknownTypeHandler;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 
 /**
  * <p>
@@ -140,7 +140,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
             Method[] methods = type.getMethods();
             // TODO 注入 CURD 动态 SQL (应该在注解之前注入)
             if (BaseMapper.class.isAssignableFrom(type)) {
-                GlobalConfiguration.getSqlInjector(configuration).inspectInject(assistant, type);
+                GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, type);
             }
             for (Method method : methods) {
                 try {

+ 2 - 2
src/main/java/com/baomidou/mybatisplus/MybatisMapperRegistry.java

@@ -26,7 +26,7 @@ import org.apache.ibatis.binding.MapperRegistry;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSession;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 
 /**
  * <p>
@@ -45,7 +45,7 @@ public class MybatisMapperRegistry extends MapperRegistry {
         super(config);
         this.config = config;
         // TODO注入SqlRunner
-        GlobalConfiguration.getSqlInjector(config).injectSqlRunner(config);
+        GlobalConfigUtils.getSqlInjector(config).injectSqlRunner(config);
     }
 
     @SuppressWarnings("unchecked")

+ 4 - 3
src/main/java/com/baomidou/mybatisplus/MybatisSessionFactoryBuilder.java

@@ -25,6 +25,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.IOUtils;
 
 /**
@@ -37,13 +38,13 @@ import com.baomidou.mybatisplus.toolkit.IOUtils;
  */
 public class MybatisSessionFactoryBuilder extends SqlSessionFactoryBuilder {
 
-    private GlobalConfiguration globalConfig = GlobalConfiguration.defaults();
+    private GlobalConfiguration globalConfig = GlobalConfigUtils.defaults();
 
     @Override
     public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
         try {
             MybatisXMLConfigBuilder parser = new MybatisXMLConfigBuilder(reader, environment, properties);
-            GlobalConfiguration.setGlobalConfig(parser.getConfiguration(), this.globalConfig);
+            GlobalConfigUtils.setGlobalConfig(parser.getConfiguration(), this.globalConfig);
             return build(parser.parse());
         } catch (Exception e) {
             throw ExceptionFactory.wrapException("Error building SqlSession.", e);
@@ -57,7 +58,7 @@ public class MybatisSessionFactoryBuilder extends SqlSessionFactoryBuilder {
     public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
         try {
             MybatisXMLConfigBuilder parser = new MybatisXMLConfigBuilder(inputStream, environment, properties);
-            GlobalConfiguration.setGlobalConfig(parser.getConfiguration(), this.globalConfig);
+            GlobalConfigUtils.setGlobalConfig(parser.getConfiguration(), this.globalConfig);
             return build(parser.parse());
         } catch (Exception e) {
             throw ExceptionFactory.wrapException("Error building SqlSession.", e);

+ 2 - 1
src/main/java/com/baomidou/mybatisplus/entity/Column.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.entity;
 import java.io.Serializable;
 
 import com.baomidou.mybatisplus.mapper.SqlRunner;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 /**
@@ -63,7 +64,7 @@ public class Column implements Serializable {
         }
         String quote = null;
         if (isEscape() && SqlRunner.FACTORY != null) {
-            GlobalConfiguration globalConfig = GlobalConfiguration.getGlobalConfig(SqlRunner.FACTORY.getConfiguration());
+            GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(SqlRunner.FACTORY.getConfiguration());
             quote = globalConfig.getIdentifierQuote() == null ? globalConfig.getDbType().getQuote() : globalConfig.getIdentifierQuote();
         }
         return AS + (StringUtils.isNotEmpty(quote) ? String.format(quote, as) : as);

+ 4 - 180
src/main/java/com/baomidou/mybatisplus/entity/GlobalConfiguration.java

@@ -15,18 +15,10 @@
  */
 package com.baomidou.mybatisplus.entity;
 
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Map;
+import java.io.Serializable;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentSkipListSet;
 
-import javax.sql.DataSource;
-
-import org.apache.ibatis.logging.Log;
-import org.apache.ibatis.logging.LogFactory;
-import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 
@@ -34,16 +26,13 @@ import com.baomidou.mybatisplus.MybatisSqlSessionTemplate;
 import com.baomidou.mybatisplus.enums.DBType;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.enums.IdType;
-import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.incrementer.IKeyGenerator;
-import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
 import com.baomidou.mybatisplus.mapper.ISqlInjector;
 import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
-import com.baomidou.mybatisplus.toolkit.IOUtils;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.JdbcUtils;
 import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
-import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 
 /**
  * <p>
@@ -53,18 +42,8 @@ import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
  * @author Caratacus
  * @since 2016-12-06
  */
-public class GlobalConfiguration implements Cloneable {
+public class GlobalConfiguration implements Serializable {
 
-    /**
-     * 默认参数
-     */
-    public static final GlobalConfiguration DEFAULT = new GlobalConfiguration();
-    // 日志
-    private static final Log logger = LogFactory.getLog(GlobalConfiguration.class);
-    /**
-     * 缓存全局信息
-     */
-    private static final Map<String, GlobalConfiguration> GLOBAL_CONFIG = new ConcurrentHashMap<>();
     // 逻辑删除全局值
     private String logicDeleteValue = null;
     // 逻辑未删除全局值
@@ -106,106 +85,6 @@ public class GlobalConfiguration implements Cloneable {
         this.sqlInjector = sqlInjector;
     }
 
-    /**
-     * 获取当前的SqlSessionFactory
-     *
-     * @param clazz
-     * @return
-     */
-    public static SqlSessionFactory currentSessionFactory(Class<?> clazz) {
-        String configMark = TableInfoHelper.getTableInfo(clazz).getConfigMark();
-        GlobalConfiguration mybatisGlobalConfig = GlobalConfiguration.getGlobalConfig(configMark);
-        return mybatisGlobalConfig.getSqlSessionFactory();
-    }
-
-    /**
-     * 获取默认MybatisGlobalConfig
-     *
-     * @return
-     */
-    public static GlobalConfiguration defaults() {
-        try {
-            GlobalConfiguration clone = DEFAULT.clone();
-            clone.setSqlInjector(new AutoSqlInjector());
-            return clone;
-        } catch (CloneNotSupportedException e) {
-            throw new MybatisPlusException("ERROR: CLONE MybatisGlobalConfig DEFAULT FAIL !  Cause:" + e);
-        }
-    }
-
-    /**
-     * <p>
-     * 设置全局设置(以configuration地址值作为Key)
-     * <p/>
-     *
-     * @param configuration
-     * @param mybatisGlobalConfig
-     * @return
-     */
-    public static void setGlobalConfig(Configuration configuration, GlobalConfiguration mybatisGlobalConfig) {
-        if (configuration == null || mybatisGlobalConfig == null) {
-            throw new MybatisPlusException("Error: Could not setGlobalConfig");
-        }
-        // 设置全局设置
-        GLOBAL_CONFIG.put(configuration.toString(), mybatisGlobalConfig);
-    }
-
-    /**
-     * 获取MybatisGlobalConfig (统一所有入口)
-     *
-     * @param configuration
-     * @return
-     */
-    public static GlobalConfiguration getGlobalConfig(Configuration configuration) {
-        if (configuration == null) {
-            throw new MybatisPlusException("Error: You need Initialize MybatisConfiguration !");
-        }
-        return getGlobalConfig(configuration.toString());
-    }
-
-    /**
-     * 获取MybatisGlobalConfig (统一所有入口)
-     *
-     * @param configMark
-     * @return
-     */
-    public static GlobalConfiguration getGlobalConfig(String configMark) {
-        GlobalConfiguration cache = GLOBAL_CONFIG.get(configMark);
-        if (cache == null) {
-            // 没有获取全局配置初始全局配置
-            logger.debug("DeBug: MyBatis Plus Global configuration Initializing !");
-            GLOBAL_CONFIG.put(configMark, DEFAULT);
-            return DEFAULT;
-        }
-        return cache;
-    }
-
-    public static DBType getDbType(Configuration configuration) {
-        return getGlobalConfig(configuration).getDbType();
-    }
-
-    public static IKeyGenerator getKeyGenerator(Configuration configuration) {
-        return getGlobalConfig(configuration).getKeyGenerator();
-    }
-
-    public static IdType getIdType(Configuration configuration) {
-        return getGlobalConfig(configuration).getIdType();
-    }
-
-    public static boolean isDbColumnUnderline(Configuration configuration) {
-        return getGlobalConfig(configuration).isDbColumnUnderline();
-    }
-
-    public static ISqlInjector getSqlInjector(Configuration configuration) {
-        // fix #140
-        GlobalConfiguration globalConfiguration = getGlobalConfig(configuration);
-        ISqlInjector sqlInjector = globalConfiguration.getSqlInjector();
-        if (sqlInjector == null) {
-            sqlInjector = new AutoSqlInjector();
-            globalConfiguration.setSqlInjector(sqlInjector);
-        }
-        return sqlInjector;
-    }
 
     public IKeyGenerator getKeyGenerator() {
         return keyGenerator;
@@ -215,57 +94,6 @@ public class GlobalConfiguration implements Cloneable {
         this.keyGenerator = keyGenerator;
     }
 
-    public static MetaObjectHandler getMetaObjectHandler(Configuration configuration) {
-        return getGlobalConfig(configuration).getMetaObjectHandler();
-    }
-
-    public static FieldStrategy getFieldStrategy(Configuration configuration) {
-        return getGlobalConfig(configuration).getFieldStrategy();
-    }
-
-    public static boolean isRefresh(Configuration configuration) {
-        return getGlobalConfig(configuration).isRefresh();
-    }
-
-    public static boolean isAutoSetDbType(Configuration configuration) {
-        return getGlobalConfig(configuration).isAutoSetDbType();
-    }
-
-    public static Set<String> getMapperRegistryCache(Configuration configuration) {
-        return getGlobalConfig(configuration).getMapperRegistryCache();
-    }
-
-    public static String getIdentifierQuote(Configuration configuration) {
-        return getGlobalConfig(configuration).getIdentifierQuote();
-    }
-
-    public static SqlSession getSqlSession(Configuration configuration) {
-        return getGlobalConfig(configuration).getSqlSession();
-    }
-
-    /**
-     * 设置元数据相关属性
-     *
-     * @param dataSource
-     * @param globalConfig
-     */
-    public static void setMetaData(DataSource dataSource, GlobalConfiguration globalConfig) {
-        Connection connection = null;
-        try {
-            connection = dataSource.getConnection();
-            String jdbcUrl = connection.getMetaData().getURL();
-            // 设置全局关键字
-            globalConfig.setSqlKeywords(connection.getMetaData().getSQLKeywords());
-            // 自动设置数据库类型
-            if (globalConfig.isAutoSetDbType()) {
-                globalConfig.setDbTypeByJdbcUrl(jdbcUrl);
-            }
-        } catch (SQLException e) {
-            logger.warn("Warn: GlobalConfiguration setMetaData Fail !  Cause:" + e);
-        } finally {
-            IOUtils.closeQuietly(connection);
-        }
-    }
 
     public String getLogicDeleteValue() {
         return logicDeleteValue;
@@ -398,10 +226,6 @@ public class GlobalConfiguration implements Cloneable {
         return sqlSession;
     }
 
-    @Override
-    protected GlobalConfiguration clone() throws CloneNotSupportedException {
-        return (GlobalConfiguration) super.clone();
-    }
 
     /**
      * <p>
@@ -413,7 +237,7 @@ public class GlobalConfiguration implements Cloneable {
      */
     public SqlSessionFactory signGlobalConfig(SqlSessionFactory sqlSessionFactory) {
         if (null != sqlSessionFactory) {
-            setGlobalConfig(sqlSessionFactory.getConfiguration(), this);
+            GlobalConfigUtils.setGlobalConfig(sqlSessionFactory.getConfiguration(), this);
         }
         return sqlSessionFactory;
     }

+ 5 - 4
src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -47,6 +47,7 @@ import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.enums.IdType;
 import com.baomidou.mybatisplus.enums.SqlMethod;
 import com.baomidou.mybatisplus.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
@@ -75,7 +76,7 @@ public class AutoSqlInjector implements ISqlInjector {
      */
     public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
         String className = mapperClass.toString();
-        Set<String> mapperRegistryCache = GlobalConfiguration.getMapperRegistryCache(builderAssistant.getConfiguration());
+        Set<String> mapperRegistryCache = GlobalConfigUtils.getMapperRegistryCache(builderAssistant.getConfiguration());
         if (!mapperRegistryCache.contains(className)) {
             inject(builderAssistant, mapperClass);
             mapperRegistryCache.add(className);
@@ -152,7 +153,7 @@ public class AutoSqlInjector implements ISqlInjector {
         this.injectSelectMapsSql(SqlMethod.SELECT_MAPS, mapperClass, modelClass, table);
         this.injectSelectMapsSql(SqlMethod.SELECT_MAPS_PAGE, mapperClass, modelClass, table);
         this.injectSelectObjsSql(SqlMethod.SELECT_OBJS, mapperClass, modelClass, table);
-		/* 自定义方法 */
+        /* 自定义方法 */
         this.inject(configuration, builderAssistant, mapperClass, modelClass, table);
     }
 
@@ -570,7 +571,7 @@ public class AutoSqlInjector implements ISqlInjector {
      * @return
      */
     protected String sqlWordConvert(String convertStr) {
-        GlobalConfiguration globalConfig = GlobalConfiguration.getGlobalConfig(configuration);
+        GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
         return SqlReservedWords.convert(globalConfig, convertStr);
     }
 
@@ -999,7 +1000,7 @@ public class AutoSqlInjector implements ISqlInjector {
      * </p>
      */
     protected GlobalConfiguration getGlobalConfig() {
-        return GlobalConfiguration.getGlobalConfig(configuration);
+        return GlobalConfigUtils.getGlobalConfig(configuration);
     }
 
 }

+ 5 - 5
src/main/java/com/baomidou/mybatisplus/mapper/SqlHelper.java

@@ -24,11 +24,11 @@ import org.apache.ibatis.session.ExecutorType;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.entity.TableInfo;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.plugins.Page;
 import com.baomidou.mybatisplus.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 
 /**
@@ -64,7 +64,7 @@ public class SqlHelper {
      * @return SqlSession
      */
     public static SqlSession sqlSessionBatch(Class<?> clazz) {
-        return GlobalConfiguration.currentSessionFactory(clazz).openSession(ExecutorType.BATCH);
+        return GlobalConfigUtils.currentSessionFactory(clazz).openSession(ExecutorType.BATCH);
     }
 
     /**
@@ -76,9 +76,9 @@ public class SqlHelper {
     private static SqlSession getSqlSession(Class<?> clazz) {
         SqlSession session = null;
         try {
-            SqlSessionFactory sqlSessionFactory = GlobalConfiguration.currentSessionFactory(clazz);
+            SqlSessionFactory sqlSessionFactory = GlobalConfigUtils.currentSessionFactory(clazz);
             Configuration configuration = sqlSessionFactory.getConfiguration();
-            session = GlobalConfiguration.getGlobalConfig(configuration).getSqlSession();
+            session = GlobalConfigUtils.getGlobalConfig(configuration).getSqlSession();
         } catch (Exception e) {
             // ignored
         }
@@ -96,7 +96,7 @@ public class SqlHelper {
      */
     public static SqlSession sqlSession(Class<?> clazz, boolean autoCommit) {
         SqlSession sqlSession = getSqlSession(clazz);
-        return (sqlSession != null) ? sqlSession : GlobalConfiguration.currentSessionFactory(clazz).openSession(autoCommit);
+        return (sqlSession != null) ? sqlSession : GlobalConfigUtils.currentSessionFactory(clazz).openSession(autoCommit);
     }
 
     /**

+ 2 - 2
src/main/java/com/baomidou/mybatisplus/mapper/SqlRunner.java

@@ -23,8 +23,8 @@ import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.springframework.transaction.annotation.Transactional;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.plugins.Page;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 /**
@@ -147,7 +147,7 @@ public class SqlRunner {
      * <p/>
      */
     private SqlSession sqlSession() {
-        return (clazz != null) ? SqlHelper.sqlSession(clazz) : GlobalConfiguration.getSqlSession(FACTORY.getConfiguration());
+        return (clazz != null) ? SqlHelper.sqlSession(clazz) : GlobalConfigUtils.getSqlSession(FACTORY.getConfiguration());
     }
 
 }

+ 1 - 1
src/main/java/com/baomidou/mybatisplus/plugins/PerformanceInterceptor.java

@@ -98,7 +98,7 @@ public class PerformanceInterceptor implements Interceptor {
                     }
                 } else {
                     Class<?> clazz = Class.forName("oracle.jdbc.driver.OracleStatement");
-                    oracleGetOriginalSqlMethod = clazz.getDeclaredMethod("getOriginalSql", null);
+                    oracleGetOriginalSqlMethod = clazz.getDeclaredMethod("getOriginalSql", (Class<?>) null);
                     if (oracleGetOriginalSqlMethod != null) {
                         Object stmtSql = oracleGetOriginalSqlMethod.invoke(statement);
                         if (stmtSql != null && stmtSql instanceof String) {

+ 2 - 2
src/main/java/com/baomidou/mybatisplus/plugins/SqlExplainInterceptor.java

@@ -35,9 +35,9 @@ import org.apache.ibatis.plugin.Signature;
 import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.session.Configuration;
 
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.enums.DBType;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 import com.baomidou.mybatisplus.toolkit.VersionUtils;
 
@@ -74,7 +74,7 @@ public class SqlExplainInterceptor implements Interceptor {
             BoundSql boundSql = ms.getBoundSql(parameter);
             Connection connection = executor.getTransaction().getConnection();
             String databaseVersion = connection.getMetaData().getDatabaseProductVersion();
-            if (GlobalConfiguration.getDbType(configuration).equals(DBType.MYSQL)
+            if (GlobalConfigUtils.getDbType(configuration).equals(DBType.MYSQL)
                     && VersionUtils.compare(minMySQLVersion, databaseVersion)) {
                 logger.warn("Warn: Your mysql version needs to be greater than '5.6.3' to execute of Sql Explain!");
                 return invocation.proceed();

+ 3 - 3
src/main/java/com/baomidou/mybatisplus/plugins/pagination/dialects/DB2Dialect.java

@@ -1,12 +1,12 @@
 /**
  * Copyright (c) 2011-2014, hubin (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>
  * http://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

+ 2 - 1
src/main/java/com/baomidou/mybatisplus/spring/MybatisMapperRefresh.java

@@ -43,6 +43,7 @@ import org.springframework.core.io.UrlResource;
 import org.springframework.util.ResourceUtils;
 
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.SystemClock;
 
 /**
@@ -138,7 +139,7 @@ public class MybatisMapperRefresh implements Runnable {
     }
 
     public void run() {
-        final GlobalConfiguration globalConfig = GlobalConfiguration.getGlobalConfig(configuration);
+        final GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
         /*
          * 启动 XML 热加载
 		 */

+ 3 - 2
src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java

@@ -60,6 +60,7 @@ import com.baomidou.mybatisplus.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.mapper.SqlRunner;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.PackageHelper;
 
 /**
@@ -119,7 +120,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 
     private ObjectWrapperFactory objectWrapperFactory;
 
-    private GlobalConfiguration globalConfig = GlobalConfiguration.defaults();
+    private GlobalConfiguration globalConfig = GlobalConfigUtils.defaults();
 
     // TODO 注入全局配置
     public void setGlobalConfig(GlobalConfiguration globalConfig) {
@@ -513,7 +514,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 
         configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));
         // 设置元数据相关
-        GlobalConfiguration.setMetaData(dataSource, globalConfig);
+        GlobalConfigUtils.setMetaData(dataSource, globalConfig);
         SqlSessionFactory sqlSessionFactory = this.sqlSessionFactoryBuilder.build(configuration);
         // TODO SqlRunner
         SqlRunner.FACTORY = sqlSessionFactory;

+ 200 - 0
src/main/java/com/baomidou/mybatisplus/toolkit/GlobalConfigUtils.java

@@ -0,0 +1,200 @@
+package com.baomidou.mybatisplus.toolkit;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.sql.DataSource;
+
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+
+import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.enums.DBType;
+import com.baomidou.mybatisplus.enums.FieldStrategy;
+import com.baomidou.mybatisplus.enums.IdType;
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.incrementer.IKeyGenerator;
+import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
+import com.baomidou.mybatisplus.mapper.ISqlInjector;
+import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
+
+/**
+ * <p>
+ * Mybatis全局缓存工具类
+ * </p>
+ *
+ * @author Caratacus
+ * @since 2017-06-15
+ */
+public class GlobalConfigUtils {
+
+    /**
+     * 默认参数
+     */
+    public static final GlobalConfiguration DEFAULT = defaults();
+    // 日志
+    private static final Log logger = LogFactory.getLog(GlobalConfigUtils.class);
+    /**
+     * 缓存全局信息
+     */
+    private static final Map<String, GlobalConfiguration> GLOBAL_CONFIG = new ConcurrentHashMap<>();
+
+    public GlobalConfigUtils() {
+        // 构造方法
+    }
+
+    /**
+     * 获取当前的SqlSessionFactory
+     *
+     * @param clazz
+     * @return
+     */
+    public static SqlSessionFactory currentSessionFactory(Class<?> clazz) {
+        String configMark = TableInfoHelper.getTableInfo(clazz).getConfigMark();
+        GlobalConfiguration mybatisGlobalConfig = GlobalConfigUtils.getGlobalConfig(configMark);
+        return mybatisGlobalConfig.getSqlSessionFactory();
+    }
+
+    /**
+     * 获取默认MybatisGlobalConfig
+     *
+     * @return
+     */
+    public static GlobalConfiguration defaults() {
+        return new GlobalConfiguration();
+    }
+
+    /**
+     * <p>
+     * 设置全局设置(以configuration地址值作为Key)
+     * <p/>
+     *
+     * @param configuration
+     * @param mybatisGlobalConfig
+     * @return
+     */
+    public static void setGlobalConfig(Configuration configuration, GlobalConfiguration mybatisGlobalConfig) {
+        if (configuration == null || mybatisGlobalConfig == null) {
+            throw new MybatisPlusException("Error: Could not setGlobalConfig");
+        }
+        // 设置全局设置
+        GLOBAL_CONFIG.put(configuration.toString(), mybatisGlobalConfig);
+    }
+
+    /**
+     * 获取MybatisGlobalConfig (统一所有入口)
+     *
+     * @param configuration
+     * @return
+     */
+    public static GlobalConfiguration getGlobalConfig(Configuration configuration) {
+        if (configuration == null) {
+            throw new MybatisPlusException("Error: You need Initialize MybatisConfiguration !");
+        }
+        return getGlobalConfig(configuration.toString());
+    }
+
+    /**
+     * 获取MybatisGlobalConfig (统一所有入口)
+     *
+     * @param configMark
+     * @return
+     */
+    public static GlobalConfiguration getGlobalConfig(String configMark) {
+        GlobalConfiguration cache = GLOBAL_CONFIG.get(configMark);
+        if (cache == null) {
+            // 没有获取全局配置初始全局配置
+            logger.debug("DeBug: MyBatis Plus Global configuration Initializing !");
+            GLOBAL_CONFIG.put(configMark, DEFAULT);
+            return DEFAULT;
+        }
+        return cache;
+    }
+
+    public static DBType getDbType(Configuration configuration) {
+        return getGlobalConfig(configuration).getDbType();
+    }
+
+    public static IKeyGenerator getKeyGenerator(Configuration configuration) {
+        return getGlobalConfig(configuration).getKeyGenerator();
+    }
+
+    public static IdType getIdType(Configuration configuration) {
+        return getGlobalConfig(configuration).getIdType();
+    }
+
+    public static boolean isDbColumnUnderline(Configuration configuration) {
+        return getGlobalConfig(configuration).isDbColumnUnderline();
+    }
+
+    public static ISqlInjector getSqlInjector(Configuration configuration) {
+        // fix #140
+        GlobalConfiguration globalConfiguration = getGlobalConfig(configuration);
+        ISqlInjector sqlInjector = globalConfiguration.getSqlInjector();
+        if (sqlInjector == null) {
+            sqlInjector = new AutoSqlInjector();
+            globalConfiguration.setSqlInjector(sqlInjector);
+        }
+        return sqlInjector;
+    }
+
+    public static MetaObjectHandler getMetaObjectHandler(Configuration configuration) {
+        return getGlobalConfig(configuration).getMetaObjectHandler();
+    }
+
+    public static FieldStrategy getFieldStrategy(Configuration configuration) {
+        return getGlobalConfig(configuration).getFieldStrategy();
+    }
+
+    public static boolean isRefresh(Configuration configuration) {
+        return getGlobalConfig(configuration).isRefresh();
+    }
+
+    public static boolean isAutoSetDbType(Configuration configuration) {
+        return getGlobalConfig(configuration).isAutoSetDbType();
+    }
+
+    public static Set<String> getMapperRegistryCache(Configuration configuration) {
+        return getGlobalConfig(configuration).getMapperRegistryCache();
+    }
+
+    public static String getIdentifierQuote(Configuration configuration) {
+        return getGlobalConfig(configuration).getIdentifierQuote();
+    }
+
+    public static SqlSession getSqlSession(Configuration configuration) {
+        return getGlobalConfig(configuration).getSqlSession();
+    }
+
+    /**
+     * 设置元数据相关属性
+     *
+     * @param dataSource
+     * @param globalConfig
+     */
+    public static void setMetaData(DataSource dataSource, GlobalConfiguration globalConfig) {
+        Connection connection = null;
+        try {
+            connection = dataSource.getConnection();
+            String jdbcUrl = connection.getMetaData().getURL();
+            // 设置全局关键字
+            globalConfig.setSqlKeywords(connection.getMetaData().getSQLKeywords());
+            // 自动设置数据库类型
+            if (globalConfig.isAutoSetDbType()) {
+                globalConfig.setDbTypeByJdbcUrl(jdbcUrl);
+            }
+        } catch (SQLException e) {
+            logger.warn("Warn: GlobalConfiguration setMetaData Fail !  Cause:" + e);
+        } finally {
+            IOUtils.closeQuietly(connection);
+        }
+    }
+
+
+}

+ 1 - 1
src/main/java/com/baomidou/mybatisplus/toolkit/Sequence.java

@@ -96,7 +96,7 @@ public class Sequence {
             mpid.append(name.split("@")[0]);
         }
         /*
-		 * MAC + PID 的 hashcode 获取16个低位
+         * MAC + PID 的 hashcode 获取16个低位
 		 */
         return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
     }

+ 6 - 6
src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java

@@ -116,10 +116,10 @@ public class TableInfoHelper {
         if (null != builderAssistant) {
             tableInfo.setCurrentNamespace(builderAssistant.getCurrentNamespace());
             tableInfo.setConfigMark(builderAssistant.getConfiguration());
-            globalConfig = GlobalConfiguration.getGlobalConfig(builderAssistant.getConfiguration());
+            globalConfig = GlobalConfigUtils.getGlobalConfig(builderAssistant.getConfiguration());
         } else {
             // 兼容测试场景
-            globalConfig = GlobalConfiguration.DEFAULT;
+            globalConfig = GlobalConfigUtils.DEFAULT;
         }
         /* 表名 */
         TableName table = clazz.getAnnotation(TableName.class);
@@ -379,13 +379,13 @@ public class TableInfoHelper {
      */
     public static void initSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
         Configuration configuration = sqlSessionFactory.getConfiguration();
-        GlobalConfiguration globalConfig = GlobalConfiguration.getGlobalConfig(configuration);
+        GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
         // SqlRunner
         SqlRunner.FACTORY = sqlSessionFactory;
         if (globalConfig == null) {
-            GlobalConfiguration defaultCache = GlobalConfiguration.defaults();
+            GlobalConfiguration defaultCache = GlobalConfigUtils.defaults();
             defaultCache.setSqlSessionFactory(sqlSessionFactory);
-            GlobalConfiguration.setGlobalConfig(configuration, defaultCache);
+            GlobalConfigUtils.setGlobalConfig(configuration, defaultCache);
         } else {
             globalConfig.setSqlSessionFactory(sqlSessionFactory);
         }
@@ -398,7 +398,7 @@ public class TableInfoHelper {
      */
     public static KeyGenerator genKeyGenerator(TableInfo tableInfo, MapperBuilderAssistant builderAssistant,
                                                String baseStatementId, LanguageDriver languageDriver) {
-        IKeyGenerator keyGenerator = GlobalConfiguration.getKeyGenerator(builderAssistant.getConfiguration());
+        IKeyGenerator keyGenerator = GlobalConfigUtils.getKeyGenerator(builderAssistant.getConfiguration());
         if (null == keyGenerator) {
             throw new IllegalArgumentException("not configure IKeyGenerator implementation class.");
         }

+ 1 - 1
src/test/java/com/baomidou/mybatisplus/test/EntityWrapperTest.java

@@ -76,7 +76,7 @@ public class EntityWrapperTest {
     @Test
     public void test12() {
         /*
-		 * 实体带where orderby
+         * 实体带where orderby
 		 */
         ew.setEntity(new User(1));
         ew.where("name={0}", "'123'").orderBy("id", false);

+ 3 - 2
src/test/java/com/baomidou/mybatisplus/test/GlobalConfigurationTest.java

@@ -32,6 +32,7 @@ import com.baomidou.mybatisplus.test.mysql.entity.NotPK;
 import com.baomidou.mybatisplus.test.mysql.entity.Test;
 import com.baomidou.mybatisplus.test.mysql.mapper.NotPKMapper;
 import com.baomidou.mybatisplus.test.mysql.mapper.TestMapper;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 
 /**
  * <p>
@@ -48,7 +49,7 @@ public class GlobalConfigurationTest {
      */
     @SuppressWarnings("unchecked")
     public static void main(String[] args) {
-        GlobalConfiguration global = GlobalConfiguration.defaults();
+        GlobalConfiguration global = GlobalConfigUtils.defaults();
         global.setAutoSetDbType(true);
         // 设置全局校验机制为FieldStrategy.Empty
         global.setFieldStrategy(2);
@@ -58,7 +59,7 @@ public class GlobalConfigurationTest {
         dataSource.setUsername("root");
         dataSource.setPassword("521");
         dataSource.setMaxTotal(1000);
-        GlobalConfiguration.setMetaData(dataSource, global);
+        GlobalConfigUtils.setMetaData(dataSource, global);
         // 加载配置文件
         InputStream inputStream = GlobalConfigurationTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
         MybatisSessionFactoryBuilder factoryBuilder = new MybatisSessionFactoryBuilder();

+ 1 - 1
src/test/java/com/baomidou/mybatisplus/test/mysql/UserMapperTest.java

@@ -202,7 +202,7 @@ public class UserMapperTest {
         sleep();
 
 		/*
-		 * <p> 修改 </p>
+         * <p> 修改 </p>
 		 *
 		 * updateById 是从 BaseMapper 中继承而来的,UserMapper.xml中并没有申明改sql
 		 */