diff --git a/len-core/pom.xml b/len-core/pom.xml index 7c9edf9..4d1bad7 100644 --- a/len-core/pom.xml +++ b/len-core/pom.xml @@ -26,5 +26,6 @@ 3.8.1 test + diff --git a/len-core/src/main/java/com/len/validator/ValidatorUtils.java b/len-core/src/main/java/com/len/validator/ValidatorUtils.java new file mode 100644 index 0000000..7d532a3 --- /dev/null +++ b/len-core/src/main/java/com/len/validator/ValidatorUtils.java @@ -0,0 +1,37 @@ +package com.len.validator; +import com.len.exception.MyException; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; + +/** + * + * @ClassName: ValidatorUtils + * @Description: hibernate-validator校验工具类 + * @author: liutao + * @date: 2018年3月16日 上午11:56:34 + */ +public class ValidatorUtils { + private static Validator validator; + static { + validator = Validation.buildDefaultValidatorFactory().getValidator(); + } + /** + * 校验对象 + * @param object 待校验对象 + * @param groups 待校验的组 + * @throws MyException 校验不通过,则报BusinessException异常 + */ + public static void validateEntity(Object object, Class>... groups) + throws MyException { + Set> constraintViolations = validator.validate(object, groups); + if (!constraintViolations.isEmpty()) { + ConstraintViolation constraint = + (ConstraintViolation)constraintViolations.iterator().next(); + throw new MyException(constraint.getMessage()); + } + } +} diff --git a/len-core/src/main/java/com/len/validator/group/AddGroup.java b/len-core/src/main/java/com/len/validator/group/AddGroup.java new file mode 100644 index 0000000..10316fc --- /dev/null +++ b/len-core/src/main/java/com/len/validator/group/AddGroup.java @@ -0,0 +1,11 @@ +package com.len.validator.group; + +/** + * + * @ClassName: AddGroup + * @Description: 新增数据 Group + * @author: liutao + * @date: 2018年3月16日 下午12:15:23 + */ +public interface AddGroup { +} diff --git a/len-core/src/main/java/com/len/validator/group/Group.java b/len-core/src/main/java/com/len/validator/group/Group.java new file mode 100644 index 0000000..3b92508 --- /dev/null +++ b/len-core/src/main/java/com/len/validator/group/Group.java @@ -0,0 +1,15 @@ +package com.len.validator.group; + +import javax.validation.GroupSequence; + +/** + * + * @ClassName: Group + * @Description: 定义校验顺序,如果AddGroup组失败,则UpdateGroup组不会再校验 + * @author: liutao + * @date: 2018年3月16日 下午2:29:21 + */ +@GroupSequence({AddGroup.class, UpdateGroup.class}) +public interface Group { + +} diff --git a/len-core/src/main/java/com/len/validator/group/UpdateGroup.java b/len-core/src/main/java/com/len/validator/group/UpdateGroup.java new file mode 100644 index 0000000..e7f965d --- /dev/null +++ b/len-core/src/main/java/com/len/validator/group/UpdateGroup.java @@ -0,0 +1,12 @@ +package com.len.validator.group; + +/** + * + * @ClassName: UpdateGroup + * @Description: 更新数据 Group + * @author: liutao + * @date: 2018年3月16日 下午2:29:34 + */ +public interface UpdateGroup { + +} diff --git a/len-sys/src/main/java/com/len/entity/SysMenu.java b/len-sys/src/main/java/com/len/entity/SysMenu.java index bb5f4e4..797059f 100644 --- a/len-sys/src/main/java/com/len/entity/SysMenu.java +++ b/len-sys/src/main/java/com/len/entity/SysMenu.java @@ -4,10 +4,16 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; + +import com.len.validator.group.AddGroup; +import com.len.validator.group.UpdateGroup; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.ToString; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotEmpty; @Getter @Setter @@ -16,12 +22,14 @@ import lombok.ToString; public class SysMenu implements Serializable { private String id; + @NotEmpty(message = "菜单名称不能为空", groups = {AddGroup.class, UpdateGroup.class}) private String name; private String pId; private String url; + @Length(min = 1,max = 4, message = "序号长度不对") private Integer orderNum; private String icon; diff --git a/len-sys/src/main/resources/ftl/system/menu/add-menu.ftl b/len-sys/src/main/resources/ftl/system/menu/add-menu.ftl index 537ec9a..068a37b 100644 --- a/len-sys/src/main/resources/ftl/system/menu/add-menu.ftl +++ b/len-sys/src/main/resources/ftl/system/menu/add-menu.ftl @@ -103,7 +103,7 @@ To change this template use File | Settings | File Templates.--> + autocomplete="off" class="layui-input" maxlength="4"> diff --git a/len-web/src/main/resources/application-dev.yml b/len-web/src/main/resources/application-dev.yml index 97d5c6e..45ecd78 100644 --- a/len-web/src/main/resources/application-dev.yml +++ b/len-web/src/main/resources/application-dev.yml @@ -1,12 +1,12 @@ server: - port: 8082 + port: 8072 spring: datasource: url: jdbc:mysql://localhost:3306/lenos?useUnicode=true&characterEncoding=UTF-8 username: root # password: 123456 # password: 1234 - password: 123456 + password: l123456 # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver diff --git a/pom.xml b/pom.xml index 81abb49..0ef78d8 100644 --- a/pom.xml +++ b/pom.xml @@ -293,11 +293,21 @@ quartz 2.3.0 - - cn.hutool - hutool-core - 4.0.10 - + + cn.hutool + hutool-core + 4.0.12 + + + org.hibernate.validator + hibernate-validator + 6.0.10.Final + + + javax.validation + validation-api + 2.0.1.Final +