前提:在 pom.xml 导入
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
1.客户端传入参数
public class ParkEnterpriseModel extends BaseModel {
@Min(value=1,message="id不能小于0")
private long businessesid;
@Min(value=1,message="id不能小于0")
private long enterpriseid;
@NotEmpty(message="名称不能为空")
private String name;
@Email(message = "邮件格式错误");
private String email;
}
2.响应客户端
public WebResponse<Boolean> set(@Valid ParkEnterpriseModel parkEnterpriseModel, BindingResult result) {
if (result.hasErrors()) {
List<ObjectError> errorList = result.getAllErrors();
for(ObjectError error : errorList){
System.out.println(error.getDefaultMessage());
return WebResponse.ERROR( ResultCode.SERVER_ERROR, error.getDefaultMessage() );
}
}
ParkEnterpriseModel parkModel = parkService.getByUuid( parkEnterpriseModel.getEnterpriseid() );
if (parkModel == null) {
int ret = parkService.insertSelective( parkEnterpriseModel );
if (ret == 0) {
return WebResponse.ERROR( ResultCode.SERVER_ERROR, "数据插入数据库失败" );
}
}
parkService.updateByPrimaryKeySelective( parkEnterpriseModel );
return WebResponse.SUCCESS(true);
}
网友评论