注册的逻辑:
首先通过getotp页面获取otp短信验证码,该部分的逻辑和代码实现在之前已经讲过,此处则省略。
然后跳转到注册页面,填写手机号和获取到的验证码,再填写其他信息后点击提交注册,这些数据通过前端的ajax请求传递到controller层,它的处理是,获取到前端传递的参数,进行校验,主要是判空和手机号与验证码是否匹配的校验,校验通过后再将前端传递的数据封装到Usermodel模型数据中,调用service的注册方法时作为参数,传递给dao层,它的处理是,先判断usermodel是否为空,若为空则返回错误码和错误信息,若正确则进一步调用校验类validator的validation方法去判断前端传递的参数是否符合usermodel模型数据中定义的变量的规则。
校验通过后,再将controller层的模型数据转化为dao层的模型数据。转换完成后调用 userDOMapper.insertSelective(userDO)方法,
将这个dao层的模型数据(前端传递来的数据)通过参数传递给数据库,,数据库通过userDOMapper.xml中的sql语句把前端用户的参数插入到数据库表中。
注意,由于数据库中每个表都有id,而用户在填写登陆信息时填写这个字段的,因此,需要在sql语句中添加属性设置这个id是自增的:
keyProperty=“id” useGeneratedKeys=“true”
数据库操作完成后,返回到dao层,若是插入失败,可以在抱一个用户已存在的异常。
同时需要将数据库表的id手动设置到usermodel中。
在dao层还需要去处理密码字段,因为用户填写前端数据包含了,而在数据库密码单独放在了一张表中,因此登陆时将用户信息插入到数据表时也需要将数据的密码信息单独用一条sql语句插入到数据密码表中。
首先需要先将模型数据usermodel转换成密码的dao层模型数据,再调用userPasswordDOMapper.insertSelective(userPasswordDO);将转换后的dao层密码数据作为参数传递到数据库,,,,
数据库中插入密码与插入用户基本信息一致。
到此,service层的处理完成,返回到controller层,在这层中,调用通用的返回类CommonReturnType中的create()方法返回,由于登陆只需将数据插入到数据库中,因此没有返回值。
前端通过后台传递回来的数据,判断后台返回的结果状态是成功还是失败,若是成功,则跳转到详情页面,若是失败,则获取错误信息,反馈给用户。
前端页面与代码:
前台注册页面的代码:
<body class="login">
<div class="content">
<h3 class="form-title">用户注册</h3>
<div class="form-group">
<label class="control-label">手机号</label>
<div>
<input class="form-control" type="text" placeholder="手机号" name="telephone" id="telephone">
</div>
</div>
<div class="form-group">
<label class="control-label">验证码</label>
<div>
<input class="form-control" type="text" placeholder="验证码" name="otpCode" id="otpCode">
</div>
</div>
<div class="form-group">
<label class="control-label">用户昵称</label>
<div>
<input class="form-control" type="text" placeholder="用户昵称" name="name" id="name">
</div>
</div>
<div class="form-group">
<label class="control-label">性别</label>
<div>
<input class="form-control" type="text" placeholder="性别" name="gender" id="gender">
</div>
</div>
<div class="form-group">
<label class="control-label">年龄</label>
<div>
<input class="form-control" type="text" placeholder="年龄" name="age" id="age">
</div>
</div>
<div class="form-group">
<label class="control-label">密码</label>
<div>
<input class="form-control" type="text" placeholder="密码" name="password" id="password">
</div>
</div>
<div class="form-actions">
<button class="btn blue" id="register" type="submit">
提交注册
</button>
</div>
</div>
</body>
在jquery中的处理是:当用户点击提交注册按钮时,获取到用户提交的数据,并进行一一判空处理(在前端判空有助于用户快速感知到错误),然后通过ajax请求将这些用户信息传递到controller层:
controller层的处理:
在us er controller中编写一个方法register(),添加注解 @RequestMapping
和@ResponseBody,用于映射前端ajax请求中的路径和该方法返回后将数据返回给前端,在该方法的参数中用注解@RequestParam获取到前端用户填写的信息
在方法中,首先用getsession()获取到该手机号的验证码(在controller的getotp方法中,将后台产生的验证码存入了session中,也就是在注册时,获取到session中的验证码与用户填写的是否一致),比对前端用户填写的验证码是否匹配,匹配则继续往下执行,否则报错。
然后将用户信息等装在usermodel中,作为参数传递到service中
调用通过类的creste方法,返回给前端后台的处理结果。(由于注册也是将用户的信息插入到数据库中,因此没有返回数据,该方法的参数是null)
代码如下:
//用户注册接口
@RequestMapping(value = "/register",method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType register(@RequestParam(name = "telephone")String telephone,
@RequestParam(name = "otpCode")String otpCode,
@RequestParam(name = "name")String name,
@RequestParam(name = "gender")Integer gender,
@RequestParam(name = "age")Integer age,
@RequestParam(name = "password")String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
//验证手机号和对应的otp相符合
String inSessionOtpCode= (String) this.httpServletRequest.getSession().getAttribute(telephone);
if(!StringUtils.equals(otpCode,inSessionOtpCode)){
throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR,"短信验证码错误");
}
//用户的注册流程
UserModel userModel=new UserModel();
//将参数传递的值给userModel
userModel.setName(name);
userModel.setGender(gender);
userModel.setAge(age);
userModel.setTelephone(telephone);
userModel.setRegisterMode("byphone");
userModel.setEncryptPassword(this.EncodeByMd5(password));
//调用service层register方法,将封装在userModel中的前端数据传递到dao层,进行数据库查询操作
userService.register(userModel);
return CommonReturnType.create(null);
}
在userservice接口中添加方法register(),在实现类UserServiceImpl中实现register方法:
校验前端传递的数据是否为null,是否正确,不正确则报错,正确就继续往下执行
接着将service层的模型数据转换成dao层的模型数据userdo,调用dao层的方法将用户填写的信息(作为参数传递到数据库)插入到数据库中:
再转换用户密码,同时将其插入到数据库密码表中。
```java
//用户注册
@Override
@Transactional
public void register(UserModel userModel) throws BusinessException {
//判断封装在userModel的模型数据是否为null
if(userModel==null){
throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
}
ValidationResult result=validator.validate(userModel);
if(result.isHasErrors()){
throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
}
//实现model->dataobject方法
UserDO userDO = convertFromModel(userModel);
try{
userDOMapper.insertSelective(userDO);
}catch(DuplicateKeyException ex){
throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR,"手机号已重复注册");
}
userModel.setId(userDO.getId());
UserPasswordDO userPasswordDO=convertPasswordFromModel(userModel);
userPasswordDOMapper.insertSelective(userPasswordDO);//将前端传递来的用户密码数据插入到数据库中
return ;
}
dao层:将用户信息插入到用户信息表中,将用户密码插入到用户密码表中,也就是两个插入数据库的sql语句:
userDOMapper.insertSelective(userDO);
userPasswordDOMapper.insertSelective(userPasswordDO);
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.UserDO" keyProperty="id" useGeneratedKeys="true">
insert into user_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="gender != null">
gender,
</if>
<if test="age != null">
age,
</if>
<if test="telephone != null">
telephone,
</if>
<if test="registerMode != null">
register_mode,
</if>
<if test="thirdPartyId != null">
third_party_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="gender != null">
#{gender,jdbcType=INTEGER},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="telephone != null">
#{telephone,jdbcType=VARCHAR},
</if>
<if test="registerMode != null">
#{registerMode,jdbcType=VARCHAR},
</if>
<if test="thirdPartyId != null">
#{thirdPartyId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<insert id="insertSelective" parameterType="com.miaoshaproject.dataobject.UserPasswordDO" >
insert into user_password
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="encrptPassword != null">
encrpt_password,
</if>
<if test="userId != null">
user_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="encrptPassword != null">
#{encrptPassword,jdbcType=VARCHAR},
</if>
<if test="userId != null">
#{userId,jdbcType=INTEGER},
</if>
</trim>
</insert>