浏览代码

优化数据方言获取方式减少对象创建

hubin 4 年之前
父节点
当前提交
3f5e2e832f

+ 1 - 1
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/DbType.java

@@ -120,7 +120,7 @@ public enum DbType {
     /**
      * HighGo
      */
-    HighGo("highgo", "瀚高数据库"),
+    HIGH_GO("highgo", "瀚高数据库"),
     /**
      * UNKONWN DB
      */

+ 50 - 8
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/DialectFactory.java

@@ -16,11 +16,10 @@
 package com.baomidou.mybatisplus.extension.plugins.pagination;
 
 import com.baomidou.mybatisplus.annotation.DbType;
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.DialectRegistry;
-import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
+import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.*;
 
-import java.util.Optional;
+import java.util.EnumMap;
+import java.util.Map;
 
 /**
  * 分页方言工厂类
@@ -29,11 +28,54 @@ import java.util.Optional;
  * @since 2016-01-23
  */
 public class DialectFactory {
-
-    private static final DialectRegistry DIALECT_REGISTRY = new DialectRegistry();
+    private static final Map<DbType, IDialect> DIALECT_ENUM_MAP = new EnumMap<>(DbType.class);
 
     public static IDialect getDialect(DbType dbType) {
-        return Optional.ofNullable(DIALECT_REGISTRY.getDialect(dbType))
-            .orElseThrow(() -> ExceptionUtils.mpe("%s database not supported.", dbType.getDb()));
+        IDialect dialect = DIALECT_ENUM_MAP.get(dbType);
+        if (null == dialect) {
+            if (dbType == DbType.OTHER) {
+                // 不支持情况
+                return null;
+            }
+            // mysql same type
+            else if (dbType == DbType.MYSQL
+                || dbType == DbType.MARIADB
+                || dbType == DbType.GBASE
+                || dbType == DbType.OSCAR
+                || dbType == DbType.XU_GU
+                || dbType == DbType.CLICK_HOUSE
+                || dbType == DbType.OCEAN_BASE) {
+                dialect = new MySqlDialect();
+            }
+            // postgresql same type
+            else if (dbType == DbType.ORACLE
+                || dbType == DbType.DM
+                || dbType == DbType.GAUSS) {
+                dialect = new OracleDialect();
+            }
+            // oracle same type
+            else if (dbType == DbType.POSTGRE_SQL
+                || dbType == DbType.H2
+                || dbType == DbType.SQLITE
+                || dbType == DbType.HSQL
+                || dbType == DbType.KINGBASE_ES
+                || dbType == DbType.PHOENIX) {
+                dialect = new PostgreDialect();
+            } else if (dbType == DbType.HIGH_GO) {
+                dialect = new HighGoDialect();
+            } else if (dbType == DbType.ORACLE_12C) {
+                dialect = new Oracle12cDialect();
+            } else if (dbType == DbType.DB2) {
+                dialect = new DB2Dialect();
+            } else if (dbType == DbType.SQL_SERVER2005) {
+                dialect = new SQLServer2005Dialect();
+            } else if (dbType == DbType.SQL_SERVER) {
+                dialect = new SQLServerDialect();
+            } else if (dbType == DbType.SYBASE) {
+                dialect = new SybaseDialect();
+            }
+            DIALECT_ENUM_MAP.put(dbType, dialect);
+        }
+        return dialect;
     }
 }

+ 0 - 74
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/dialects/DialectRegistry.java

@@ -1,74 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * 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
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.extension.plugins.pagination.dialects;
-
-import com.baomidou.mybatisplus.annotation.DbType;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.EnumMap;
-import java.util.Map;
-
-/**
- * 数据库方言注入
- *
- * @author nieqiuqiu
- * @since 2020-01-09
- */
-public class DialectRegistry {
-
-    private final Map<DbType, IDialect> dialect_enum_map = new EnumMap<>(DbType.class);
-
-    public DialectRegistry() {
-        // mysql and children
-        MySqlDialect mySqlDialect = new MySqlDialect();
-        dialect_enum_map.put(DbType.MYSQL, mySqlDialect);
-        dialect_enum_map.put(DbType.MARIADB, mySqlDialect);
-        dialect_enum_map.put(DbType.GBASE, mySqlDialect);
-        dialect_enum_map.put(DbType.OSCAR, mySqlDialect);
-        dialect_enum_map.put(DbType.XU_GU, mySqlDialect);
-        dialect_enum_map.put(DbType.CLICK_HOUSE, mySqlDialect);
-        dialect_enum_map.put(DbType.OCEAN_BASE, mySqlDialect);
-        // postgresql and children
-        PostgreDialect postgreDialect = new PostgreDialect();
-        dialect_enum_map.put(DbType.POSTGRE_SQL, postgreDialect);
-        dialect_enum_map.put(DbType.H2, postgreDialect);
-        dialect_enum_map.put(DbType.SQLITE, postgreDialect);
-        dialect_enum_map.put(DbType.HSQL, postgreDialect);
-        dialect_enum_map.put(DbType.KINGBASE_ES, postgreDialect);
-        dialect_enum_map.put(DbType.PHOENIX, postgreDialect);
-        dialect_enum_map.put(DbType.HighGo, new HighGoDialect());
-        // oracle and children
-        OracleDialect oracleDialect = new OracleDialect();
-        dialect_enum_map.put(DbType.ORACLE, oracleDialect);
-        dialect_enum_map.put(DbType.DM, oracleDialect);
-        dialect_enum_map.put(DbType.GAUSS, oracleDialect);
-        // other
-        dialect_enum_map.put(DbType.ORACLE_12C, new Oracle12cDialect());
-        dialect_enum_map.put(DbType.DB2, new DB2Dialect());
-        dialect_enum_map.put(DbType.SQL_SERVER2005, new SQLServer2005Dialect());
-        dialect_enum_map.put(DbType.SQL_SERVER, new SQLServerDialect());
-        dialect_enum_map.put(DbType.SYBASE, new SybaseDialect());
-    }
-
-    public IDialect getDialect(DbType dbType) {
-        return dialect_enum_map.get(dbType);
-    }
-
-    public Collection<IDialect> getDialects() {
-        return Collections.unmodifiableCollection(dialect_enum_map.values());
-    }
-}

+ 1 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/JdbcUtils.java

@@ -102,7 +102,7 @@ public class JdbcUtils {
         } else if (jdbcUrl.contains(":oceanbase:")) {
             return DbType.OCEAN_BASE;
         }else if (url.contains(":highgo:")) {
-            return DbType.HighGo;
+            return DbType.HIGH_GO;
         }else {
             logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
             return DbType.OTHER;