Browse Source

Merge remote-tracking branch 'origin/dev' into dev

yuxiaobin 8 years ago
parent
commit
b88c8d6ffb

+ 5 - 17
src/main/java/com/baomidou/mybatisplus/annotations/TableField.java

@@ -15,13 +15,14 @@
  */
 package com.baomidou.mybatisplus.annotations;
 
+import com.baomidou.mybatisplus.enums.FieldIgnore;
+import com.baomidou.mybatisplus.enums.FieldStrategy;
+
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
-import com.baomidou.mybatisplus.enums.FieldStrategy;
-
 /**
  * <p>
  * 表字段标识
@@ -74,21 +75,8 @@ public @interface TableField {
 
     /**
      * <p>
-     * 是否插入忽略
-     * </p>
-     * <p>
-     * 默认为false,即不忽略
-     * </p>
-     */
-    boolean insertIgnore() default false;
-
-    /**
-     * <p>
-     * 是否更新忽略
-     * </p>
-     * <p>
-     * 默认为false,即不忽略
+     * 字段忽略策略
      * </p>
      */
-    boolean updateIgnore() default false;
+    FieldIgnore ignore() default FieldIgnore.DEFAULT;
 }

+ 10 - 23
src/main/java/com/baomidou/mybatisplus/entity/TableFieldInfo.java

@@ -15,14 +15,15 @@
  */
 package com.baomidou.mybatisplus.entity;
 
-import java.lang.reflect.Field;
-
 import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableLogic;
+import com.baomidou.mybatisplus.enums.FieldIgnore;
 import com.baomidou.mybatisplus.enums.FieldStrategy;
 import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
+import java.lang.reflect.Field;
+
 /**
  * <p>
  * 数据库表字段反射信息
@@ -76,14 +77,9 @@ public class TableFieldInfo {
     private String logicNotDeleteValue;
 
     /**
-     * 插入忽
+     * 字段忽略策
      */
-    private boolean insertIgnore = false;
-
-    /**
-     * 更新忽略
-     */
-    private boolean updateIgnore = false;
+    private FieldIgnore fieldIgnore = FieldIgnore.DEFAULT;
 
     /**
      * <p>
@@ -124,8 +120,7 @@ public class TableFieldInfo {
 		/*
 		 * 保存当前字段的插入忽略,更新忽略值
 		 */
-		this.insertIgnore = tableField.insertIgnore();
-        this.updateIgnore = tableField.updateIgnore();
+		this.fieldIgnore = tableField.ignore();
     }
 
     public TableFieldInfo(GlobalConfiguration globalConfig, TableInfo tableInfo, Field field) {
@@ -250,19 +245,11 @@ public class TableFieldInfo {
         this.logicNotDeleteValue = logicNotDeleteValue;
     }
 
-    public boolean isInsertIgnore() {
-        return insertIgnore;
-    }
-
-    public void setInsertIgnore(boolean insertIgnore) {
-        this.insertIgnore = insertIgnore;
-    }
-
-    public boolean isUpdateIgnore() {
-        return updateIgnore;
+    public FieldIgnore getFieldIgnore() {
+        return fieldIgnore;
     }
 
-    public void setUpdateIgnore(boolean updateIgnore) {
-        this.updateIgnore = updateIgnore;
+    public void setFieldIgnore(FieldIgnore fieldIgnore) {
+        this.fieldIgnore = fieldIgnore;
     }
 }

+ 65 - 0
src/main/java/com/baomidou/mybatisplus/enums/FieldIgnore.java

@@ -0,0 +1,65 @@
+/**
+ * Copyright (c) 2011-2014, 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.enums;
+
+/**
+ * <p>
+ * 字段忽略策略枚举类
+ * </p>
+ *
+ * @author hubin
+ * @Date 2016-09-09
+ */
+public enum FieldIgnore {
+    DEFAULT(0, "默认方式"),
+    INSERT(1, "忽略插入"),
+    UPDATE(2, "忽略更新"),
+    INSERT_AND_UPDATE(3, "忽略插入和更新");
+
+    /**
+     * 主键
+     */
+    private final int key;
+
+    /**
+     * 描述
+     */
+    private final String desc;
+
+    FieldIgnore(final int key, final String desc) {
+        this.key = key;
+        this.desc = desc;
+    }
+
+    public static FieldIgnore getIgnore(int key) {
+        FieldIgnore[] fss = FieldIgnore.values();
+        for (FieldIgnore fs : fss) {
+            if (fs.getKey() == key) {
+                return fs;
+            }
+        }
+        return FieldIgnore.DEFAULT;
+    }
+
+    public int getKey() {
+        return this.key;
+    }
+
+    public String getDesc() {
+        return this.desc;
+    }
+
+}

+ 9 - 3
src/main/java/com/baomidou/mybatisplus/enums/FieldStrategy.java

@@ -24,12 +24,18 @@ package com.baomidou.mybatisplus.enums;
  * @Date 2016-09-09
  */
 public enum FieldStrategy {
-    IGNORED(0, "ignored"), NOT_NULL(1, "not null"), NOT_EMPTY(2, "not empty");
+    IGNORED(0, "忽略判断"),
+    NOT_NULL(1, "非 NULL 判断"),
+    NOT_EMPTY(2, "非空判断");
 
-    /** 主键 */
+    /**
+     * 主键
+     */
     private final int key;
 
-    /** 描述 */
+    /**
+     * 描述
+     */
     private final String desc;
 
     FieldStrategy(final int key, final String desc) {

+ 5 - 2
src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import com.baomidou.mybatisplus.enums.FieldIgnore;
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
@@ -240,7 +241,8 @@ public class AutoSqlInjector implements ISqlInjector {
 
         for (TableFieldInfo fieldInfo : fieldList) {
             /* 判断是否插入忽略,插入忽略就不生成这个SQL */
-            if (fieldInfo.isInsertIgnore()) {
+            // TODO 忽略策略待完善
+            if (fieldInfo.getFieldIgnore() == FieldIgnore.INSERT) {
                 continue;
             }
             if (selective) {
@@ -532,7 +534,8 @@ public class AutoSqlInjector implements ISqlInjector {
         List<TableFieldInfo> fieldList = table.getFieldList();
         for (TableFieldInfo fieldInfo : fieldList) {
             /* 判断是不是更新忽略,是的话不生成此SQL */
-            if (fieldInfo.isUpdateIgnore()) {
+            // TODO 忽略策略待完善
+            if (fieldInfo.getFieldIgnore() == FieldIgnore.UPDATE) {
                 continue;
             }
             if (selective) {