1.总结:在昨天的学习中,主要是搭建mapper的两个接口方法实现用户的注册业务;首先是项目的创建,在创建的是够导入web,mybatis以及mysql driver
然后在生成项目的时候会自动生成mybatis,mysql的配置文件,我们根据自己的数据库进行更改。再就是创建mapper接口生成注册的插入和通过用户名
查询用户信息的两个接口方法,接着就是配置两个接口方法的xml文件,编写SQL语句。其次就是编写register业务,在这个过程中,我们首先是通过用户名
查询用户信息,再设置用户信息,注册到mysql中,其中密码是经过三次的MD5+password+MD5进行加密的;然后是根据具体方法编写异常基类和具体的异常
最后就是编写JSON的结果响应类,开始是较繁琐的把异常和成功地写在一起,后面生成一个基类,将异常放在基类中,让实际的继承基类,在实际的controller
中只写成功的,是一种很好地改进。
实体类基类
package com.ku.store.entity; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * 实体类的基类 */ @Data public class BaseEntity implements Serializable { private String createdUser; private Date createdTime; private String modifiedUser; private Date modifiedTime; }
用户实体类
package com.ku.store.entity; import lombok.Data; @Data public class User extends BaseEntity{ private Integer uid; private String username; private String password; private String salt; private String phone; private String email; private Integer gender; private String avatar; private Integer isDelete;//逻辑删除 }
UserMapper接口
package com.ku.store.mapper; import com.ku.store.entity.User; public interface UserMapper { Integer insert(User user); User findByUsername(String username); }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ku.store.mapper.UserMapper"> <resultMap id="UserEntityMap" type="com.ku.store.entity.User"> <id column="uid" property="uid"/> <result column="is_delete" property="isDelete"/> <result column="created_user" property="createdUser"/> <result column="created_time" property="createdTime"/> <result column="modified_user" property="modifiedUser"/> <result column="modified_time" property="modifiedTime"/> </resultMap> <!--useGeneratedKeys="true" :如果插入的表id以自增列为主键,则允许 JDBC 支持自动生成主键, 并可将自动生成的主键id返回。 keyProperty="uid" useGeneratedKeys="true"配合keyProperty="uid":将一个对象保存成功后,将插入的这条主键id返回, 并保存到对象中,供后续使用 --> <insert id="insert" useGeneratedKeys="true" keyProperty="uid"> INSERT into store.t_user(username, password, salt, phone, email, gender, avatar, is_delete, created_user, created_time, modified_user, modified_time) values (#{username}, #{password}, #{salt}, #{phone}, #{email}, #{gender}, #{avatar}, #{isDelete}, #{createdUser}, #{createdTime}, #{modifiedUser}, #{modifiedTime}) </insert> <select id="findByUsername" resultMap="UserEntityMap"> select * from store.t_user where username = #{username} </select> </mapper>
IUserService接口
package com.ku.store.service; import com.ku.store.entity.User; public interface IUserService { /** * 通过用户进行注册 * @param user */ void reg(User user); }
UserServiceImpl
package com.ku.store.service.impl; import com.ku.store.entity.User; import com.ku.store.mapper.UserMapper; import com.ku.store.service.IUserService; import com.ku.store.service.exception.InsertException; import com.ku.store.service.exception.UsernameDuplicateException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.DigestUtils; import java.util.Date; import java.util.UUID; @Service public class UserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override public void reg(User user) { //1.1 根据user参数获取注册的用户名 //1.2 调用UserMapper的findByUsername根据用户名查询用户信息 //1.3 判断查询结果是否为空,若为空,表示用户名已占用,抛出UsernameDuplicateException String username = user.getUsername(); User result = userMapper.findByUsername(username); if(result != null){ throw new UsernameDuplicateException("注册的用户"+username+"已被占用!"); } //2.1创建当前时间对象: // 2.2 补全数据:加密后的密码 // 2.3 补全数据:盐值 // 2.4 补全数据:IsDelete(0) // 2.5 补全数据:4项日志属性 Date now = new Date(); String salt = UUID.randomUUID().toString().toUpperCase(); String md5Password = getMd5Password(user.getPassword(), salt); user.setPassword(md5Password); user.setSalt(salt); user.setIsDelete(0); user.setCreatedUser(username); user.setCreatedTime(now); user.setModifiedUser(username); user.setModifiedTime(now); //3.1用户名没有被注册,则允许注册 //3.2调用UserMapper的insert方法,执行注册并返回结果,判断是否不为1, // 是则插入错误抛出InsertException异常 Integer rows = userMapper.insert(user); if(rows != 1){ throw new InsertException("注册用户出现未知问题,请联系系统管理员!"); } } private String getMd5Password(String password, String salt){ /** * 加密规则: * 1.无视原始密码强度 * 2.使用UUID作为盐值,在原始密码两侧拼接 * 3.循环加密3次 */ for (int i = 0; i <3 ; i++) { password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toUpperCase(); } return password; } }
exception
package com.ku.store.service.exception; /** * 业务异常的基类 */ public class ServiceException extends RuntimeException { public ServiceException() { super(); } public ServiceException(String message) { super(message); } public ServiceException(String message, Throwable cause) { super(message, cause); } public ServiceException(Throwable cause) { super(cause); } protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } package com.ku.store.service.exception; public class UsernameDuplicateException extends ServiceException { public UsernameDuplicateException() { super(); } public UsernameDuplicateException(String message) { super(message); } public UsernameDuplicateException(String message, Throwable cause) { super(message, cause); } public UsernameDuplicateException(Throwable cause) { super(cause); } protected UsernameDuplicateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } package com.ku.store.service.exception; public class InsertException extends ServiceException { public InsertException() { super(); } public InsertException(String message) { super(message); } public InsertException(String message, Throwable cause) { super(message, cause); } public InsertException(Throwable cause) { super(cause); } protected InsertException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
JsonResult
package com.ku.store.utils; import lombok.Data; import java.io.Serializable; @Data public class JsonResult<E> implements Serializable { private Integer state; private String message; private E data; public JsonResult(){//要加上无参构造,不然controller层创建返回值报错 super(); } public JsonResult(Integer state){ super(); this.state = state; } /** * 出现异常时调用 */ public JsonResult(Throwable e){ super(); this.message = e.getMessage(); } public JsonResult(Integer state, E data){ super(); this.state = state; this.data = data; } }
没有提取异常的controller
package com.ku.store.controller; import com.ku.store.entity.User; import com.ku.store.service.exception.InsertException; import com.ku.store.service.exception.UsernameDuplicateException; import com.ku.store.service.impl.UserServiceImpl; import com.ku.store.utils.JsonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/users") public class PreviousUserController { @Autowired private UserServiceImpl userService; @RequestMapping("/reg0") public JsonResult<Void> reg(User user){ //创建返回值 JsonResult<Void> result = new JsonResult<Void>(); try { userService.reg(user); result.setState(200); result.setMessage("注册成功!"); } catch (UsernameDuplicateException e) { result.setState(400); result.setMessage("用户名已被占用!"); }catch (InsertException e) { result.setState(500); result.setMessage("注册失败,请联系系统管理员!"); } return result; } }
改进后的controller
package com.ku.store.controller; import com.ku.store.service.exception.InsertException; import com.ku.store.service.exception.ServiceException; import com.ku.store.service.exception.UsernameDuplicateException; import com.ku.store.utils.JsonResult; import org.springframework.web.bind.annotation.ExceptionHandler; /** * 该类用于响应成功的状态码以及统一处理异常 ,简化原来的controller */ //控制类基类 public class BaseController { public static final int OK = 200; @ExceptionHandler(ServiceException.class) public JsonResult<Void>handleException(Throwable e){ JsonResult<Void> result = new JsonResult<>(); if (e instanceof UsernameDuplicateException){ result.setState(400); }else if(e instanceof InsertException){ result.setState(500); } return result; } }
package com.ku.store.controller; import com.ku.store.entity.User; import com.ku.store.service.impl.UserServiceImpl; import com.ku.store.utils.JsonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 处理用户相关请求的控制类 */ @RestController @RequestMapping("/users") public class UserController extends BaseController { @Autowired private UserServiceImpl userService; @RequestMapping("/reg") public JsonResult<Void>reg(User user){ userService.reg(user); return new JsonResult<>(OK); } }
2.反思:其实反手来巩固基础的项目,会更知道实际开发中我们习以为惯的授权注册的原生代码是如何实现的,能将实现原理更好理解,打好基础是重中之重,昨天主要体会在于改进的controller和基类的实体类,将公共的繁琐代码提取出来继承,可以简化很多代码的编写,这种思想很重要。
3.复盘:观看下之前自己的博客,回顾下之前别人写的,对自己的项目有什么改进之处
标签:ku,实现,用户,注册,import,com,public,store,String From: https://www.cnblogs.com/kzf-99/p/16755092.html