SpringBoot构建电商基础秒杀项目 学习笔记

修改 UserModel

添加注解

public class UserModel {

    private Integer id;
@NotBlank(message = "用户名不能为空")
private String name;
@NotNull(message = "性别不能不填写")
private Byte gender;
@NotNull(message = "年龄不能不填写")
@Min(value = 0, message = "年龄必须大于0")
@Max(value = 150, message = "年龄必须小于 150 岁")
private Integer age;
@NotBlank(message = "手机号不能为空")
private String telphone;
private String registerMode;
private String thirdPartyId; @NotBlank(message = "密码不能为空")
private String encrptPassword;
}

添加 ValidationResult

public class ValidationResult {

    private boolean hasErrors = false;

    private Map<String, String> errorMsgMap = new HashMap<>();

    public boolean isHasErrors() {
return hasErrors;
} public void setHasErrors(boolean hasErrors) {
this.hasErrors = hasErrors;
} public Map<String, String> getErrorMsgMap() {
return errorMsgMap;
} public void setErrorMsgMap(Map<String, String> errorMsgMap) {
this.errorMsgMap = errorMsgMap;
} public String getErrMsg(){ return StringUtils.join(errorMsgMap.values().toArray(), ",");
}
}

添加 ValidatorImpl

@Component
public class ValidatorImpl implements InitializingBean { private Validator validator; public ValidationResult validate(Object bean){
final 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.getErrorMsgMap().put(propertyName, errMsg);
});
} return result;
} @Override
public void afterPropertiesSet() throws Exception { // 将 hibernate validator 通过工厂的初始化方式使其实例化
this.validator = Validation.buildDefaultValidatorFactory().getValidator();
}
}

UserServiceImpl 修改

    @Autowired
private ValidatorImpl validator; @Override
@Transactional // 事务操作
public void register(UserModel userModel) throws BusinessException {
if(userModel == null){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
} ValidationResult result = validator.validate(userModel);
if(result.isHasErrors()){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
} UserDO userDO = convertFromModel(userModel);
try{
userDOMapper.insertSelective(userDO);
}catch (DuplicateKeyException ex){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "手机号码已注册");
} userModel.setId(userDO.getId()); // 获取自增 id UserPasswordDO userPasswordDO = convertPasswordFromModel(userModel);
userPasswordDOMapper.insertSelective(userPasswordDO); return;
}

源码:spring-boot-seckill

Spring Boot 构建电商基础秒杀项目 (七) 自动校验的更多相关文章

  1. Spring Boot 构建电商基础秒杀项目 (一) 项目搭建

    SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...

  2. Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)

    SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...

  3. Spring Boot 构建电商基础秒杀项目 (十一) 秒杀

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...

  4. Spring Boot 构建电商基础秒杀项目 (十) 交易下单

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...

  5. Spring Boot 构建电商基础秒杀项目 (九) 商品列表 & 详情

    SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...

  6. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...

  7. Spring Boot 构建电商基础秒杀项目 (六) 用户登陆

    SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...

  8. Spring Boot 构建电商基础秒杀项目 (五) 用户注册

    SpringBoot构建电商基础秒杀项目 学习笔记 UserService 添加 void register(UserModel userModel) throws BusinessException ...

  9. Spring Boot 构建电商基础秒杀项目 (四) getotp 页面

    SpringBoot构建电商基础秒杀项目 学习笔记 BaseController 添加 public static final String CONTENT_TYPE_FORMED = "a ...

随机推荐

  1. 高精度除法(b为int类型)

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; ],a[],b ...

  2. Java多线程(三)—— synchronized关键字详解

    一.多线程的同步 1.为什么要引入同步机制 在多线程环境中,可能会有两个甚至更多的线程试图同时访问一个有限的资源.必须对这种潜在资源冲突进行预防. 解决方法:在线程使用一个资源时为其加锁即可. 访问资 ...

  3. Java IO(三)——字节流

    一.流类 Java的流式输入/输出是建立在四个抽象类的基础上的:InputStream.OutputStream.Reader.Writer.它们用来创建具体的流式子类.尽管程序通过具体子类执行输入/ ...

  4. CF429E Points and Segments 构造、欧拉回路

    传送门 如果把一条线段\([l,r]\)看成一条无向边\((l,r+1)\),从\(l\)走到\(r+1\)表示线段\([l,r]\)染成红色,从\(r+1\)走到\(l\)表示线段\([l,r]\) ...

  5. ZooKeeper: 简介, 配置及运维指南

    1. 概览 ZooKeeper是一个供其它分布式应用程序使用的软件, 它为其它分布式应用程序提供所谓的协调服务. 所谓的协调服务, 是指ZooKeeper的如下能力 naming 命名 configu ...

  6. .NetCore 2.1中的HttpClientFactory最佳实践

    .NET Core 2.1中的HttpClientFactory最佳实践 ASP.NET Core 2.1中出现一个新的HttpClientFactory功能, 它有助于解决开发人员在使用HttpCl ...

  7. Jenkins - 构建Allure Report

    前言 本文为Pytest+Allure定制报告进阶篇,集成Jenkins,在Jenkins中直接生成报告,更方便测试人员查看. 一.安装插件allure-jenkins-plugin 1.进入系统管理 ...

  8. H5上传图片之canvas

    H5上传图片之canvas,使用canvas处理压缩图片再上传 html代码: <form action="" method="post"> < ...

  9. 容器化-Docker实战

    导读:本文系统性介绍Docker安装.Docker组件.Docker命令.Dockerfile语法和Docker应用,通过上述介绍使我们已经对docker基本操作有一定了解. 一.前言 本文将系统性的 ...

  10. 一个出色的表格(React实现__ES5语法)

    本文主要是<React快速上手开发>一书中,第三章的内容代码整理,因为书中的代码零零散散,所以自己将整理了一下. 排序和编辑功能 <script> var header = [ ...