miemie 6 months ago
parent
commit
73a8efd982

+ 21 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -30,11 +30,7 @@ import org.apache.ibatis.mapping.ResultMapping;
 import org.apache.ibatis.reflection.Reflector;
 import org.apache.ibatis.session.Configuration;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Predicate;
 
@@ -272,6 +268,26 @@ public class TableInfo implements Constants {
         return sqlSelect;
     }
 
+    /**
+     * 获取需要进行查询的 select sql 片段
+     *
+     * @param predicate 过滤条件
+     * @return sql 片段
+     */
+    public String chooseSelect(Predicate<TableFieldInfo> predicate, List<String> noSelectProperty) {
+        if (CollectionUtils.isEmpty(noSelectProperty)) {
+            return chooseSelect(predicate);
+        }
+        String fieldsSqlSelect = fieldList.stream().filter(predicate)
+            .filter(i -> !noSelectProperty.contains(i.getProperty()))
+            .map(TableFieldInfo::getSqlSelect).collect(joining(COMMA));
+        if (!havePK() || noSelectProperty.contains(keyProperty)) {
+            return fieldsSqlSelect;
+        } else {
+            return getKeySqlSelect() + COMMA + fieldsSqlSelect;
+        }
+    }
+
     /**
      * 获取 insert 时候主键 sql 脚本片段
      * <p>insert into table (字段) values (值)</p>

+ 10 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlUtils.java

@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -36,8 +37,8 @@ import java.util.regex.Pattern;
  * @since 2016-11-13
  */
 public abstract class SqlUtils implements Constants {
-
-    private static final Pattern pattern = Pattern.compile("\\{@((\\w+?)|(\\w+?:\\w+?)|(\\w+?:\\w+?:\\w+?))}");
+    private static final String tp = "[\\w-,]+?";
+    private static final Pattern pattern = Pattern.compile(String.format("\\{@((%s)|(%s:\\w+?)|(%s:\\w+?:\\w+?))}", tp, tp, tp));
 
     /**
      * 用%连接like
@@ -92,9 +93,15 @@ public abstract class SqlUtils implements Constants {
 
     @SuppressWarnings("all")
     public static String getSelectBody(String tableName, String alisa, String asAlisa, String escapeSymbol) {
+        int notSel = tableName.indexOf("-");
+        List<String> notSelColl = null;
+        if (notSel > 0) {
+            notSelColl = Arrays.asList(tableName.substring(notSel + 1).split(COMMA));
+            tableName = tableName.substring(0, notSel);
+        }
         TableInfo tableInfo = TableInfoHelper.getTableInfo(tableName);
         Assert.notNull(tableInfo, "can not find TableInfo Cache by \"%s\"", tableName);
-        String s = tableInfo.chooseSelect(TableFieldInfo::isSelect);
+        String s = tableInfo.chooseSelect(TableFieldInfo::isSelect, notSelColl);
         if (alisa == null) {
             return s;
         }

+ 3 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlUtilsTest.java

@@ -14,8 +14,9 @@ class SqlUtilsTest {
 
     @Test
     void m1() {
-        List<String> list = SqlUtils.findPlaceholder("select {@table},{@table:t},{@table:t:r} from table");
-        assertThat(list).contains("{@table}", "{@table:t}", "{@table:t:r}");
+        List<String> list = SqlUtils.findPlaceholder("select {@table},{@table-id,name},{@table:t},{@table-id,name:t}," +
+            "{@table:t:r},{@table-id,name:t:r}, from table");
+        assertThat(list).contains("{@table}", "{@table-id,name}", "{@table:t}", "{@table-id,name:t}", "{@table:t:r}", "{@table-id,name:t:r}");
     }
 
     @Test

+ 2 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/replaceplaceholder/Entity.java

@@ -18,6 +18,8 @@ public class Entity implements Serializable {
     @TableField("`name`")
     private String name;
 
+    private Integer age;
+
     @TableField(exist = false)
     private EntitySub es;
 

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/replaceplaceholder/EntityMapper.java

@@ -11,9 +11,9 @@ import java.util.List;
  */
 public interface EntityMapper extends BaseMapper<Entity> {
 
-    @Select("select {@entity} from entity")
+    @Select("select {@entity-name,id} from entity")
     List<Entity> selectAll();
 
-    @Select("select {@entity:e:es} from entity e")
+    @Select("select {@entity:e},{@entity_sub-id:es:es} from entity e left join entity_sub es on e.id = es.id")
     List<Entity> selectAll2();
 }

+ 1 - 10
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/replaceplaceholder/EntitySubMapper.java

@@ -1,19 +1,10 @@
 package com.baomidou.mybatisplus.test.replaceplaceholder;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import org.apache.ibatis.annotations.Select;
-
-import java.util.List;
 
 /**
  * @author miemie
  * @since 2020-06-23
  */
-public interface EntitySubMapper extends BaseMapper<Entity> {
-
-    @Select("select {@entity} from entity")
-    List<Entity> selectAll();
-
-    @Select("select {@entity:e} from entity e")
-    List<Entity> selectAll2();
+public interface EntitySubMapper extends BaseMapper<Entity.EntitySub> {
 }

+ 13 - 7
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/replaceplaceholder/ReplacePlaceholderTest.java

@@ -21,9 +21,14 @@ public class ReplacePlaceholderTest extends BaseDbTest<EntityMapper> {
     @Test
     void replace() {
         doTest(i -> {
-            System.out.println(i.selectAll());
-            List<Entity> list = i.selectAll2();
+            List<Entity> list = i.selectAll();
             System.out.println(list);
+            assertThat(list.getFirst().getId()).isNull();
+            assertThat(list.getFirst().getName()).isNull();
+            assertThat(list.getFirst().getAge()).isNotNull();
+            list = i.selectAll2();
+            System.out.println(list);
+            assertThat(list.getFirst().getEs().getId()).isNull();
             assertThat(list.getFirst().getEs().getName()).isNotBlank();
         });
     }
@@ -45,17 +50,18 @@ public class ReplacePlaceholderTest extends BaseDbTest<EntityMapper> {
 
     @Override
     protected String tableDataSql() {
-        return "insert into entity(id,name) values(1,'1'),(2,'2');" +
+        return "insert into entity(id,name,age) values(1,'1',1),(2,'2',2);" +
             "insert into entity_sub(id,name) values(1,'1'),(2,'2');";
     }
 
     @Override
     protected List<String> tableSql() {
-        return Arrays.asList("drop table if exists entity","drop table if exists entity_sub",
+        return Arrays.asList("drop table if exists entity", "drop table if exists entity_sub",
             "CREATE TABLE IF NOT EXISTS entity (" +
-            "id BIGINT NOT NULL," +
-            "name VARCHAR(30) NULL DEFAULT NULL," +
-            "PRIMARY KEY (id))",
+                "id BIGINT NOT NULL," +
+                "name VARCHAR(30) NULL DEFAULT NULL," +
+                "age int NULL DEFAULT NULL," +
+                "PRIMARY KEY (id))",
             "CREATE TABLE IF NOT EXISTS entity_sub (" +
                 "id BIGINT NOT NULL," +
                 "name VARCHAR(30) NULL DEFAULT NULL," +