浏览代码

Merge branch '3.0' into github3.0

hubin 3 年之前
父节点
当前提交
f23d3575c5

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## [v3.4.4] 2022.01.01
 
 - 升级 mybatis 3.5.8
+- 升级 jsqlparser 4.3
 - 添加动态表名的钩子函数 https://github.com/baomidou/mybatis-plus/pull/3965
 - 注入类 DefaultSqlInjector 优化调整
 - 反射类 ReflectionKit 优化 field -> field 改为 Function.identity()

+ 1 - 1
build.gradle

@@ -16,7 +16,7 @@ ext {
         mybatisSpringBootStarterVersion = '2.2.0',
         springVersion = '5.3.9',
         springBootVersion = '2.5.3',
-        jsqlparserVersion = '4.2',
+        jsqlparserVersion = '4.3',
         junitVersion = '5.7.2',
     ]
 

+ 7 - 13
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/PaginationInnerInterceptor.java

@@ -30,7 +30,6 @@ import lombok.NoArgsConstructor;
 import net.sf.jsqlparser.JSQLParserException;
 import net.sf.jsqlparser.expression.Alias;
 import net.sf.jsqlparser.expression.Expression;
-import net.sf.jsqlparser.expression.Function;
 import net.sf.jsqlparser.parser.CCJSqlParserUtil;
 import net.sf.jsqlparser.schema.Column;
 import net.sf.jsqlparser.schema.Table;
@@ -64,20 +63,15 @@ import java.util.stream.Collectors;
 @NoArgsConstructor
 @SuppressWarnings({"rawtypes"})
 public class PaginationInnerInterceptor implements InnerInterceptor {
-
-    protected static final List<SelectItem> COUNT_SELECT_ITEM = Collections.singletonList(defaultCountSelectItem());
-    protected static final Map<String, MappedStatement> countMsCache = new ConcurrentHashMap<>();
-    protected final Log logger = LogFactory.getLog(this.getClass());
-
     /**
      * 获取jsqlparser中count的SelectItem
      */
-    private static SelectItem defaultCountSelectItem() {
-        Function function = new Function();
-        function.setName("COUNT");
-        function.setAllColumns(true);
-        return new SelectExpressionItem(function).withAlias(new Alias("total"));
-    }
+    protected static final List<SelectItem> COUNT_SELECT_ITEM = Collections.singletonList(
+        new SelectExpressionItem(new Column().withColumnName("COUNT(*)")).withAlias(new Alias("total"))
+    );
+    protected static final Map<String, MappedStatement> countMsCache = new ConcurrentHashMap<>();
+    protected final Log logger = LogFactory.getLog(this.getClass());
+
 
     /**
      * 溢出总页数后是否进行处理
@@ -270,7 +264,7 @@ public class PaginationInnerInterceptor implements InnerInterceptor {
             Select select = (Select) CCJSqlParserUtil.parse(sql);
             SelectBody selectBody = select.getSelectBody();
             // https://github.com/baomidou/mybatis-plus/issues/3920  分页增加union语法支持
-            if(selectBody instanceof SetOperationList) {
+            if (selectBody instanceof SetOperationList) {
                 return lowLevelCountSql(sql);
             }
             PlainSelect plainSelect = (PlainSelect) select.getSelectBody();

+ 12 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.test.h2.entity.H2User;
 import com.baomidou.mybatisplus.test.h2.enums.AgeEnum;
 import com.baomidou.mybatisplus.test.h2.service.IH2UserService;
@@ -530,7 +531,7 @@ class H2UserTest extends BaseTest {
             .eq(H2User::getPrice, 2)
             .getTargetSql();
     }
-    
+
     @Test
     void testRemove() {
         //不报错即可,无需关注返回值
@@ -545,5 +546,14 @@ class H2UserTest extends BaseTest {
         userService.removeById(h2User);
         userService.removeByIds(Arrays.asList((short) 100, 100, 100.00, (float) 100, 10000L, new BigDecimal("100"), h2User));
     }
-    
+
+    @Test
+    void testPageOrderBy() {
+        // test https://gitee.com/baomidou/mybatis-plus/issues/I4BGE2
+        Page page = Page.of(1, 10);
+        Assertions.assertEquals(1, userService.page(page, Wrappers.<H2User>query().select("test_id,name")
+            .orderByDesc("test_id")).getPages());
+        Assertions.assertEquals(1, userService.page(page, Wrappers.<H2User>lambdaQuery()
+            .orderByDesc(H2User::getTestId)).getPages());
+    }
 }