Java Spring中的唯一列验证程序
我想添加一个验证器,如果值不唯一,它将返回错误。如何做到这一点?这是我当前的验证器:Java Spring中的唯一列验证程序,java,spring,unique,validation,Java,Spring,Unique,Validation,我想添加一个验证器,如果值不唯一,它将返回错误。如何做到这一点?这是我当前的验证器: @Component public class AddFormValidator implements Validator { public boolean supports(Class<?> clazz) { return AddForm.class.isAssignableFrom(clazz); } public void validate(Objec
@Component
public class AddFormValidator implements Validator {
public boolean supports(Class<?> clazz) {
return AddForm.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
AddForm addForm = (AddForm) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title",
"title.empty", "Title must not be empty.");
String title = addForm.getTitle();
if ((title.length()) > 30) {
errors.rejectValue("title", "title.tooLong",
"Title must not more than 16 characters.");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content",
"content.empty", "Content must not be empty.");
String content = addForm.getContent();
if ((content.length()) > 10000) {
errors.rejectValue("content", "content.tooLong",
"Content must not more than 10K characters.");
}
}
@组件
公共类AddFormValidator实现验证器{
公共布尔支持(类clazz){
返回AddForm.class.isAssignableFrom(clazz);
}
公共无效验证(对象目标、错误){
AddForm AddForm=(AddForm)目标;
ValidationUtils.RejectIfEmptyYorWhiteSpace(错误,“标题”,
“title.empty”,“title不能为空。”);
字符串title=addForm.getTitle();
如果((title.length())>30){
errors.rejectValue(“title”、“title.tooLong”,
“标题不得超过16个字符。”);
}
ValidationUtils.RejectIfEmptyYorWhiteSpace(错误,“内容”,
“content.empty”,“内容不能为空。”);
String content=addForm.getContent();
如果((content.length())>10000){
errors.rejectValue(“content”、“content.tooLong”,
“内容不得超过10K个字符。”);
}
}
我想验证标题。我不知道您是如何进行数据库访问的,您可能应该插入一个查询数据库的存储库,以检查标题是否已经存在。例如:
@Component
public class AddFormValidator implements Validator {
@Autowired
NewsRepository newsRepository;
public boolean supports(Class<?> clazz) {
return AddForm.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
AddForm addForm = (AddForm) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title",
"title.empty", "Title must not be empty.");
String title = addForm.getTitle();
if ((title.length()) > 30) {
errors.rejectValue("title", "title.tooLong",
"Title must not more than 16 characters.");
}
New new = newsRepository.findByTitle(title);
// New already exist
if (new != null) {
errors.rejectValue("title", "title.alreadyExist",
"New title already exist");
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content",
"content.empty", "Content must not be empty.");
String content = addForm.getContent();
if ((content.length()) > 10000) {
errors.rejectValue("content", "content.tooLong",
"Content must not more than 10K characters.");
}
}
}
@组件
公共类AddFormValidator实现验证器{
@自动连线
新闻储存库;
公共布尔支持(类clazz){
返回AddForm.class.isAssignableFrom(clazz);
}
公共无效验证(对象目标、错误){
AddForm AddForm=(AddForm)目标;
ValidationUtils.RejectIfEmptyYorWhiteSpace(错误,“标题”,
“title.empty”,“title不能为空。”);
字符串title=addForm.getTitle();
如果((title.length())>30){
errors.rejectValue(“title”、“title.tooLong”,
“标题不得超过16个字符。”);
}
New=newsRepository.findbytle(标题);
//已经存在新的
如果(新!=null){
errors.rejectValue(“title”、“title.alreadyExist”,
“新名称已存在”);
}
ValidationUtils.RejectIfEmptyYorWhiteSpace(错误,“内容”,
“content.empty”,“内容不能为空。”);
String content=addForm.getContent();
如果((content.length())>10000){
errors.rejectValue(“content”、“content.tooLong”,
“内容不得超过10K个字符。”);
}
}
}
如果值不是唯一的,那么what?unique关于what?Title列的值必须是新闻表中唯一的。不能有标题相等的新闻。这是危险的。因为检查以及随后对已验证对象执行的任何存储库操作将(可能)未在同一事务中完成。在您检查后,标题可能立即存在。这是一个最简单的示例。他当然必须添加一些内容才能处理并发性,但如何添加?我不知道这取决于您的用例…我不知道在验证对象后您正在对其执行什么操作。您使用JPA吗?