筑巢网站建设,快看影视大全官方下载,vi应用设计,wordpress dota主题目录 自定义注解的定义和作用范围如何创建自定义注解创建注解接口 如何使用自定义注解进行数据验证创建注解处理器控制器中使用注解 如何为字段添加注解 自定义注解的定义和作用范围
自定义注解可以作用在类、方法、属性、参数、异常、字段或其他注解上。
如何创建自定义注解… 目录 自定义注解的定义和作用范围如何创建自定义注解创建注解接口 如何使用自定义注解进行数据验证创建注解处理器控制器中使用注解 如何为字段添加注解 自定义注解的定义和作用范围
自定义注解可以作用在类、方法、属性、参数、异常、字段或其他注解上。
如何创建自定义注解
创建注解接口 package hanshuhuan.test.anonotion;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 实体检验自定义注解类根据我们自定义的注解去检查实体各个字段是否在规定的值内* author shuhu**/
Target(ElementType.FIELD)
Retention(RetentionPolicy.RUNTIME)
public interface ValidateEntity {public boolean required() default false;//是否检验nullpublic boolean requiredLeng() default false;//是否检验长度public boolean requiredMaxValue() default false;//是否检验最大值public boolean requiredMinValue() default false;//是否检验最小值public int maxLength() default -1;//最大长度public int minLength() default -1;//最小长度public long maxValue() default -1;//最大值public long minValue() default -1;//最小值public String errorRequiredMsg() default ;//值为null时的错误提示信息public String errorMinLengthMsg() default ;//最小长度不满足时的提示信息public String errorMaxLengthMsg() default ;//最大长度不满足时的提示信息public String errorMinValueMsg() default ;//最小值不满足时的提示信息public String errorMaxValueMsg() default ;//最大值不满足时的提示信息
}如何使用自定义注解进行数据验证
创建注解处理器
package hanshuhuan.test.util;import java.lang.reflect.Field;import hanshuhuan.test.anonotion.ValidateEntity;
import hanshuhuan.test.bean.CodeMsg;/*** 验证实体工具类* author shuhu**/
public class ValidateEntityUtil {public static CodeMsg validate(Object object){Field[] declaredFields object.getClass().getDeclaredFields();for(Field field : declaredFields){ValidateEntity annotation field.getAnnotation(ValidateEntity.class);if(annotation ! null){if(annotation.required()){//表示该字段是必填字段field.setAccessible(true);try {Object o field.get(object);//首先判断是否为空if(o null){CodeMsg codeMsg CodeMsg.VALIDATE_ENTITY_ERROR;codeMsg.setMsg(annotation.errorRequiredMsg());return codeMsg;}//到这说明该变量的值不是null//首先判断是不是String类型if(o instanceof String){//若是字符串类型则检查其长度if(annotation.requiredLeng()){if(o.toString().length() annotation.minLength()){CodeMsg codeMsg CodeMsg.VALIDATE_ENTITY_ERROR;codeMsg.setMsg(annotation.errorMinLengthMsg());return codeMsg;}if(o.toString().length() annotation.maxLength()){CodeMsg codeMsg CodeMsg.VALIDATE_ENTITY_ERROR;codeMsg.setMsg(annotation.errorMaxLengthMsg());return codeMsg;}}}//其次来判断是否为数字if(isNumberObject(o)){//判断是否规定检查最小值if(annotation.requiredMinValue()){if(Double.valueOf(o.toString()) annotation.minValue()){CodeMsg codeMsg CodeMsg.VALIDATE_ENTITY_ERROR;codeMsg.setMsg(annotation.errorMinValueMsg());return codeMsg;}}//判断是否规定检查最大值if(annotation.requiredMaxValue()){if(Double.valueOf(o.toString()) annotation.maxValue()){CodeMsg codeMsg CodeMsg.VALIDATE_ENTITY_ERROR;codeMsg.setMsg(annotation.errorMaxValueMsg());return codeMsg;}}}} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return CodeMsg.SUCCESS;}/*** 检查对象是否是数字类型* param object* return*/public static boolean isNumberObject(Object object){if(object instanceof Integer)return true;if(object instanceof Long)return true;if(object instanceof Float)return true;if(object instanceof Double)return true;return false;}
}控制器中使用注解
RequestMapping(value/login,methodRequestMethod.POST)
ResponseBody
public ResultBoolean login(User user,String cpacha){if(user null){return Result.error(CodeMsg.DATA_ERROR);}//用统一验证实体方法验证是否合法CodeMsg validate ValidateEntityUtil.validate(user);if(validate.getCode() ! CodeMsg.SUCCESS.getCode()){return Result.error(validate);}//表示实体信息合法开始验证验证码是否为空if(StringUtils.isEmpty(cpacha)){return Result.error(CodeMsg.CPACHA_EMPTY);}log.info(okuser);return Result.success(true);
}如何为字段添加注解
自定义注解的定义是通过注解名的方式实现的。
ValidateEntity(requiredtrue,requiredLengtrue,minLength4,maxLength18,errorRequiredMsg用户名不能为空!,errorMinLengthMsg用户名长度需大于4!,errorMaxLengthMsg用户名长度不能大于18!)
Column(nameusername,nullablefalse,length18)
private String username;//用户名ValidateEntity(requiredtrue,requiredLengtrue,minLength4,maxLength32,errorRequiredMsg密码不能为空,errorMinLengthMsg密码长度需大于4!,errorMaxLengthMsg密码长度不能大于32!)
Column(namepassword,nullablefalse,length32)
private String password;//密码