Browse Source

优化 DialectModel 的参数命名,以及大部分数据库方言,剩下sqlserver2005和HSQL和DB2

miemie 6 years ago
parent
commit
32d91605d5

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

@@ -20,8 +20,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
-import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
-import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
+import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.*;
 import org.apache.ibatis.session.RowBounds;
 
 import java.util.Map;
@@ -109,26 +108,26 @@ public class DialectFactory {
         switch (dbType) {
             case MYSQL:
                 return new MySqlDialect();
-//            case MARIADB:
-//                return new MariaDBDialect();
-//            case ORACLE:
-//                return new OracleDialect();
+            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 H2:
+                return new H2Dialect();
+            case SQL_SERVER:
+                return new SQLServerDialect();
 //            case SQL_SERVER2005:
 //                return new SQLServer2005Dialect();
-//            case POSTGRE_SQL:
-//                return new PostgreDialect();
+            case POSTGRE_SQL:
+                return new PostgreDialect();
 //            case HSQL:
 //                return new HSQLDialect();
-//            case SQLITE:
-//                return new SQLiteDialect();
-//            case DM:
-//                return new DmDialect();
+            case SQLITE:
+                return new SQLiteDialect();
+            case DM:
+                return new DmDialect();
             default:
                 throw ExceptionUtils.mpe("The Database's IDialect Not Supported!");
         }

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

@@ -24,8 +24,8 @@ import java.util.function.Supplier;
 @Getter
 @Accessors(chain = true)
 public class DialectModel {
-    public static final String OFFSET_NAME = "mybatis_plus_first";
-    public static final String LIMIT_NAME = "mybatis_plus_second";
+    public static final String FIRST_PARAM_NAME = "mybatis_plus_first";
+    public static final String SECOND_PARAM_NAME = "mybatis_plus_second";
 
     /**
      * 分页方言 sql
@@ -73,10 +73,10 @@ public class DialectModel {
     public DialectModel setConsumer(boolean isFirstParam, Function<List<ParameterMapping>, Integer> function) {
         if (isFirstParam) {
             firstParamConsumer = i -> i.add(function.apply(mappingsSupplier.get()),
-                new ParameterMapping.Builder(configurationSupplier.get(), OFFSET_NAME, long.class).build());
+                new ParameterMapping.Builder(configurationSupplier.get(), FIRST_PARAM_NAME, long.class).build());
         } else {
             secondParamConsumer = i -> i.add(function.apply(mappingsSupplier.get()),
-                new ParameterMapping.Builder(configurationSupplier.get(), LIMIT_NAME, long.class).build());
+                new ParameterMapping.Builder(configurationSupplier.get(), SECOND_PARAM_NAME, long.class).build());
         }
         return this;
     }
@@ -91,14 +91,25 @@ public class DialectModel {
     public DialectModel setConsumer(boolean isFirstParam) {
         if (isFirstParam) {
             firstParamConsumer = i -> i.add(new ParameterMapping.Builder(configurationSupplier.get(),
-                OFFSET_NAME, long.class).build());
+                FIRST_PARAM_NAME, long.class).build());
         } else {
             secondParamConsumer = i -> i.add(new ParameterMapping.Builder(configurationSupplier.get(),
-                LIMIT_NAME, long.class).build());
+                SECOND_PARAM_NAME, long.class).build());
         }
         return this;
     }
 
+    /**
+     * 设置消费
+     * <p>
+     * 不带下标的,两个值都有
+     *
+     * @return this
+     */
+    public DialectModel setConsumerChain() {
+        return setConsumer(true).setConsumer(false);
+    }
+
     /**
      * 消费掉
      *
@@ -119,7 +130,7 @@ public class DialectModel {
      */
     private void putToDialectMap(long firstParam, long secondParam) {
         dialectMap = new HashMap<>(2);
-        dialectMap.put(OFFSET_NAME, firstParam);
-        dialectMap.put(LIMIT_NAME, secondParam);
+        dialectMap.put(FIRST_PARAM_NAME, firstParam);
+        dialectMap.put(SECOND_PARAM_NAME, secondParam);
     }
 }

+ 28 - 28
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/dialects/DmDialect.java

@@ -1,28 +1,28 @@
-///*
-// * 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
-// * License for the specific language governing permissions and limitations under
-// * the License.
-// */
-//package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
-//
-///**
-// * <p>
-// * 达梦数据库完全继承 Oracle
-// * </p>
-// *
-// * @author hubin
-// * @since 2018-08-21
-// */
-//public class DmDialect extends OracleDialect {
-//
-//}
+/*
+ * 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
+
+/**
+ * <p>
+ * 达梦数据库完全继承 Oracle
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-08-21
+ */
+public class DmDialect extends OracleDialect {
+
+}

+ 41 - 37
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/dialects/H2Dialect.java

@@ -1,37 +1,41 @@
-///*
-// * 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
-// * License for the specific language governing permissions and limitations under
-// * the License.
-// */
-//package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
-//
-///**
-// * <p>
-// * H2 数据库分页方言
-// * </p>
-// *
-// * @author hubin
-// * @since 2016-11-10
-// */
-//public class H2Dialect implements IDialect {
-//
-//    @Override
-//    public String buildPaginationSql(String originalSql, long offset, long limit) {
-//        StringBuilder sql = new StringBuilder(originalSql);
-//        sql.append(" limit ").append(limit);
-//        if (offset > 0) {
-//            sql.append(" offset ").append(offset);
-//        }
-//        return sql.toString();
-//    }
-//}
+/*
+ * 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
+
+/**
+ * <p>
+ * H2 数据库分页方言
+ * </p>
+ *
+ * @author hubin
+ * @since 2016-11-10
+ */
+public class H2Dialect implements IDialect {
+
+    @Override
+    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
+        String sql = originalSql + " limit " + LIMIT;
+        boolean existOffset = false;
+        if (offset > 0) {
+            existOffset = true;
+            sql += (" offset " + OFFSET);
+        }
+        DialectModel model = new DialectModel(sql, limit, offset);
+        return existOffset ? model.setConsumerChain() : model.setConsumer(true);
+    }
+}

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

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
 
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
 
 /**
@@ -26,6 +27,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
  * @since 2016-01-23
  */
 public interface IDialect {
+    /**
+     * 这俩没什么特殊意义
+     * 只是为了实现类方便使用,以及区分分页 sql 的参数
+     */
+    String LIMIT = StringPool.QUESTION_MARK;
+    String OFFSET = StringPool.QUESTION_MARK;
 
     /**
      * 组装分页语句

+ 2 - 11
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/dialects/MySqlDialect.java

@@ -30,16 +30,7 @@ public class MySqlDialect implements IDialect {
 
     @Override
     public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
-        String sql;
-        boolean existOffset = true;
-        if (offset == 0) {
-            existOffset = false;
-            sql = originalSql + " LIMIT " + StringPool.QUESTION_MARK;
-        } else {
-            sql = originalSql + " LIMIT " + StringPool.QUESTION_MARK + StringPool.COMMA + StringPool.QUESTION_MARK;
-        }
-        DialectModel model = new DialectModel(sql, offset, limit);
-        model.setConsumer(false);
-        return existOffset ? model.setConsumer(true) : model;
+        String sql = originalSql + " LIMIT " + OFFSET + StringPool.COMMA + LIMIT;
+        return new DialectModel(sql, offset, limit).setConsumerChain();
     }
 }

+ 5 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/dialects/OracleDialect.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
 
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
 
 /**
@@ -30,11 +29,11 @@ public class OracleDialect implements IDialect {
 
     @Override
     public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
-        long firstParam = (offset >= 1) ? (offset + limit) : limit;
+        limit = (offset >= 1) ? (offset + limit) : limit;
         String sql = "SELECT * FROM ( SELECT TMP.*, ROWNUM ROW_ID FROM ( " +
-            originalSql + " ) TMP WHERE ROWNUM <=" + StringPool.QUESTION_MARK +
-            ") WHERE ROW_ID > " + StringPool.QUESTION_MARK;
-        DialectModel model = new DialectModel(sql, firstParam, offset);
-        return model.setConsumer(true).setConsumer(false);
+            originalSql + " ) TMP WHERE ROWNUM <=" + limit +
+            ") WHERE ROW_ID > " + OFFSET;
+        DialectModel model = new DialectModel(sql, limit, offset);
+        return model.setConsumerChain();
     }
 }

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

@@ -1,32 +1,36 @@
-///*
-// * 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
-// * License for the specific language governing permissions and limitations under
-// * the License.
-// */
-//package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
-//
-///**
-// * <p>
-// * Postgre 数据库分页语句组装实现
-// * </p>
-// *
-// * @author hubin
-// * @since 2016-01-23
-// */
-//public class PostgreDialect implements IDialect {
-//
-//    @Override
-//    public String buildPaginationSql(String originalSql, long offset, long limit) {
-//        return originalSql + " limit " + limit + " offset " + offset;
-//    }
-//}
+/*
+ * 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
+
+/**
+ * <p>
+ * Postgre 数据库分页语句组装实现
+ * </p>
+ *
+ * @author hubin
+ * @since 2016-01-23
+ */
+public class PostgreDialect implements IDialect {
+
+    @Override
+    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
+        String sql = originalSql + " limit " + LIMIT + " offset " + OFFSET;
+        DialectModel model = new DialectModel(sql, limit, offset);
+        return model.setConsumerChain();
+    }
+}

+ 36 - 36
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/dialects/SQLServerDialect.java

@@ -1,36 +1,36 @@
-///*
-// * 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
-// * License for the specific language governing permissions and limitations under
-// * the License.
-// */
-//package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
-//
-///**
-// * <p>
-// * SQLServer 数据库分页语句组装实现
-// * </p>
-// *
-// * @author hubin
-// * @since 2016-03-23
-// */
-//public class SQLServerDialect implements IDialect {
-//
-//
-//    @Override
-//    public String buildPaginationSql(String originalSql, long offset, long limit) {
-//        StringBuilder sql = new StringBuilder(originalSql);
-//        sql.append(" OFFSET ").append(offset).append(" ROWS FETCH NEXT ");
-//        sql.append(limit).append(" ROWS ONLY");
-//        return sql.toString();
-//    }
-//}
+/*
+ * 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
+
+/**
+ * <p>
+ * SQLServer 数据库分页语句组装实现
+ * </p>
+ *
+ * @author hubin
+ * @since 2016-03-23
+ */
+public class SQLServerDialect implements IDialect {
+
+    @Override
+    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
+        String sql = originalSql + " OFFSET " + OFFSET + " ROWS FETCH NEXT " + LIMIT + " ROWS ONLY";
+        DialectModel model = new DialectModel(sql, offset, limit);
+        return model.setConsumerChain();
+    }
+}

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

@@ -1,32 +1,36 @@
-///*
-// * 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
-// * License for the specific language governing permissions and limitations under
-// * the License.
-// */
-//package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
-//
-///**
-// * <p>
-// * SQLite 数据库分页语句组装实现
-// * </p>
-// *
-// * @author hubin
-// * @since 2016-01-23
-// */
-//public class SQLiteDialect implements IDialect {
-//
-//    @Override
-//    public String buildPaginationSql(String originalSql, long offset, long limit) {
-//        return originalSql + " limit " + limit + " offset " + offset;
-//    }
-//}
+/*
+ * 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
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
+
+/**
+ * <p>
+ * SQLite 数据库分页语句组装实现
+ * </p>
+ *
+ * @author hubin
+ * @since 2016-01-23
+ */
+public class SQLiteDialect implements IDialect {
+
+    @Override
+    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
+        String sql = originalSql + " limit " + LIMIT + " offset " + OFFSET;
+        DialectModel model = new DialectModel(sql, limit, offset);
+        return model.setConsumerChain();
+    }
+}