Browse Source

优化获取 sqlSet

miemie 6 years ago
parent
commit
9da45ac173

+ 5 - 4
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/IdType.java

@@ -32,13 +32,14 @@ public enum IdType {
      */
      */
     AUTO(0),
     AUTO(0),
     /**
     /**
-     * 用户输入ID
+     * 该类型为未设置主键类型
      */
      */
-    INPUT(1),
+    NONE(1),
     /**
     /**
-     * 该类型为未设置主键类型
+     * 用户输入ID
+     * 该类型可以通过自己注册自动填充插件进行填充
      */
      */
-    NONE(2),
+    INPUT(2),
 
 
     /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
     /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
     /**
     /**

+ 35 - 15
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -17,14 +17,13 @@ package com.baomidou.mybatisplus.core.metadata;
 
 
 import com.baomidou.mybatisplus.annotation.*;
 import com.baomidou.mybatisplus.annotation.*;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
 import lombok.AccessLevel;
 import lombok.AccessLevel;
-import lombok.Data;
 import lombok.Getter;
 import lombok.Getter;
-import lombok.Setter;
-import lombok.experimental.Accessors;
 
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Field;
 
 
@@ -36,8 +35,7 @@ import java.lang.reflect.Field;
  * @author hubin sjy willenfoo tantan
  * @author hubin sjy willenfoo tantan
  * @since 2016-09-09
  * @since 2016-09-09
  */
  */
-@Data
-@Accessors(chain = true)
+@Getter
 public class TableFieldInfo {
 public class TableFieldInfo {
 
 
     /**
     /**
@@ -66,6 +64,10 @@ public class TableFieldInfo {
      * 属性类型
      * 属性类型
      */
      */
     private Class<?> propertyType;
     private Class<?> propertyType;
+    /**
+     * 属性是否是 CharSequence 类型
+     */
+    private boolean isCharSequence;
     /**
     /**
      * 字段策略【 默认,自判断 null 】
      * 字段策略【 默认,自判断 null 】
      */
      */
@@ -97,7 +99,6 @@ public class TableFieldInfo {
     /**
     /**
      * 缓存 sql select
      * 缓存 sql select
      */
      */
-    @Setter(AccessLevel.NONE)
     @Getter(AccessLevel.NONE)
     @Getter(AccessLevel.NONE)
     private String sqlSelect;
     private String sqlSelect;
 
 
@@ -110,6 +111,7 @@ public class TableFieldInfo {
                           String column, String el, TableField tableField) {
                           String column, String el, TableField tableField) {
         this.property = field.getName();
         this.property = field.getName();
         this.propertyType = field.getType();
         this.propertyType = field.getType();
+        this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.fieldFill = tableField.fill();
         this.fieldFill = tableField.fill();
         this.clazz = field.getDeclaringClass();
         this.clazz = field.getDeclaringClass();
         this.update = tableField.update();
         this.update = tableField.update();
@@ -152,9 +154,10 @@ public class TableFieldInfo {
      */
      */
     public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field) {
     public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field) {
         this.property = field.getName();
         this.property = field.getName();
+        this.propertyType = field.getType();
+        this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.el = field.getName();
         this.el = field.getName();
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.fieldStrategy = dbConfig.getFieldStrategy();
-        this.propertyType = field.getType();
         this.setCondition(dbConfig);
         this.setCondition(dbConfig);
         this.clazz = field.getDeclaringClass();
         this.clazz = field.getDeclaringClass();
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
@@ -203,10 +206,6 @@ public class TableFieldInfo {
         return false;
         return false;
     }
     }
 
 
-    public boolean isRelated() {
-        return related;
-    }
-
     /**
     /**
      * 是否开启逻辑删除
      * 是否开启逻辑删除
      */
      */
@@ -218,10 +217,10 @@ public class TableFieldInfo {
      * 全局配置开启字段 LIKE 并且为字符串类型字段
      * 全局配置开启字段 LIKE 并且为字符串类型字段
      * 注入 LIKE 查询!!!
      * 注入 LIKE 查询!!!
      */
      */
-    public void setCondition(GlobalConfig.DbConfig dbConfig) {
-        if (null == this.condition || SqlCondition.EQUAL.equals(this.condition)) {
-            if (dbConfig.isColumnLike() && StringUtils.isCharSequence(this.propertyType)) {
-                this.condition = dbConfig.getDbType().getLike();
+    private void setCondition(GlobalConfig.DbConfig dbConfig) {
+        if (null == condition || SqlCondition.EQUAL.equals(condition)) {
+            if (dbConfig.isColumnLike() && isCharSequence) {
+                condition = dbConfig.getDbType().getLike();
             }
             }
         }
         }
     }
     }
@@ -244,4 +243,25 @@ public class TableFieldInfo {
         }
         }
         return sqlSelect;
         return sqlSelect;
     }
     }
+
+    /**
+     * 获取 set sql 片段
+     *
+     * @param isInsert 是 insert sql 否
+     * @param prefix   前缀
+     * @return sql 脚本片段
+     */
+    public String getSqlSet(boolean isInsert, String prefix) {
+        prefix = StringUtils.isEmpty(prefix) ? StringPool.EMPTY : prefix;
+        // 默认判断 update 语句
+        boolean existIf = fieldFill != FieldFill.UPDATE && fieldFill != FieldFill.INSERT_UPDATE;
+        if (isInsert) {
+            existIf = fieldFill != FieldFill.INSERT && fieldFill != FieldFill.INSERT_UPDATE;
+        }
+        String sqlSet = column + StringPool.EQUALS + "#{" + prefix + el + "}" + StringPool.COMMA;
+        if (existIf) {
+            sqlSet = SqlScriptUtils.convertIf(sqlSet, prefix + property, isCharSequence, fieldStrategy);
+        }
+        return sqlSet;
+    }
 }
 }

+ 33 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -189,4 +189,37 @@ public class TableInfo {
         }
         }
         return allSqlSelect;
         return allSqlSelect;
     }
     }
+
+    /**
+     * 获取主键的 sql set 片段
+     *
+     * @param prefix 前缀
+     * @return sql 脚本片段
+     */
+    public String getSqlSet(String prefix) {
+        if (StringUtils.isNotEmpty(keyProperty)) {
+            return keyColumn + StringPool.EQUALS + "#{" + prefix + keyProperty + "}" + StringPool.COMMA;
+        }
+        return "";
+    }
+
+    /**
+     * 获取所有的 sql set 片段
+     *
+     * @param isInsert 是否 insert 语句
+     * @param prefix   前缀
+     * @return sql 脚本片段
+     */
+    public String getAllSqlSet(boolean isInsert, String prefix) {
+        String newPrefix = StringUtils.isEmpty(prefix) ? StringPool.EMPTY : prefix;
+        String allSqlSet = fieldList.stream().map(i -> i.getSqlSet(isInsert, newPrefix)).collect(joining(""));
+        if (!isInsert) {
+            return allSqlSet;
+        }
+        if (idType.getKey() > 1) {
+            // 可带有填充功能
+            allSqlSet = getSqlSet(newPrefix) + allSqlSet;
+        }
+        return allSqlSet;
+    }
 }
 }

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlHelper.java

@@ -37,7 +37,7 @@ import java.util.List;
  * @author hubin
  * @author hubin
  * @since 2016-11-06
  * @since 2016-11-06
  */
  */
-public class SqlHelper {
+public final class SqlHelper {
 
 
     private static final Log logger = LogFactory.getLog(SqlHelper.class);
     private static final Log logger = LogFactory.getLog(SqlHelper.class);
     public static SqlSessionFactory FACTORY;
     public static SqlSessionFactory FACTORY;

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

@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.core.toolkit.sql;
+
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+
+/**
+ * <p>
+ * sql 脚本工具类
+ * </p>
+ *
+ * @author miemie
+ * @since 2018-08-15
+ */
+public final class SqlScriptUtils {
+
+    /**
+     * <p>
+     * 获取 带 if 标签的 sql
+     * </p>
+     *
+     * @param sqlSet         set sql 片段
+     * @param property       entity 属性
+     * @param isCharSequence 是 CharSequence 类型否
+     * @param fieldStrategy  验证逻辑
+     * @return if 脚本
+     */
+    public static String convertIf(String sqlSet, String property, boolean isCharSequence, FieldStrategy fieldStrategy) {
+        if (fieldStrategy == FieldStrategy.IGNORED) {
+            return sqlSet;
+        }
+        if (fieldStrategy == FieldStrategy.NOT_NULL) {
+            return String.format("<if test=\"%s != null\">%s</if>", property, sqlSet);
+        }
+        if (isCharSequence) {
+            return String.format("<if test=\"%s != null and %s != ''\">%s</if>", property, property, sqlSet);
+        }
+        return String.format("<if test=\"%s != null\">%s</if>", property, sqlSet);
+    }
+}

+ 3 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/EncryptTest.java

@@ -28,12 +28,13 @@ public class EncryptTest {
 
 
     @Test
     @Test
     public void testTableInfoHelper() {
     public void testTableInfoHelper() {
-        TableInfo tableInfo = TableInfoHelper.initTableInfo(null, Xx.class);
-        System.out.println(tableInfo);
+        TableInfo info = TableInfoHelper.initTableInfo(null, Xx.class);
+        System.out.println(info.getAllSqlSet(false, null));
     }
     }
 
 
     @Data
     @Data
     public static class Xx {
     public static class Xx {
+        private Long id;
         @TableField(exist = false)
         @TableField(exist = false)
         private String x1;
         private String x1;
         private String x2;
         private String x2;