用户注册接口
# 手机号的唯一性
为了保证手机号的唯一性,在phone字段上添加UNIQUE索引
//增加唯一索引
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL COMMENT '昵称',
`pwd` varchar(124) DEFAULT NULL COMMENT '密码',
`head_img` varchar(524) DEFAULT NULL COMMENT '头像',
`phone` varchar(64) DEFAULT '' COMMENT '手机号',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `phone` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
又或者可以根据手机号查询用户信息,判断用户是否已经存在(手机号已经注册)
<!--根据手机号查询用户信息-->
<select id="findByPhone" resultType="User">
select * from user where phone =#{phone}
</select>
Controller
@RestController
@RequestMapping("api/v1/pri/user")
public class UserContoller {
@Autowired
private UserService userService;
@PostMapping("register")
public JsonData register(@RequestBody Map<String,String> userInfo ){
int rows = userService.save(userInfo);
return rows == 1 ? JsonData.buildSuccess(): JsonData.buildError("注册失败,请重试");
}
}
Service
interface
public interface UserService {
/**
* 新增用户
* @param userInfo
* @return
*/
int save(Map<String, String> userInfo);
}
impl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int save(Map<String, String> userInfo) {
User user = parseToUser(userInfo);
if( user != null){
return userMapper.save(user);
}else {
return -1;
}
}
/**
* 解析 user 对象
* @param userInfo
* @return
*/
private User parseToUser(Map<String,String> userInfo) {
if(userInfo.containsKey("phone") && userInfo.containsKey("pwd") && userInfo.containsKey("name")){
User user = new User();
user.setName(userInfo.get("name"));
user.setHeadImg(getRandomImg());
user.setCreateTime(new Date());
user.setPhone(userInfo.get("phone"));
String pwd = userInfo.get("pwd");
//MD5加密
user.setPwd(CommonUtils.MD5(pwd));
return user;
}else {
return null;
}
}
/**
* 放在CDN上的随机头像
*/
private static final String [] headImg = {
"https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
"https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
"https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
"https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
"https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
};
private String getRandomImg(){
int size = headImg.length;
Random random = new Random();
int index = random.nextInt(size);
return headImg[index];
}
}
Mapper
public interface UserMapper {
int save(User user);
User findByPhone(@Param("phone") String phone);
}
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="net.xdclass.online_xdclass.mapper.UserMapper">
<insert id="save" parameterType="User">
INSERT INTO user (name, pwd, head_img, phone , create_time)
values (#{name,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR}, #{headImg,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})
</insert>
<!--根据手机号查询用户信息-->
<select id="findByPhone" resultType="User">
select * from user where phone =#{phone}
</select>
</mapper>
JsonData
package net.xdclass.online_xdclass.utils;
public class JsonData {
/**
* 状态码 0表示成功过,1表示处理中,-1 表示失败
*/
private Integer code;
/**
* 业务数据
*/
private Object data;
/**
* 信息表示
*/
private String msg;
public JsonData(){}
public JsonData(Integer code, Object data, String msg){
this.code = code;
this.data = data;
this.msg = msg;
}
/**
* 成功,不用返回数据
* @return
*/
public static JsonData buildSuccess(){
return new JsonData(0,null,null);
}
/**
* 成功,返回数据
* @param data
* @return
*/
public static JsonData buildSuccess(Object data){
return new JsonData(0,data,null);
}
/**
* 失败,固定状态码
* @param msg
* @return
*/
public static JsonData buildError(String msg){
return new JsonData(-1 ,null,msg);
}
/**
* 失败,自定义错误码和信息
* @param code
* @param msg
* @return
*/
public static JsonData buildError(Integer code , String msg){
return new JsonData(code ,null,msg);
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
CommonUtils(MD5加密工具类)
package net.xdclass.online_xdclass.utils;
import java.security.MessageDigest;
/**
* 工具类
*/
public class CommonUtils {
/**
* MD5加密工具类
* @param data
* @return
*/
public static String MD5(String data) {
try {
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
} catch (Exception exception) {
}
return null;
}
}
测试
localhost:8081/api/v1/pri/user/register
POST:Body->raw->JSON(application/json)
{
"name":"jack",
"phone":"12345678",
"pwd":"123456"
}