Browse Source

feat: rename selectStrategy to whereStrategy, add test case

yuxiaobin 6 years ago
parent
commit
3d7f80ad44

+ 14 - 5
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableField.java

@@ -67,25 +67,34 @@ public @interface TableField {
      * 字段验证策略
      * 字段验证策略
      * <p>默认追随全局配置</p>
      * <p>默认追随全局配置</p>
      *
      *
-     * @deprecated 3.1.2 please use {@link #insertStrategy} and {@link #updateStrategy} and {@link #selectStrategy}
+     * @deprecated 3.1.2 please use {@link #insertStrategy} and {@link #updateStrategy} and {@link #whereStrategy}
      */
      */
     @Deprecated
     @Deprecated
     FieldStrategy strategy() default FieldStrategy.DEFAULT;
     FieldStrategy strategy() default FieldStrategy.DEFAULT;
 
 
     /**
     /**
-     * 字段验证策略之 insert
+     * 字段验证策略之 insert: 当insert操作时,该字段拼接insert语句时的策略
+     * IGNORED: 直接拼接 insert into table_a(column) values (#{columnProperty});
+     * NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
+     * NOT_EMPTY: insert into table_a(<if test="columnProperty != null and columnProperty!=''">column</if>) values (<if test="columnProperty != null and columnProperty!=''">#{columnProperty}</if>)
      */
      */
     FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
     FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
 
 
     /**
     /**
-     * 字段验证策略之 update
+     * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
+     * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
+     * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if>
+     * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
      */
      */
     FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;
     FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;
 
 
     /**
     /**
-     * 字段验证策略之 select
+     * 字段验证策略之 where: 表示该字段在拼接where条件时的策略
+     * IGNORED: 直接拼接 column=#{columnProperty}
+     * NOT_NULL: <if test="columnProperty != null">column=#{columnProperty}</if>
+     * NOT_EMPTY: <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
      */
      */
-    FieldStrategy selectStrategy() default FieldStrategy.DEFAULT;
+    FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;
 
 
     /**
     /**
      * 字段自动填充策略
      * 字段自动填充策略

+ 21 - 12
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -15,20 +15,26 @@
  */
  */
 package com.baomidou.mybatisplus.core.metadata;
 package com.baomidou.mybatisplus.core.metadata;
 
 
-import com.baomidou.mybatisplus.annotation.*;
+import java.lang.reflect.Field;
+import java.util.Optional;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.annotation.SqlCondition;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.Version;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 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.SqlScriptUtils;
+
 import lombok.AccessLevel;
 import lombok.AccessLevel;
 import lombok.EqualsAndHashCode;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.ToString;
 import lombok.ToString;
 
 
-import java.lang.reflect.Field;
-import java.util.Optional;
-
 /**
 /**
  * 数据库表字段反射信息
  * 数据库表字段反射信息
  *
  *
@@ -68,22 +74,25 @@ public class TableFieldInfo implements Constants {
     /**
     /**
      * 字段策略【 默认,自判断 null 】
      * 字段策略【 默认,自判断 null 】
      *
      *
-     * @deprecated 3.1.2 please use {@link #insertStrategy} and {@link #updateStrategy} and {@link #selectStrategy}
+     * @deprecated 3.1.2 please use {@link #insertStrategy} and {@link #updateStrategy} and {@link #whereStrategy}
      */
      */
     @Deprecated
     @Deprecated
     private final FieldStrategy fieldStrategy;
     private final FieldStrategy fieldStrategy;
     /**
     /**
      * 字段验证策略之 insert
      * 字段验证策略之 insert
+     * Refer to {@link TableField#insertStrategy()}
      */
      */
     private final FieldStrategy insertStrategy;
     private final FieldStrategy insertStrategy;
     /**
     /**
      * 字段验证策略之 update
      * 字段验证策略之 update
+     * Refer to {@link TableField#updateStrategy()}
      */
      */
     private final FieldStrategy updateStrategy;
     private final FieldStrategy updateStrategy;
     /**
     /**
-     * 字段验证策略之 select
+     * 字段验证策略之 where
+     * Refer to {@link TableField#whereStrategy()}
      */
      */
-    private final FieldStrategy selectStrategy;
+    private final FieldStrategy whereStrategy;
     /**
     /**
      * 是否进行 select 查询
      * 是否进行 select 查询
      * <p>大字段可设置为 false 不加入 select 查询范围</p>
      * <p>大字段可设置为 false 不加入 select 查询范围</p>
@@ -168,10 +177,10 @@ public class TableFieldInfo implements Constants {
         /*
         /*
          * 优先使用单个字段注解,否则使用全局配置
          * 优先使用单个字段注解,否则使用全局配置
          */
          */
-        if (tableField.selectStrategy() == FieldStrategy.DEFAULT) {
-            this.selectStrategy = dbConfig.getSelectStrategy();
+        if (tableField.whereStrategy() == FieldStrategy.DEFAULT) {
+            this.whereStrategy = dbConfig.getSelectStrategy();
         } else {
         } else {
-            this.selectStrategy = tableField.selectStrategy();
+            this.whereStrategy = tableField.whereStrategy();
         }
         }
 
 
         if (StringUtils.isNotEmpty(tableField.condition())) {
         if (StringUtils.isNotEmpty(tableField.condition())) {
@@ -198,7 +207,7 @@ public class TableFieldInfo implements Constants {
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();
-        this.selectStrategy = dbConfig.getSelectStrategy();
+        this.whereStrategy = dbConfig.getSelectStrategy();
         this.setCondition(dbConfig);
         this.setCondition(dbConfig);
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
 
 
@@ -390,7 +399,7 @@ public class TableFieldInfo implements Constants {
         // 默认:  AND column=#{prefix + el}
         // 默认:  AND column=#{prefix + el}
         String sqlScript = " AND " + String.format(condition, column, newPrefix + el);
         String sqlScript = " AND " + String.format(condition, column, newPrefix + el);
         // 查询的时候只判非空
         // 查询的时候只判非空
-        return convertIf(sqlScript, newPrefix + property, selectStrategy);
+        return convertIf(sqlScript, newPrefix + property, whereStrategy);
     }
     }
 
 
     /**
     /**

+ 71 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserStrategyTest.java

@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2011-2019, 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>
+ * https://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.test.h2;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.test.h2.entity.H2UserStrategy;
+import com.baomidou.mybatisplus.test.h2.mapper.H2UserStrategyMapper;
+
+/**
+ * Mybatis Plus H2 Junit Test
+ *
+ * @author Caratacus
+ * @since 2017/4/1
+ */
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(locations = {"classpath:h2/spring-test-h2.xml"})
+class H2UserStrategyTest extends BaseTest {
+
+    @Autowired
+    protected H2UserStrategyMapper userStrategyMapper;
+
+    @Test
+    @Order(1)
+    void testNewlyAdded3Strategy() {
+        H2UserStrategy insertUser = new H2UserStrategy();
+        insertUser.setName("userStrategy").setDesc("updateStrategy=IGNORE").setVersion(1);
+        int row = userStrategyMapper.insert(insertUser);
+        Long id = insertUser.getTestId();
+        Assertions.assertEquals(1, row);
+        Assertions.assertEquals(3, userStrategyMapper.selectById(id).getTestType(),"autofilled with 3");
+
+        QueryWrapper<H2UserStrategy> wrapper = new QueryWrapper<>(new H2UserStrategy().setDesc("updateStrategy=IGNORE").setTestType(3));
+        Assertions.assertEquals(0, userStrategyMapper.selectCount(wrapper), "name is whereStrategy=IGNORE, so should have where name=null which cause count=0");
+
+        H2UserStrategy updateUser = new H2UserStrategy().setName("update");
+        updateUser.setTestId(id);
+        Assertions.assertEquals(1, userStrategyMapper.updateById(updateUser));
+
+        H2UserStrategy selectUser = userStrategyMapper.selectById(id);
+        Assertions.assertEquals(selectUser.getName(), "update");
+        Assertions.assertNull(selectUser.getDesc(), "desc is updateStrategy=IGNORE, so should have set desc=null when updateById");
+        Assertions.assertNull(selectUser.getTestType(), "handle: strategy=IGNORED, should be set test_type=null when updateById ");
+
+    }
+
+
+}

+ 140 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/H2UserStrategy.java

@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (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>
+ * https://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.test.h2.entity;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.Version;
+import com.baomidou.mybatisplus.test.h2.enums.AgeEnum;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+/**
+ * 测试用户类
+ *
+ * @author hubin sjy
+ */
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
+@TableName("h2user")
+@EqualsAndHashCode(callSuper = true)
+public class H2UserStrategy extends SuperEntity {
+
+    /**
+	 * serialVersionUID
+	 */
+	private static final long serialVersionUID = 2043176352335589747L;
+
+    /**
+     * whereStrategy = FieldStrategy.IGNORED 在拼接where条件时是在带上该条件
+     */
+    @TableField(value = "name", whereStrategy = FieldStrategy.IGNORED)
+    private String name;
+
+    private AgeEnum age;
+
+    /*BigDecimal 测试*/
+    private BigDecimal price;
+
+    /* 测试下划线字段命名类型, 字段填充 */
+    @TableField(strategy = FieldStrategy.IGNORED, fill = FieldFill.INSERT)
+    private Integer testType;
+
+    /**
+     * 转义关键字测试
+     * @since 2019-5-7 测试updateStrategy
+     */
+    @TableField(value = "`desc`", updateStrategy = FieldStrategy.IGNORED)
+    private String desc;
+
+    /**
+     * 该注解 select 默认不注入 select 查询
+     */
+    @TableField(select = false)
+    private Date testDate;
+
+    @Version
+    private Integer version;
+
+    @TableLogic
+    private Integer deleted;
+
+
+    public H2UserStrategy() {
+
+    }
+
+    public H2UserStrategy(String name) {
+        this.name = name;
+    }
+
+    public H2UserStrategy(Integer testType) {
+        this.testType = testType;
+    }
+
+    public H2UserStrategy(String name, AgeEnum age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public H2UserStrategy(Long id, String name) {
+        this.setTestId(id);
+        this.name = name;
+    }
+
+    public H2UserStrategy(Long id, AgeEnum age) {
+        this.setTestId(id);
+        this.age = age;
+    }
+
+    public H2UserStrategy(Long id, String name, AgeEnum age, Integer testType) {
+        this.setTestId(id);
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2UserStrategy(String name, AgeEnum age, Integer testType) {
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2UserStrategy(String name, Integer deleted) {
+        this.name = name;
+        this.deleted = deleted;
+    }
+
+    @Override
+    public String toString() {
+        return "h2user:{name=" + name + "," +
+            "age=" + age + "," +
+            "price=" + price + "," +
+            "testType=" + testType + "," +
+            "desc=" + desc + "," +
+            "testDate=" + testDate + "," +
+            "version=" + version;
+    }
+}

+ 28 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/mapper/H2UserStrategyMapper.java

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (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>
+ * https://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.test.h2.mapper;
+
+import com.baomidou.mybatisplus.test.h2.entity.H2UserStrategy;
+
+/**
+ * 这里继承自定义父类 SuperMapper
+ *
+ * @author Caratacus
+ * @since 2017/4/1
+ */
+public interface H2UserStrategyMapper extends SuperMapper<H2UserStrategy> {
+
+}