聂秋秋 5 лет назад
Родитель
Сommit
b086d92c01

+ 11 - 65
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/DataSourceConfig.java

@@ -17,14 +17,15 @@ package com.baomidou.mybatisplus.generator.config;
 
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import com.baomidou.mybatisplus.generator.config.converts.*;
-import com.baomidou.mybatisplus.generator.config.querys.*;
+import com.baomidou.mybatisplus.generator.config.converts.TypeConvertRegistry;
+import com.baomidou.mybatisplus.generator.config.querys.DbQueryRegistry;
 import lombok.Data;
 import lombok.experimental.Accessors;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.util.Optional;
 
 /**
  * 数据库配置
@@ -71,39 +72,10 @@ public class DataSourceConfig {
 
     public IDbQuery getDbQuery() {
         if (null == dbQuery) {
-            switch (getDbType()) {
-                case ORACLE:
-                    dbQuery = new OracleQuery();
-                    break;
-                case SQL_SERVER:
-                    dbQuery = new SqlServerQuery();
-                    break;
-                case POSTGRE_SQL:
-                    dbQuery = new PostgreSqlQuery();
-                    break;
-                case DB2:
-                    dbQuery = new DB2Query();
-                    break;
-                case MARIADB:
-                    dbQuery = new MariadbQuery();
-                    break;
-                case H2:
-                    dbQuery = new H2Query();
-                    break;
-                case SQLITE:
-                    dbQuery = new SqliteQuery();
-                    break;
-                case DM:
-                    dbQuery = new DMQuery();
-                    break;
-                case KINGBASE_ES:
-                    dbQuery = new KingbaseESQuery();
-                    break;
-                default:
-                    // 默认 MYSQL
-                    dbQuery = new MySqlQuery();
-                    break;
-            }
+            DbType dbType = getDbType();
+            DbQueryRegistry dbQueryRegistry = new DbQueryRegistry();
+            // 默认 MYSQL
+            dbQuery = Optional.ofNullable(dbQueryRegistry.getDbQuery(dbType)).orElseGet(() -> dbQueryRegistry.getDbQuery(DbType.MYSQL));
         }
         return dbQuery;
     }
@@ -159,36 +131,10 @@ public class DataSourceConfig {
 
     public ITypeConvert getTypeConvert() {
         if (null == typeConvert) {
-            switch (getDbType()) {
-                case ORACLE:
-                    typeConvert = new OracleTypeConvert();
-                    break;
-                case SQL_SERVER:
-                    typeConvert = new SqlServerTypeConvert();
-                    break;
-                case POSTGRE_SQL:
-                    typeConvert = new PostgreSqlTypeConvert();
-                    break;
-                case DB2:
-                    typeConvert = new DB2TypeConvert();
-                    break;
-                case SQLITE:
-                    typeConvert = new SqliteTypeConvert();
-                    break;
-                case DM:
-                    typeConvert = new DmTypeConvert();
-                    break;
-                case MARIADB:
-                    typeConvert = new MySqlTypeConvert();
-                    break;
-                case KINGBASE_ES:
-                    typeConvert = new KingbaseESTypeConvert();
-                    break;
-                default:
-                    // 默认 MYSQL
-                    typeConvert = new MySqlTypeConvert();
-                    break;
-            }
+            DbType dbType = getDbType();
+            TypeConvertRegistry typeConvertRegistry = new TypeConvertRegistry();
+            // 默认 MYSQL
+            typeConvert = Optional.ofNullable(typeConvertRegistry.getTypeConvert(dbType)).orElseGet(() -> typeConvertRegistry.getTypeConvert(DbType.MYSQL));
         }
         return typeConvert;
     }

+ 48 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/converts/TypeConvertRegistry.java

@@ -0,0 +1,48 @@
+/*
+ * 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.generator.config.converts;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.generator.config.ITypeConvert;
+
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * @author nieqiuqiu
+ * @date 2020-01-09
+ * @since 3.3.1
+ */
+public class TypeConvertRegistry {
+
+    private final Map<DbType, ITypeConvert> type_convert_enum_map = new EnumMap<>(DbType.class);
+
+    public TypeConvertRegistry() {
+        type_convert_enum_map.put(DbType.ORACLE, new OracleTypeConvert());
+        type_convert_enum_map.put(DbType.SQL_SERVER, new SqlServerTypeConvert());
+        type_convert_enum_map.put(DbType.POSTGRE_SQL, new PostgreSqlTypeConvert());
+        type_convert_enum_map.put(DbType.DB2, new DB2TypeConvert());
+        type_convert_enum_map.put(DbType.SQLITE, new SqliteTypeConvert());
+        type_convert_enum_map.put(DbType.DM, new DmTypeConvert());
+        type_convert_enum_map.put(DbType.MARIADB, new MySqlTypeConvert());
+        type_convert_enum_map.put(DbType.KINGBASE_ES, new KingbaseESTypeConvert());
+        type_convert_enum_map.put(DbType.MYSQL, new MySqlTypeConvert());
+    }
+
+    public ITypeConvert getTypeConvert(DbType dbType) {
+        return type_convert_enum_map.get(dbType);
+    }
+}

+ 49 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DbQueryRegistry.java

@@ -0,0 +1,49 @@
+/*
+ * 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.generator.config.querys;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.generator.config.IDbQuery;
+
+import java.util.EnumMap;
+import java.util.Map;
+
+/**
+ * @author nieqiuqiu
+ * @date 2020-01-09
+ * @since 3.3.1
+ */
+public class DbQueryRegistry {
+
+    private final Map<DbType, IDbQuery> db_query_enum_map = new EnumMap<>(DbType.class);
+
+    public DbQueryRegistry() {
+        db_query_enum_map.put(DbType.ORACLE, new OracleQuery());
+        db_query_enum_map.put(DbType.SQL_SERVER, new SqlServerQuery());
+        db_query_enum_map.put(DbType.POSTGRE_SQL, new PostgreSqlQuery());
+        db_query_enum_map.put(DbType.DB2, new DB2Query());
+        db_query_enum_map.put(DbType.MARIADB, new MariadbQuery());
+        db_query_enum_map.put(DbType.H2, new H2Query());
+        db_query_enum_map.put(DbType.SQLITE, new SqliteQuery());
+        db_query_enum_map.put(DbType.DM, new DMQuery());
+        db_query_enum_map.put(DbType.KINGBASE_ES, new KingbaseESQuery());
+        db_query_enum_map.put(DbType.MYSQL, new MySqlQuery());
+    }
+
+    public IDbQuery getDbQuery(DbType dbType) {
+        return db_query_enum_map.get(dbType);
+    }
+}