您好,登錄后才能下訂單哦!
從用戶(hù)注冊(cè)接口開(kāi)始。源碼:https://github.com/limingios/wxProgram.git 中的wx-springboot
返回統(tǒng)一工具類(lèi)JSONResult 放入common工程中
package?com.idig8.utils; /** ?*?@Description:?自定義響應(yīng)數(shù)據(jù)結(jié)構(gòu) ?*??????????????這個(gè)類(lèi)是提供給門(mén)戶(hù),ios,安卓,微信商城用的 ?*??????????????門(mén)戶(hù)接受此類(lèi)數(shù)據(jù)后需要使用本類(lèi)的方法轉(zhuǎn)換成對(duì)于的數(shù)據(jù)類(lèi)型格式(類(lèi),或者list) ?*??????????????其他自行處理 ?*??????????????200:表示成功 ?*??????????????500:表示錯(cuò)誤,錯(cuò)誤信息在msg字段中 ?*??????????????501:bean驗(yàn)證錯(cuò)誤,不管多少個(gè)錯(cuò)誤都以map形式返回 ?*??????????????502:攔截器攔截到用戶(hù)token出錯(cuò) ?*??????????????555:異常拋出信息 ?*/ public?class?JSONResult?{ ????//?響應(yīng)業(yè)務(wù)狀態(tài) ????private?Integer?status; ????//?響應(yīng)消息 ????private?String?msg; ????//?響應(yīng)中的數(shù)據(jù) ????private?Object?data; ????private?String?ok;??//?不使用 ????public?static?JSONResult?build(Integer?status,?String?msg,?Object?data)?{ ????????return?new?JSONResult(status,?msg,?data); ????} ????public?static?JSONResult?ok(Object?data)?{ ????????return?new?JSONResult(data); ????} ????public?static?JSONResult?ok()?{ ????????return?new?JSONResult(null); ????} ????public?static?JSONResult?errorMsg(String?msg)?{ ????????return?new?JSONResult(500,?msg,?null); ????} ????public?static?JSONResult?errorMap(Object?data)?{ ????????return?new?JSONResult(501,?"error",?data); ????} ????public?static?JSONResult?errorTokenMsg(String?msg)?{ ????????return?new?JSONResult(502,?msg,?null); ????} ????public?static?JSONResult?errorException(String?msg)?{ ????????return?new?JSONResult(555,?msg,?null); ????} ????public?JSONResult()?{ ????} ????public?JSONResult(Integer?status,?String?msg,?Object?data)?{ ????????this.status?=?status; ????????this.msg?=?msg; ????????this.data?=?data; ????} ????public?JSONResult(Object?data)?{ ????????this.status?=?200; ????????this.msg?=?"OK"; ????????this.data?=?data; ????} ????public?Boolean?isOK()?{ ????????return?this.status?==?200; ????} ????public?Integer?getStatus()?{ ????????return?status; ????} ????public?void?setStatus(Integer?status)?{ ????????this.status?=?status; ????} ????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?String?getOk()?{ ????????return?ok; ????} ????public?void?setOk(String?ok)?{ ????????this.ok?=?ok; ????} } ```` *?加密類(lèi)MD5Utils放入common工程中 ```?java package?com.idig8.utils; import?java.security.MessageDigest; import?org.apache.commons.codec.binary.Base64; public?class?MD5Utils?{ ????/** ?????*?@Description:?對(duì)字符串進(jìn)行md5加密 ?????*/ ????public?static?String?getMD5Str(String?strValue)?throws?Exception?{ ????????MessageDigest?md5?=?MessageDigest.getInstance("MD5"); ????????String?newstr?=?Base64.encodeBase64String(md5.digest(strValue.getBytes())); ????????return?newstr; ????} ????public?static?void?main(String[]?args)?{ ????????try?{ ????????????String?md5?=?getMD5Str("idig8"); ????????????System.out.println(md5); ????????}?catch?(Exception?e)?{ ????????????e.printStackTrace(); ????????} ????} }
org包下面的放入common工程中,主要里面有個(gè)Sid的自動(dòng)生成Id的工具
UserService.java
package?com.idig8.service; import?com.idig8.pojo.Users; public?interface?UserService?{ ????/** ?????*?判斷用戶(hù)名是否存在 ?????*?@param?username ?????*?@return ?????*/ ????public?boolean?queryUsernameIsExist(String?username); ????/** ?????*?保存用戶(hù) ?????*?@param?user ?????*?@return ?????*/ ????public?void?saveUser(Users?user); }
UserServiceImpl.java
package?com.idig8.service; import?org.n3r.idworker.Sid; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.transaction.annotation.Propagation; import?org.springframework.transaction.annotation.Transactional; import?com.idig8.mapper.UsersMapper; import?com.idig8.pojo.Users; public?class?UserServiceImpl?implements?UserService?{ ????@Autowired ????private?UsersMapper?usersMapper; ????@Autowired ????private?Sid?sid; ????@Transactional(propagation?=Propagation.SUPPORTS) ????@Override ????public?boolean?queryUsernameIsExist(String?username)?{ ????????Users?user?=?new?Users(); ????????user.setUsername(username); ????????Users?result?=?usersMapper.selectOne(user); ????????return?result==null??false:true; ????} ????@Transactional(propagation?=Propagation.REQUIRED) ????@Override ????public?void?saveUser(Users?user)?{ ????????String?userId?=sid.nextShort(); ????????user.setId(userId); ????????usersMapper.insert(user); ????} }
RegistLoginController
package?com.idig8.controller; import?org.apache.commons.lang3.StringUtils; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.web.bind.annotation.PostMapping; import?org.springframework.web.bind.annotation.RequestBody; import?org.springframework.web.bind.annotation.RestController; import?com.github.pagehelper.util.StringUtil; import?com.idig8.pojo.Users; import?com.idig8.service.UserService; import?com.idig8.utils.JSONResult; import?com.idig8.utils.MD5Utils; @RestController public?class?RegistLoginController?{ ????@Autowired ????private?UserService?userService; ????@PostMapping("/regist") ????public?JSONResult?regist(@RequestBody?Users?user)?{ ????????//1.判斷用戶(hù)名和密碼不能為空 ????????if(StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword()))?{ ????????????return?JSONResult.errorMsg("用戶(hù)名或密碼不能為空"); ????????} ????????//2.判斷用戶(hù)名是否存在 ????????boolean?usernameIsExist?=?userService.queryUsernameIsExist(user.getUsername()); ????????if(!usernameIsExist)?{ ????????????user.setNickname(user.getUsername()); ????????????try?{ ????????????????user.setPassword(MD5Utils.getMD5Str(user.getPassword())); ????????????}?catch?(Exception?e)?{ ????????????????return?JSONResult.errorMsg(e.getMessage()); ????????????} ????????????user.setFollowCounts(0); ????????????user.setReceiveLikeCounts(0); ????????????user.setFansCounts(0); ????????????userService.saveUser(user); ????????}else?{ ????????????return?JSONResult.errorMsg("用戶(hù)名或已經(jīng)存在,請(qǐng)更換在試試!"); ????????} ????????return?JSONResult.ok(); ????} }
PS:基本的流程就是先確定業(yè)務(wù)邏輯,開(kāi)發(fā)service類(lèi),開(kāi)發(fā)controller類(lèi),common里面都是一些工具。下次說(shuō)說(shuō)swagger的使用。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。