mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-10-10 01:35:14 +08:00
添加查询书籍;BookDO映射文件更新;DOMapper添加@Repository注解;添加自定义业务异常处理;添加Validation验证
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package plus.bookshelf.Common.Enum;
|
||||
|
||||
public enum Language {
|
||||
SIMPLIFIED_CHINESE(1000, "简体中文"),
|
||||
ENGLISH(1001, "English"),
|
||||
TRADITIONAL_CHINESE(1001, "繁体中文");
|
||||
|
||||
private Language(Integer langId, String langName) {
|
||||
this.langId = langId;
|
||||
this.langName = langName;
|
||||
}
|
||||
|
||||
private Integer langId;
|
||||
private String langName;
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package plus.bookshelf.Common.Error;
|
||||
|
||||
public enum BusinessErrorCode implements CommonError {
|
||||
// 通用错误类型 10001
|
||||
PARAMETER_VALIDATION_ERROR(10001, "参数不合法"),
|
||||
UNKNOWN_ERROR(10002, "未知错误"),
|
||||
|
||||
// 20000开头为用户信息相关错误定义
|
||||
USER_NOT_EXIST(20001, "用户不存在"),
|
||||
USER_LOGIN_FAILED(20002, "用户手机号或密码不正确"),
|
||||
USER_NOT_LOGIN(20003, "用户还未登录");
|
||||
|
||||
|
||||
private BusinessErrorCode(int errCode, String errMsg) {
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg;
|
||||
}
|
||||
|
||||
private int errCode;
|
||||
private String errMsg;
|
||||
|
||||
@Override
|
||||
public int getErrCode() {
|
||||
return this.errCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrMsg() {
|
||||
return this.errMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonError setErrMsg(String errMsg) {
|
||||
this.errCode = errCode;
|
||||
this.errMsg = errMsg;
|
||||
return this;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package plus.bookshelf.Common.Error;
|
||||
|
||||
/**
|
||||
* 包装器业务异常类实现
|
||||
*/
|
||||
public class BusinessException extends Exception implements CommonError {
|
||||
|
||||
private CommonError commonError;
|
||||
|
||||
public BusinessException(CommonError commonError) {
|
||||
super();
|
||||
this.commonError = commonError;
|
||||
}
|
||||
|
||||
// 接收自定义errMsg的方式构造业务异常
|
||||
public BusinessException(CommonError commonError,String errMsg){
|
||||
super();
|
||||
this.commonError=commonError;
|
||||
this.commonError.setErrMsg(errMsg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getErrCode() {
|
||||
return this.commonError.getErrCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrMsg() {
|
||||
return this.commonError.getErrMsg();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonError setErrMsg(String errMsg) {
|
||||
this.commonError.setErrMsg(errMsg);
|
||||
return this;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package plus.bookshelf.Common.Error;
|
||||
|
||||
public interface CommonError {
|
||||
public int getErrCode();
|
||||
public String getErrMsg();
|
||||
public CommonError setErrMsg(String errMsg);
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package plus.bookshelf.Common.Validator;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ValidationResult {
|
||||
// 校验结果是否有错
|
||||
private boolean hasErrors = false;
|
||||
|
||||
// 存放错误信息的Map
|
||||
private Map<String, String> errMsgMap = new HashMap<>();
|
||||
|
||||
// 实现通用的通过格式化字符串信息获取错误结果的msg方法
|
||||
public String getErrMsg() {
|
||||
return StringUtils.join(errMsgMap.values().toArray(), ",");
|
||||
}
|
||||
|
||||
public boolean isHasErrors() {
|
||||
return hasErrors;
|
||||
}
|
||||
|
||||
public void setHasErrors(boolean hasErrors) {
|
||||
this.hasErrors = hasErrors;
|
||||
}
|
||||
|
||||
public Map<String, String> getErrMsgMap() {
|
||||
return errMsgMap;
|
||||
}
|
||||
|
||||
public void setErrMsgMap(Map<String, String> errMsgMap) {
|
||||
this.errMsgMap = errMsgMap;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package plus.bookshelf.Common.Validator;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class ValidatorImpl implements InitializingBean {
|
||||
|
||||
private Validator validator;
|
||||
|
||||
// 实现校验方法并返回校验结果
|
||||
public ValidationResult validate(Object bean) {
|
||||
ValidationResult result = new ValidationResult();
|
||||
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(bean);
|
||||
if (constraintViolationSet.size() > 0) {
|
||||
// 有错误
|
||||
result.setHasErrors(true);
|
||||
constraintViolationSet.forEach(constraintViolation -> {
|
||||
String errMsg = constraintViolation.getMessage();
|
||||
String propertyName = constraintViolation.getPropertyPath().toString();
|
||||
result.getErrMsgMap().put(propertyName, errMsg);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// 将Hibernate Validator通过工厂的初始化方式使其实例化
|
||||
this.validator = (Validator) Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user