标签:center json sele parse https md5 map ESS setname
<?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.ybclass.online_ybclass.mapper.UserMapper"> <!--根据手机号查询用户信息--> <select id="findByPhone" resultType="User"> SELECT * FROM user WHERE phone=#{phone} </select> <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> </mapper>
package net.ybclass.online_ybclass.mapper; import net.ybclass.online_ybclass.domain.User; import org.apache.ibatis.annotations.Param; public interface UserMapper { int save(User user); User findByPhone(@Param("phone") String phone); }
package net.ybclass.online_ybclass.service; import net.ybclass.online_ybclass.domain.User; import java.util.Map; public interface UserService { /** * 新增用户 * @param userInfo * @return */ int save(Map<String,String> userInfo); /** * 根据手机号查询用户 * @param phone * @return */ User findByPhone(String phone); }
package net.ybclass.online_ybclass.service.impl; import net.ybclass.online_ybclass.domain.User; import net.ybclass.online_ybclass.mapper.UserMapper; import net.ybclass.online_ybclass.service.UserService; import net.ybclass.online_ybclass.utils.CommonUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.Map; import java.util.Random; @Service public class UserServiceImpl implements UserService { @Autowired(required = false) 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; } } /** * 解析map中的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.setPhone(userInfo.get("phone")); user.setCreateTime(new Date()); String pwd=userInfo.get("pwd"); //MD5加密 user.setPwd(CommonUtils.MD5(pwd)); return user; } 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" }; /** * 获取随机头像 * @return */ private String getRandomImg(){ int size=headImg.length; Random random=new Random(); int index=random.nextInt(size); return headImg[index]; } @Override public User findByPhone(String phone) { return userMapper.findByPhone(phone); } }
package net.ybclass.online_ybclass.utils; public class CommonUtils { /** * MD5加密工具类 * @param data * @return */ public static String MD5(String data) { try { java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); byte[] array = md.digest(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte bt : array) { sb.append(Integer.toHexString((bt & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); } catch (Exception ex) { } return null; } }
package net.ybclass.online_ybclass.controller; import net.ybclass.online_ybclass.service.UserService; import net.ybclass.online_ybclass.utils.JsonData; import org.apache.ibatis.annotations.Param; 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(required = false) private UserService userService; @PostMapping("register") public JsonData register(@RequestBody Map<String,String> userInfo){ return userService.save(userInfo)==1?JsonData.buildSuccess():JsonData.buildError("注册失败,请重试"); } @PostMapping("find_phone") public JsonData findByPhone(@Param("phone") String phone){ return JsonData.buildSuccess(userService.findByPhone(phone)); } }
标签:center json sele parse https md5 map ESS setname
原文地址:https://www.cnblogs.com/chenyanbin/p/13323908.html