用户注册接口
# 手机号的唯一性
为了保证手机号的唯一性,在 phone 字段上添加 UNIQUE 索引
1//增加唯一索引
2CREATE TABLE `user` (
3 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
4 `name` varchar(128) DEFAULT NULL COMMENT '昵称',
5 `pwd` varchar(124) DEFAULT NULL COMMENT '密码',
6 `head_img` varchar(524) DEFAULT NULL COMMENT '头像',
7 `phone` varchar(64) DEFAULT '' COMMENT '手机号',
8 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
9 PRIMARY KEY (`id`),
10 UNIQUE KEY `phone` (`phone`)
11) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
又或者可以根据手机号查询用户信息,判断用户是否已经存在(手机号已经注册)
1 <!--根据手机号查询用户信息-->
2 <select id="findByPhone" resultType="User">
3
4 select * from user where phone =#{phone}
5
6 </select>
7
Controller
1@RestController
2@RequestMapping("api/v1/pri/user")
3public class UserContoller {
4
5
6
7 @Autowired
8 private UserService userService;
9
10 @PostMapping("register")
11 public JsonData register(@RequestBody Map<String,String> userInfo ){
12
13 int rows = userService.save(userInfo);
14
15 return rows == 1 ? JsonData.buildSuccess(): JsonData.buildError("注册失败,请重试");
16
17 }
18}
19
Service
interface
1public interface UserService {
2
3 /**
4 * 新增用户
5 * @param userInfo
6 * @return
7 */
8 int save(Map<String, String> userInfo);
9
10}
impl
1@Service
2public class UserServiceImpl implements UserService {
3
4 @Autowired
5 private UserMapper userMapper;
6
7
8 @Override
9 public int save(Map<String, String> userInfo) {
10
11 User user = parseToUser(userInfo);
12 if( user != null){
13 return userMapper.save(user);
14 }else {
15 return -1;
16 }
17
18 }
19
20 /**
21 * 解析 user 对象
22 * @param userInfo
23 * @return
24 */
25 private User parseToUser(Map<String,String> userInfo) {
26
27 if(userInfo.containsKey("phone") && userInfo.containsKey("pwd") && userInfo.containsKey("name")){
28 User user = new User();
29 user.setName(userInfo.get("name"));
30 user.setHeadImg(getRandomImg());
31 user.setCreateTime(new Date());
32 user.setPhone(userInfo.get("phone"));
33 String pwd = userInfo.get("pwd");
34 //MD5加密
35 user.setPwd(CommonUtils.MD5(pwd));
36
37 return user;
38 }else {
39 return null;
40 }
41
42 }
43
44 /**
45 * 放在CDN上的随机头像
46 */
47 private static final String [] headImg = {
48 "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
49 "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
50 "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
51 "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
52 "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
53 };
54
55 private String getRandomImg(){
56 int size = headImg.length;
57 Random random = new Random();
58 int index = random.nextInt(size);
59 return headImg[index];
60 }
61
62}
Mapper
1public interface UserMapper {
2
3 int save(User user);
4
5 User findByPhone(@Param("phone") String phone);
6
7
8}
9
XML
1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3<mapper namespace="net.xdclass.online_xdclass.mapper.UserMapper">
4
5 <insert id="save" parameterType="User">
6
7 INSERT INTO user (name, pwd, head_img, phone , create_time)
8 values (#{name,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR}, #{headImg,jdbcType=VARCHAR},
9 #{phone,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})
10
11 </insert>
12
13
14 <!--根据手机号查询用户信息-->
15 <select id="findByPhone" resultType="User">
16
17 select * from user where phone =#{phone}
18
19 </select>
20
21</mapper>
JsonData
1package net.xdclass.online_xdclass.utils;
2
3public class JsonData {
4
5 /**
6 * 状态码 0表示成功过,1表示处理中,-1 表示失败
7 */
8 private Integer code;
9
10 /**
11 * 业务数据
12 */
13 private Object data;
14
15 /**
16 * 信息表示
17 */
18 private String msg;
19
20 public JsonData(){}
21
22 public JsonData(Integer code, Object data, String msg){
23 this.code = code;
24 this.data = data;
25 this.msg = msg;
26 }
27
28
29 /**
30 * 成功,不用返回数据
31 * @return
32 */
33 public static JsonData buildSuccess(){
34 return new JsonData(0,null,null);
35 }
36
37 /**
38 * 成功,返回数据
39 * @param data
40 * @return
41 */
42 public static JsonData buildSuccess(Object data){
43 return new JsonData(0,data,null);
44 }
45
46
47 /**
48 * 失败,固定状态码
49 * @param msg
50 * @return
51 */
52 public static JsonData buildError(String msg){
53 return new JsonData(-1 ,null,msg);
54 }
55
56
57 /**
58 * 失败,自定义错误码和信息
59 * @param code
60 * @param msg
61 * @return
62 */
63 public static JsonData buildError(Integer code , String msg){
64 return new JsonData(code ,null,msg);
65 }
66
67
68 public Integer getCode() {
69 return code;
70 }
71
72 public void setCode(Integer code) {
73 this.code = code;
74 }
75
76 public Object getData() {
77 return data;
78 }
79
80 public void setData(Object data) {
81 this.data = data;
82 }
83
84 public String getMsg() {
85 return msg;
86 }
87
88 public void setMsg(String msg) {
89 this.msg = msg;
90 }
91}
92
CommonUtils(MD5 加密工具类)
1package net.xdclass.online_xdclass.utils;
2
3import java.security.MessageDigest;
4
5/**
6 * 工具类
7 */
8public class CommonUtils {
9
10 /**
11 * MD5加密工具类
12 * @param data
13 * @return
14 */
15 public static String MD5(String data) {
16 try {
17 java.security.MessageDigest md = MessageDigest.getInstance("MD5");
18 byte[] array = md.digest(data.getBytes("UTF-8"));
19 StringBuilder sb = new StringBuilder();
20 for (byte item : array) {
21 sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
22 }
23
24 return sb.toString().toUpperCase();
25 } catch (Exception exception) {
26 }
27 return null;
28 }
29}
30
测试
1localhost:8081/api/v1/pri/user/register
2
3POST:Body->raw->JSON(application/json)
4
5
6
7{
8 "name":"jack",
9 "phone":"12345678",
10 "pwd":"123456"
11}