Prechádzať zdrojové kódy

去除1=1这么难?

Caratacus 6 rokov pred
rodič
commit
ceb6da727d

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

@@ -106,14 +106,10 @@ public abstract class Wrapper<T> implements ISqlSegment {
         if (tableInfo == null) {
             return false;
         }
-        if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
-            // 有主键
-            return Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) &&
-                tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e));
-        } else {
-            // 无主键
-            return tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e));
+        if (tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e))) {
+            return true;
         }
+        return StringUtils.isNotEmpty(tableInfo.getKeyProperty()) ? Objects.nonNull(ReflectionKit.getMethodValue(entity, tableInfo.getKeyProperty())) : false;
     }
 
     /**

+ 3 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -190,7 +190,7 @@ public abstract class AbstractMethod implements Constants {
         String sqlScript = table.getAllSqlWhere(false, true, WRAPPER_ENTITY_DOT);
         sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true);
         sqlScript += NEWLINE;
-        sqlScript += SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT),
+        sqlScript += SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", "ew.nonEmptyOfEntity and ew.nonEmptyOfNormal", false) + " ${%s}", WRAPPER_SQLSEGMENT),
             String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
                 WRAPPER_NONEMPTYOFWHERE), true);
         sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
@@ -279,4 +279,6 @@ public abstract class AbstractMethod implements Constants {
      * @return MappedStatement
      */
     public abstract MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo);
+
+
 }

+ 13 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -234,8 +234,8 @@ public class MysqlTestDataMapperTest {
     @Test
     @SuppressWarnings("unchecked")
     public void d7_1_selectListForNoLogic() {
-        // 1. 只有 entity
         MysqlData data = new MysqlData().setOrder(1);
+        // 1. 只有 entity
         Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(data))));
         // 2. 有 entity 也有 where 条件
         Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(data).eq(MysqlData::getGroup, 1))));
@@ -247,6 +247,12 @@ public class MysqlTestDataMapperTest {
         // 5. 只有 order by 或者 last
         Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.<MysqlData>query()
             .lambda().orderByDesc(MysqlData::getOrder).last("limit 1"))));
+        // 6. 什么都没有情况
+        Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.emptyWrapper())));
+        // 7. 只有 where 条件
+        Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(new MysqlData()).eq(MysqlData::getGroup, 1))));
+        // 8. 有 where 条件 也有 last 条件
+        Assert.assertTrue(CollectionUtils.isNotEmpty(mysqlMapper.selectList(Wrappers.query(new MysqlData()).eq(MysqlData::getGroup, 1).last("limit 1"))));
     }
 
     @Test
@@ -265,6 +271,12 @@ public class MysqlTestDataMapperTest {
         // 5. 只有 order by 或者 last
         Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.<CommonLogicData>query()
             .lambda().orderByAsc(CommonLogicData::getTestInt).last("limit 1"))));
+        // 6. 什么都没有情况
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.emptyWrapper())));
+        // 7. 只有 where 条件
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.query(new CommonLogicData()).eq(CommonLogicData::getId, 11))));
+        // 8. 有 where 条件 也有 last 条件
+        Assert.assertTrue(CollectionUtils.isNotEmpty(commonLogicMapper.selectList(Wrappers.query(new CommonLogicData()).eq(CommonLogicData::getId, 11).last("limit 1"))));
     }
 
     @Test