Prechádzať zdrojové kódy

[修改] 简化MP自定义SQL使用方法,现在可以使用 `自定义sql` + ${ew.customSqlSegment} 方式

Caratacus 6 rokov pred
rodič
commit
995b545794

+ 14 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -44,6 +44,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiPredicate;
 import java.util.function.Function;
@@ -475,13 +476,20 @@ public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, T
     }
 
     @Override
-    public String getCustomSql() {
-        NormalSegmentList normal = getExpression().getNormal();
-        if (normal.isEmpty()) {
-            return getSqlSegment();
-        } else {
-            return concatWhere(getSqlSegment());
+    public String getCustomSqlSegment() {
+        MergeSegments expression = getExpression();
+        if (Objects.nonNull(expression)) {
+            NormalSegmentList normal = expression.getNormal();
+            String sqlSegment = getSqlSegment();
+            if (StringUtils.isNotEmpty(sqlSegment)) {
+                if (normal.isEmpty()) {
+                    return sqlSegment;
+                } else {
+                    return concatWhere(sqlSegment);
+                }
+            }
         }
+        return StringUtils.EMPTY;
     }
 
     /**

+ 3 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

@@ -67,13 +67,13 @@ public abstract class Wrapper<T> implements ISqlSegment {
     /**
      * 获取自定义SQL 简化自定义XML复杂情况
      * 使用方法
-     * `自定义sql` + #{ew.customSql}
+     * `自定义sql` + ${ew.customSqlSegment}
      * <p>1.逻辑删除需要自己拼接条件 (之前自定义也同样)</p>
      * <p>2.不支持wrapper中附带实体的情况 (wrapper自带实体会更麻烦)</p>
-     * <p>3.用法 #{ew.customSql} (不需要where标签包裹,切记!)</>
+     * <p>3.用法 ${ew.customSqlSegment} (不需要where标签包裹,切记!)</>
      * <p>4.ew是wrapper定义别名,可自行替换</>
      */
-    public abstract String getCustomSql();
+    public abstract String getCustomSqlSegment();
 
     /**
      * 查询条件为空(包含entity)

+ 11 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/base/mapper/mysql/MysqlDataMapper.java

@@ -1,5 +1,13 @@
 package com.baomidou.mybatisplus.test.base.mapper.mysql;
 
+import java.util.List;
+
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.ResultType;
+import org.apache.ibatis.annotations.Select;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.test.base.entity.mysql.MysqlData;
 import com.baomidou.mybatisplus.test.base.mapper.MyBaseMapper;
 
@@ -9,4 +17,7 @@ import com.baomidou.mybatisplus.test.base.mapper.MyBaseMapper;
  */
 public interface MysqlDataMapper extends MyBaseMapper<MysqlData> {
 
+    @ResultType(MysqlData.class)
+    @Select("select * from mysql_data ${ew.customSql}")
+    List<MysqlData> getAll(@Param(Constants.WRAPPER) Wrapper wrapper);
 }

+ 30 - 11
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -1,5 +1,22 @@
 package com.baomidou.mybatisplus.test.mysql;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.Resource;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.MethodSorters;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -16,17 +33,6 @@ import com.baomidou.mybatisplus.test.base.mapper.commons.CommonDataMapper;
 import com.baomidou.mybatisplus.test.base.mapper.commons.CommonLogicDataMapper;
 import com.baomidou.mybatisplus.test.base.mapper.mysql.MysqlDataMapper;
 import com.baomidou.mybatisplus.test.mysql.config.MysqlDb;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.FixMethodOrder;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.MethodSorters;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-import javax.annotation.Resource;
-import java.util.*;
 
 
 /**
@@ -349,4 +355,17 @@ public class MysqlTestDataMapperTest {
         commonMapper.selectList(Wrappers.lambdaQuery(new CommonData().setTestInt(12)).orderByAsc(CommonData::getCreateDatetime));
         commonLogicMapper.selectList(Wrappers.lambdaQuery(new CommonLogicData().setTestInt(12)).orderByAsc(CommonLogicData::getCreateDatetime));
     }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void d11_testWrapperCustomSql() {
+        // 1. 只有 order by 或者 last
+        mysqlMapper.getAll(Wrappers.<MysqlData>query().lambda().orderByDesc(MysqlData::getOrder).last("limit 1"));
+        // 2. 什么都没有情况
+        mysqlMapper.getAll(Wrappers.emptyWrapper());
+        // 3. 只有 where 条件
+        mysqlMapper.getAll(Wrappers.lambdaQuery(new MysqlData()).eq(MysqlData::getGroup, 1));
+        // 4. 有 where 条件 也有 last 条件
+        mysqlMapper.getAll(Wrappers.lambdaQuery(new MysqlData()).eq(MysqlData::getGroup, 1).last("limit 1"));
+    }
 }