1.在resources下的mapper下添加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.xiaoguan.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>
2.在java下的mapper下添加UserMapper.java
package com.xiaoguan.mapper;
import com.xiaoguan.model.entity.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
int save(User user);
User findByPhone(@Param("phone") String phone);
}
3.在service下添加UserService
package com.xiaoguan.service;
import java.util.Map;
public interface UserService {
/*
* 添加新用户
* @param userInfo
* @return
* */
int save(Map<String,String> userInfo);
}
4.在utils下添加CommonUtils.java(添加md5加密工具)
import java.security.MessageDigest;
/*
* 工具类
* */
public class CommonUtils {
/*
* MD5加密工具类
* @param data
* @retrun
* */
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;
}
}
5.在service.Impl下添加UserServiceImpl.java
package com.xiaoguan.service.Impl;
import com.xiaoguan.mapper.UserMapper;
import com.xiaoguan.model.entity.User;
import com.xiaoguan.service.UserService;
import com.xiaoguan.untils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.Map;
import java.util.Random;
public class UserServiceImpl implements UserService {
@Autowired
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");
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];
}
}
6.在controller添加UserController
package com.xiaoguan.controller;
import com.xiaoguan.service.UserService;
import com.xiaoguan.untils.JsonData;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("api/v1/pri/user")
public class UserController {
@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("注册失败,请重试");
}
}
第一次send:
第二次send: