增加Hibernate-validator数据校验
This commit is contained in:
parent
50570d7d1f
commit
e753e3c689
|
@ -26,5 +26,6 @@
|
|||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -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<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
|
||||
if (!constraintViolations.isEmpty()) {
|
||||
ConstraintViolation<Object> constraint =
|
||||
(ConstraintViolation<Object>)constraintViolations.iterator().next();
|
||||
throw new MyException(constraint.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -103,7 +103,7 @@ To change this template use File | Settings | File Templates.-->
|
|||
</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="orderNum" id="orderNum" name="orderNum" lay-verify="orderNum"
|
||||
autocomplete="off" class="layui-input">
|
||||
autocomplete="off" class="layui-input" maxlength="4">
|
||||
</div>
|
||||
</div>
|
||||
<div style="height: 60px"></div>
|
||||
|
|
|
@ -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
|
||||
|
|
20
pom.xml
20
pom.xml
|
@ -293,11 +293,21 @@
|
|||
<artifactId>quartz</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>4.0.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>4.0.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.10.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.1.Final</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue