Browse Source

一波优化+测试用例

miemie 7 years ago
parent
commit
313bc90b37

+ 13 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -297,6 +297,19 @@ public class TableFieldInfo {
         return convertIf(sqlSet, prefix + property);
     }
 
+    /**
+     * 获取 查询的 sql 片段
+     *
+     * @param prefix 前缀
+     * @return sql 脚本片段
+     */
+    public String getSqlWhere(String prefix) {
+        prefix = StringUtils.isEmpty(prefix) ? StringPool.EMPTY : prefix;
+        // 默认:  AND column=#{prefix + el}
+        String sqlScript = " AND " + String.format(condition, column, prefix + el);
+        return convertIf(sqlScript, prefix + property);
+    }
+
     /**
      * 转换成 if 标签的脚本片段
      *

+ 61 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.KeySequence;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
+import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
@@ -255,14 +256,71 @@ public class TableInfo {
             .collect(joining(StringPool.NEWLINE));
     }
 
+    /**
+     * 获取所有的查询的 sql 片段
+     *
+     * @param ignoreLogicDelFiled 是否过滤掉逻辑删除字段
+     * @param withId              是否包含 id 项
+     * @param prefix              前缀
+     * @return sql 脚本片段
+     */
+    public String getAllSqlWhere(boolean ignoreLogicDelFiled, boolean withId, String prefix) {
+        String newPrefix = StringUtils.isEmpty(prefix) ? StringPool.EMPTY : prefix;
+        String filedSqlScript = fieldList.stream()
+            .filter(i -> {
+                if (ignoreLogicDelFiled) {
+                    return !(isLogicDelete() && i.isLogicDelete());
+                }
+                return true;
+            })
+            .map(i -> i.getSqlWhere(newPrefix)).collect(joining(StringPool.NEWLINE));
+        if (!withId) {
+            return filedSqlScript;
+        }
+        String newKeyProperty = newPrefix + keyProperty;
+        String keySqlScript = keyColumn + StringPool.EQUALS +
+            SqlScriptUtils.HASH_LEFT_BRACE + newKeyProperty + StringPool.RIGHT_BRACE;
+        return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty)) +
+            StringPool.NEWLINE + filedSqlScript;
+    }
+
     /**
      * 获取所有的 sql set 片段
      *
-     * @param prefix 前缀
+     * @param ignoreLogicDelFiled 是否过滤掉逻辑删除字段
+     * @param prefix              前缀
      * @return sql 脚本片段
      */
-    public String getAllSqlSet(String prefix) {
+    public String getAllSqlSet(boolean ignoreLogicDelFiled, String prefix) {
         String newPrefix = StringUtils.isEmpty(prefix) ? StringPool.EMPTY : prefix;
-        return fieldList.stream().map(i -> i.getSqlSet(newPrefix)).collect(joining(StringPool.NEWLINE));
+        return fieldList.stream()
+            .filter(i -> {
+                if (ignoreLogicDelFiled) {
+                    return !(isLogicDelete() && i.isLogicDelete());
+                }
+                return true;
+            })
+            .map(i -> i.getSqlSet(newPrefix)).collect(joining(StringPool.NEWLINE));
+    }
+
+    /**
+     * 获取逻辑删除字段的 sql 脚本
+     *
+     * @param startWithAnd 是否以 and 开头
+     * @return sql 脚本
+     */
+    public String getLogicDeleteSql(boolean startWithAnd) {
+        if (isLogicDelete()) {
+            TableFieldInfo fieldInfo = fieldList.stream().filter(TableFieldInfo::isLogicDelete).findFirst()
+                .orElseThrow(() -> ExceptionUtils.mpe(String.format("can't find the logicFiled from table {%s}", tableName)));
+            String formatStr = fieldInfo.isCharSequence() ? "'%s'" : "%s";
+            String logicDeleteSql = fieldInfo.getColumn() + StringPool.EQUALS +
+                String.format(formatStr, fieldInfo.getLogicNotDeleteValue());
+            if (startWithAnd) {
+                logicDeleteSql = " AND " + logicDeleteSql;
+            }
+            return logicDeleteSql;
+        }
+        return "";
     }
 }

+ 12 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlScriptUtils.java

@@ -79,4 +79,16 @@ public final class SqlScriptUtils {
         return sb.append(StringPool.RIGHT_CHEV).append(StringPool.NEWLINE).append(sqlScript)
             .append(StringPool.NEWLINE).append("</trim>").append(StringPool.NEWLINE).toString();
     }
+
+    /**
+     * 生成 choose 标签的脚本
+     *
+     * @param whenTest  when 内 test 的内容
+     * @param otherwise otherwise 内容
+     * @return choose 脚本
+     */
+    public static String convertChoose(String whenTest, String otherwise) {
+        return StringPool.NEWLINE + "<when test=\"" + whenTest + StringPool.QUOTE + StringPool.RIGHT_CHEV +
+            "</when>" + StringPool.NEWLINE + "<otherwise>" + otherwise + "</otherwise>" + "</choose>";
+    }
 }

File diff suppressed because it is too large
+ 4 - 8
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/EncryptTest.java


+ 14 - 9
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/SampleTest.java

@@ -1,21 +1,26 @@
 package com.baomidou.mybatisplus.test;
 
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
+import com.baomidou.mybatisplus.test.base.entity.LogicTestData;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import org.junit.Assert;
 import org.junit.Test;
 
 public class SampleTest {
 
     private final ObjectMapper mapper = new ObjectMapper();
 
+
     @Test
-    public void testPageJsonDecode() throws Exception {
-        String json = "{\"current\":2,\"pages\":1222,\"total\":10,\"size\":5,\"ascs\":[\"name\",\"age\",\"qiuqiu\"]}";
-        Page page = mapper.readValue(json, Page.class);
-        Assert.assertEquals(2, page.getCurrent());
-        Assert.assertEquals(5, page.getSize());
-        Assert.assertEquals(3, page.ascs().length);
-        Assert.assertEquals(2, page.getPages());
+    public void testTableInfoHelper2() {
+        TableInfo info = TableInfoHelper.initTableInfo(null, LogicTestData.class);
+//        System.out.println("----------- AllInsertSqlColumn -----------");
+//        System.out.println(info.getAllInsertSqlColumn());
+//        System.out.println("----------- AllInsertSqlProperty -----------");
+//        System.out.println(info.getAllInsertSqlProperty());
+        System.out.println("----------- AllSqlSet -----------");
+        System.out.println(info.getAllSqlSet(true, "ew.entity."));
+        System.out.println("----------- AllSqlWhere -----------");
+        System.out.println(info.getAllSqlWhere(false, true, "ew.entity."));
     }
 }

+ 5 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -85,6 +85,11 @@ public class MysqlTestDataMapperTest {
         logicMapper.deleteByMap(map);
     }
 
+    @Test
+    public void other() {
+        logicMapper.update(new LogicTestData().setDeleted(true), null);
+    }
+
     @Test
     public void delete() {
         mapper.delete(new QueryWrapper<TestData>().lambda()

Some files were not shown because too many files changed in this diff