Bladeren bron

优化 DialectModel 的参数命名,OracleDialect

miemie 6 jaren geleden
bovenliggende
commit
70377bda0b

+ 19 - 25
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_offset";
-    public static final String LIMIT_NAME = "mybatis_plus_limit";
+    public static final String OFFSET_NAME = "mybatis_plus_first";
+    public static final String LIMIT_NAME = "mybatis_plus_second";
 
     /**
      * 分页方言 sql
@@ -45,12 +45,12 @@ public class DialectModel {
     /**
      * 消费偏移量
      */
-    private Consumer<List<ParameterMapping>> offsetConsumer = i -> {
+    private Consumer<List<ParameterMapping>> firstParamConsumer = i -> {
     };
     /**
      * 消费范围量
      */
-    private Consumer<List<ParameterMapping>> limitConsumer = i -> {
+    private Consumer<List<ParameterMapping>> secondParamConsumer = i -> {
     };
     /**
      * 分页数据参数 map
@@ -58,9 +58,9 @@ public class DialectModel {
     @Setter(value = AccessLevel.NONE)
     private Map<String, Long> dialectMap;
 
-    public DialectModel(String dialectSql, long offset, long limit) {
+    public DialectModel(String dialectSql, long firstParam, long secondParam) {
         this.dialectSql = dialectSql;
-        this.putToDialectMap(offset, limit);
+        this.putToDialectMap(firstParam, secondParam);
     }
 
     /**
@@ -68,16 +68,14 @@ public class DialectModel {
      * <p>
      * 带下标的
      *
-     * @param isOffset 是否是 offset
-     * @param function 提供 index
      * @return this
      */
-    public DialectModel setConsumer(boolean isOffset, Function<List<ParameterMapping>, Integer> function) {
-        if (isOffset) {
-            offsetConsumer = i -> i.add(function.apply(mappingsSupplier.get()),
+    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());
         } else {
-            limitConsumer = i -> i.add(function.apply(mappingsSupplier.get()),
+            secondParamConsumer = i -> i.add(function.apply(mappingsSupplier.get()),
                 new ParameterMapping.Builder(configurationSupplier.get(), LIMIT_NAME, long.class).build());
         }
         return this;
@@ -88,15 +86,14 @@ public class DialectModel {
      * <p>
      * 不带下标的
      *
-     * @param isOffset 是否是 offset
      * @return this
      */
-    public DialectModel setConsumer(boolean isOffset) {
-        if (isOffset) {
-            offsetConsumer = i -> i.add(new ParameterMapping.Builder(configurationSupplier.get(),
+    public DialectModel setConsumer(boolean isFirstParam) {
+        if (isFirstParam) {
+            firstParamConsumer = i -> i.add(new ParameterMapping.Builder(configurationSupplier.get(),
                 OFFSET_NAME, long.class).build());
         } else {
-            limitConsumer = i -> i.add(new ParameterMapping.Builder(configurationSupplier.get(),
+            secondParamConsumer = i -> i.add(new ParameterMapping.Builder(configurationSupplier.get(),
                 LIMIT_NAME, long.class).build());
         }
         return this;
@@ -113,19 +110,16 @@ public class DialectModel {
         Assert.notNull(configuration, "configuration must not be null!");
         configurationSupplier = () -> configuration;
         mappingsSupplier = () -> mappings;
-        offsetConsumer.accept(mappings);
-        limitConsumer.accept(mappings);
+        firstParamConsumer.accept(mappings);
+        secondParamConsumer.accept(mappings);
     }
 
     /**
      * 存储 offset 和 limit
-     *
-     * @param offset 偏移量
-     * @param limit  范围量
      */
-    private void putToDialectMap(long offset, long limit) {
+    private void putToDialectMap(long firstParam, long secondParam) {
         dialectMap = new HashMap<>(2);
-        dialectMap.put(OFFSET_NAME, offset);
-        dialectMap.put(LIMIT_NAME, limit);
+        dialectMap.put(OFFSET_NAME, firstParam);
+        dialectMap.put(LIMIT_NAME, secondParam);
     }
 }

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

@@ -1,36 +1,40 @@
-///*
-// * 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 2016-01-23
-// */
-//public class OracleDialect implements IDialect {
-//
-//    @Override
-//    public String buildPaginationSql(String originalSql, long offset, long limit) {
-//        StringBuilder sql = new StringBuilder();
-//        sql.append("SELECT * FROM ( SELECT TMP.*, ROWNUM ROW_ID FROM ( ");
-//        sql.append(originalSql).append(" ) TMP WHERE ROWNUM <=").append((offset >= 1) ? (offset + limit) : limit);
-//        sql.append(") WHERE ROW_ID > ").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.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
+
+/**
+ * <p>
+ * ORACLE 数据库分页语句组装实现
+ * </p>
+ *
+ * @author hubin
+ * @since 2016-01-23
+ */
+public class OracleDialect implements IDialect {
+
+    @Override
+    public DialectModel buildPaginationSql(String originalSql, long offset, long limit) {
+        long firstParam = (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);
+    }
+}