Browse Source

!84 优化分页方言获取.
Merge pull request !84 from 聂秋秋/dialect

青苗 5 years ago
parent
commit
f0d5cb1c85

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

@@ -30,55 +30,55 @@ public enum DbType {
     /**
      * MYSQL
      */
-    MYSQL("mysql", "MySql数据库"),
+    MYSQL("mysql", "MySql数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect"),
     /**
      * MARIADB
      */
-    MARIADB("mariadb", "MariaDB数据库"),
+    MARIADB("mariadb", "MariaDB数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MariaDBDialect"),
     /**
      * ORACLE
      */
-    ORACLE("oracle", "Oracle数据库"),
+    ORACLE("oracle", "Oracle数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.OracleDialect"),
     /**
      * DB2
      */
-    DB2("db2", "DB2数据库"),
+    DB2("db2", "DB2数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.DB2Dialect"),
     /**
      * H2
      */
-    H2("h2", "H2数据库"),
+    H2("h2", "H2数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.H2Dialect"),
     /**
      * HSQL
      */
-    HSQL("hsql", "HSQL数据库"),
+    HSQL("hsql", "HSQL数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.HSQLDialect"),
     /**
      * SQLITE
      */
-    SQLITE("sqlite", "SQLite数据库"),
+    SQLITE("sqlite", "SQLite数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.SQLiteDialect"),
     /**
      * POSTGRE
      */
-    POSTGRE_SQL("postgresql", "Postgre数据库"),
+    POSTGRE_SQL("postgresql", "Postgre数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.PostgreDialect"),
     /**
      * SQLSERVER2005
      */
-    SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"),
+    SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.SQLServer2005Dialect"),
     /**
      * SQLSERVER
      */
-    SQL_SERVER("sqlserver", "SQLServer数据库"),
+    SQL_SERVER("sqlserver", "SQLServer数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.SQLServerDialect"),
     /**
      * DM
      */
-    DM("dm", "达梦数据库"),
+    DM("dm", "达梦数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.DmDialect"),
     /**
      * xugu
      */
-    XUGU("xugu", "虚谷数据库"),
+    XUGU("xugu", "虚谷数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.XuguDialect"),
     /**
      * UNKONWN DB
      */
-    OTHER("other", "其他数据库");
+    OTHER("other", "其他数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.UnknownDialect");
 
     /**
      * 数据库名称
@@ -89,6 +89,11 @@ public enum DbType {
      */
     private final String desc;
 
+    /**
+     * 分页方言
+     */
+    private String dialect;
+
     /**
      * 获取数据库类型(默认 MySql)
      *

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

@@ -64,27 +64,17 @@ public class DialectFactory {
     private static IDialect getDialect(DbType dbType, String dialectClazz) {
         IDialect dialect = DIALECT_CACHE.get(dbType.getDb());
         if (null == dialect) {
-            // 自定义方言
-            if (StringUtils.isNotEmpty(dialectClazz)) {
-                dialect = DIALECT_CACHE.get(dialectClazz);
-                if (null != dialect) {
-                    return dialect;
+            String dialectClassName = StringUtils.isEmpty(dialectClazz) ? dbType.getDialect() : dialectClazz;
+            try {
+                Class<?> clazz = Class.forName(dialectClassName);
+                if (IDialect.class.isAssignableFrom(clazz)) {
+                    dialect = (IDialect) clazz.newInstance();
+                    DIALECT_CACHE.put(dbType.getDb(), dialect);
                 }
-                try {
-                    Class<?> clazz = Class.forName(dialectClazz);
-                    if (IDialect.class.isAssignableFrom(clazz)) {
-                        dialect = (IDialect) clazz.newInstance();
-                        DIALECT_CACHE.put(dialectClazz, dialect);
-                    }
-                } catch (ClassNotFoundException e) {
-                    throw ExceptionUtils.mpe("Class : %s is not found", dialectClazz);
-                } catch (IllegalAccessException | InstantiationException e) {
-                    throw ExceptionUtils.mpe("Class : %s can not be instance", dialectClazz);
-                }
-            } else {
-                // 缓存方言
-                dialect = getDialectByDbType(dbType);
-                DIALECT_CACHE.put(dbType.getDb(), dialect);
+            } catch (ClassNotFoundException e) {
+                throw ExceptionUtils.mpe("Class : %s is not found", dialectClazz);
+            } catch (IllegalAccessException | InstantiationException e) {
+                throw ExceptionUtils.mpe("Class : %s can not be instance", dialectClazz);
             }
             /* 未配置方言则抛出异常 */
             Assert.notNull(dialect, "The value of the dialect property in mybatis configuration.xml is not defined.");
@@ -92,40 +82,4 @@ public class DialectFactory {
         return dialect;
     }
 
-    /**
-     * 根据数据库类型选择不同分页方言
-     *
-     * @param dbType 数据库类型
-     * @return 分页语句组装类
-     */
-    private static IDialect getDialectByDbType(DbType dbType) {
-        switch (dbType) {
-            case MYSQL:
-                return new MySqlDialect();
-            case MARIADB:
-                return new MariaDBDialect();
-            case ORACLE:
-                return new OracleDialect();
-            case DB2:
-                return new DB2Dialect();
-            case H2:
-                return new H2Dialect();
-            case SQL_SERVER:
-                return new SQLServerDialect();
-            case SQL_SERVER2005:
-                return new SQLServer2005Dialect();
-            case POSTGRE_SQL:
-                return new PostgreDialect();
-            case HSQL:
-                return new HSQLDialect();
-            case SQLITE:
-                return new SQLiteDialect();
-            case DM:
-                return new DmDialect();
-            case XUGU:
-                return new XuguDialect();
-            default:
-                throw ExceptionUtils.mpe("The Database's IDialect Not Supported!");
-        }
-    }
 }

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

@@ -0,0 +1,32 @@
+/*
+ * 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.extension.plugins.pagination.dialects;
+
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
+
+/**
+ * 未知方言
+ * @author nieqiuqiu
+ */
+public class UnknownDialect implements IDialect {
+
+    @Override
+    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
+        throw new MybatisPlusException(" not support.");
+    }
+
+}

+ 40 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/plugins/pagination/DialectFactoryTest.java

@@ -0,0 +1,40 @@
+/*
+ * 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.extension.plugins.pagination;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * @author nieqiuqiu
+ */
+class DialectFactoryTest {
+
+    @Test
+    void test() {
+        DialectModel dialectModel;
+        IPage<Object> page = new Page<>();
+        //使用默认方言
+        dialectModel = DialectFactory.buildPaginationSql(page, "select * from test ", DbType.MYSQL, null);
+        Assertions.assertEquals(dialectModel.getDialectSql(), "select * from test  LIMIT ?,?");
+        //使用默认自定义方言
+        dialectModel = DialectFactory.buildPaginationSql(page, "select * from test ", DbType.H2, MySqlDialect.class.getName());
+        Assertions.assertEquals(dialectModel.getDialectSql(), "select * from test  LIMIT ?,?");
+    }
+}

+ 58 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/DbTypeTest.java

@@ -0,0 +1,58 @@
+/*
+ * 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.test;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.*;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author nieqiuqiu
+ */
+class DbTypeTest {
+
+    private static final Map<DbType, Class<? extends IDialect>> DIALECT_MAP = new ConcurrentHashMap<>();
+
+    static {
+        DIALECT_MAP.put(DbType.DB2, DB2Dialect.class);
+        DIALECT_MAP.put(DbType.DM, DmDialect.class);
+        DIALECT_MAP.put(DbType.H2, H2Dialect.class);
+        DIALECT_MAP.put(DbType.MYSQL, MySqlDialect.class);
+        DIALECT_MAP.put(DbType.MARIADB, MariaDBDialect.class);
+        DIALECT_MAP.put(DbType.ORACLE, OracleDialect.class);
+        DIALECT_MAP.put(DbType.POSTGRE_SQL, PostgreDialect.class);
+        DIALECT_MAP.put(DbType.SQL_SERVER, SQLServerDialect.class);
+        DIALECT_MAP.put(DbType.SQL_SERVER2005, SQLServer2005Dialect.class);
+        DIALECT_MAP.put(DbType.SQLITE, SQLiteDialect.class);
+        DIALECT_MAP.put(DbType.HSQL, HSQLDialect.class);
+        DIALECT_MAP.put(DbType.XUGU, XuguDialect.class);
+        DIALECT_MAP.put(DbType.OTHER, UnknownDialect.class);
+    }
+
+    @Test
+    void test() throws ClassNotFoundException {
+        DbType[] values = DbType.values();
+        Assertions.assertEquals(values.length, DIALECT_MAP.size());
+        for (DbType dbType : values) {
+            Class<?> aClass = Class.forName(dbType.getDialect());
+            Assertions.assertEquals(aClass, DIALECT_MAP.get(dbType));
+        }
+    }
+}