馬哥講IT:springboot2 +mybatis-plus 開發通用管理後臺框架mboot框架第九講,小試牛刀:完善註冊功能。
馬哥講IT:springboot2 +mybatis-plus 開發通用管理後臺框架mboot框架第九講,小試牛刀:完善註冊功能。
1,Result 統一返回提示 public class Result implements Serializable { private static final long serialVersionUID = 1L; private boolean flag; private Integer code; private String msg; private Object data; public Result(boolean flag, Object data) {this.flag = flag;this.data = data;} public Result(boolean flag, String msg) {this.flag = flag;this.msg = msg;} public Result(boolean flag, Integer code, String msg, Object data) {this.flag = flag; this.setCode(code);this.msg = msg;this.data = data;} public String getMsg() {return msg;} public void setMsg(String msg) {this.msg = msg;} public Object getData() {return data;} public void setData(Object data) {this.data = data;} public Integer getCode() {return code;} public void setCode(Integer code) {this.code = code;} public boolean isFlag() {return flag;} public void setFlag(boolean flag) {this.flag = flag;} }
2,UsersServiceImpl處理註冊流程org.it123.mageIT.common.service.impl.UsersServiceImpl
@Servicepublic class UsersServiceImpl extends IBaseService<Users> implements UsersService { @AutowiredUserDao userDao; public Result reg(Users users) {Users resUsers = getUserInfoByName(users.getName());if (resUsers != null) {return new Result(false, 400, "使用者名稱已經存在,請更換!", null);} Users nUsers = new Users();nUsers.setId(IdUtil.fastSimpleUUID());nUsers.setName(users.getName());BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();nUsers.setPassword(passwordEncoder.encode(users.getPassword()));// 預設角色是usernUsers.setRole("user");if (userDao.insert(nUsers) > 0) {return new Result(true, 200, "註冊成功", nUsers);} return new Result(false, 400, "註冊失敗!", null);} public Users getUserInfoByName(String name) {LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>();queryWrapper.eq(Users::getName, name);return userDao.selectOne(queryWrapper);}