ソースを参照

新增验证字段策略枚举类

jobob 8 年 前
コミット
60b96de9ef

+ 50 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/annotations/FieldStrategy.java

@@ -0,0 +1,50 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ *
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.annotations;
+
+/**
+ * <p>
+ * 字段策略枚举类
+ * </p>
+ * 
+ * @author hubin
+ * @Date 2015-11-10
+ */
+public enum FieldStrategy {
+	IGNORED(0, "忽略"),
+	NOT_NULL(1, "非 null"),
+	NOT_EMPTY(1, "非空");
+
+	/** 主键 */
+	private final int key;
+
+	/** 描述 */
+	private final String desc;
+
+	FieldStrategy(final int key, final String desc) {
+		this.key = key;
+		this.desc = desc;
+	}
+
+	public int getKey() {
+		return this.key;
+	}
+
+	public String getDesc() {
+		return this.desc;
+	}
+
+}

+ 18 - 6
mybatis-plus/src/main/java/com/baomidou/mybatisplus/annotations/TableField.java

@@ -31,27 +31,39 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.FIELD)
 public @interface TableField {
-	
+
 	/*
 	 * <p>
 	 * 字段值(驼峰命名方式,该值可无)
 	 * </p>
 	 */
 	String value() default "";
-	
+
+	/*
+	 * <p>
+	 * 当该Field为类对象时, 可使用#{对象.属性}来映射到数据表.
+	 * </p>
+	 */
+	String el() default "";
+
 	/*
 	 * <p>
 	 * 是否为数据库表字段
 	 * </p>
+	 * <p>
 	 * 默认 true 存在,false 不存在
-	 * 
+	 * </p>
 	 */
 	boolean exist() default true;
-
+	
 	/*
 	 * <p>
-	 * 当该Field为类对象时, 可使用#{对象.属性}来映射到数据表.
+	 * 字段验证
+	 * </p>
+	 * <p>
+	 * 默认 非 null 判断
 	 * </p>
 	 */
-	String el() default "";
+	FieldStrategy validate() default FieldStrategy.NOT_EMPTY;
+
 }

+ 16 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableFieldInfo.java

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.toolkit;
 
+import com.baomidou.mybatisplus.annotations.FieldStrategy;
+
 /**
  * <p>
  * 数据库表字段反射信息
@@ -48,6 +50,11 @@ public class TableFieldInfo {
 	 */
 	private String el;
 
+	/**
+	 * 字段策略
+	 */
+	private FieldStrategy fieldStrategy;
+
 	public TableFieldInfo(boolean related, String column, String property, String el) {
 		this.related = related;
 		this.setColumn(column);
@@ -100,4 +107,13 @@ public class TableFieldInfo {
 	public void setEl(String el) {
 		this.el = el;
 	}
+
+	public FieldStrategy getFieldStrategy() {
+		return fieldStrategy;
+	}
+
+	public void setFieldStrategy(FieldStrategy fieldStrategy) {
+		this.fieldStrategy = fieldStrategy;
+	}
+
 }

+ 65 - 45
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/PhoneNumber.java

@@ -1,65 +1,85 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ *
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.mysql.entity;
 
 /**
+ * <p>
+ * 测试手机号码
+ * </p>
+ * 
  * @author junyu
  * @Date 2016-09-09
  */
-
 public class PhoneNumber {
-    private String countryCode;
-    private String stateCode;
-    private String number;
+	private String countryCode;
+	private String stateCode;
+	private String number;
 
-    public PhoneNumber() {
-    }
+	public PhoneNumber() {
+	}
 
-    public PhoneNumber(String countryCode, String stateCode, String
-            number) {
-        this.countryCode = countryCode;
-        this.stateCode = stateCode;
-        this.number = number;
-    }
+	public PhoneNumber(String countryCode, String stateCode, String number) {
+		this.countryCode = countryCode;
+		this.stateCode = stateCode;
+		this.number = number;
+	}
 
-    public PhoneNumber(String string) {
-        if (string != null) {
-            String[] parts = string.split("-");
-            if (parts.length > 0) this.countryCode = parts[0];
-            if (parts.length > 1) this.stateCode = parts[1];
-            if (parts.length > 2) this.number = parts[2];
-        }
-    }
+	public PhoneNumber(String string) {
+		if (string != null) {
+			String[] parts = string.split("-");
+			if (parts.length > 0)
+				this.countryCode = parts[0];
+			if (parts.length > 1)
+				this.stateCode = parts[1];
+			if (parts.length > 2)
+				this.number = parts[2];
+		}
+	}
 
-    public String getAsString() {
-        return countryCode + "-" + stateCode + "-" + number;
+	public String getAsString() {
+		return countryCode + "-" + stateCode + "-" + number;
 
-    }
+	}
 
-    public String getCountryCode() {
-        return countryCode;
-    }
+	public String getCountryCode() {
+		return countryCode;
+	}
 
-    public void setCountryCode(String countryCode) {
-        this.countryCode = countryCode;
-    }
+	public void setCountryCode(String countryCode) {
+		this.countryCode = countryCode;
+	}
 
-    public String getStateCode() {
-        return stateCode;
-    }
+	public String getStateCode() {
+		return stateCode;
+	}
 
-    public void setStateCode(String stateCode) {
-        this.stateCode = stateCode;
-    }
+	public void setStateCode(String stateCode) {
+		this.stateCode = stateCode;
+	}
 
-    public String getNumber() {
-        return number;
-    }
+	public String getNumber() {
+		return number;
+	}
 
-    public void setNumber(String number) {
-        this.number = number;
-    }
+	public void setNumber(String number) {
+		this.number = number;
+	}
 
-    @Override
-    public String toString() {
-        return getAsString();
-    }
+	@Override
+	public String toString() {
+		return getAsString();
+	}
 }