千峰商城-springboot项目搭建-10-用户管理-接口开发-DAO
1.更改mapper子工程的application.yml,修改连接的数据库
spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/fmmall2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: 123456 mybatis: mapper-locations: classpath:mappers/*Mapper.xml type-aliases-package: com.qfedu.fmmall.entity
2.创建user实体类
beans-src-main-java-com.qfedu.fmmall.entity- User.java:
package com.qfedu.fmmall.entity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor @ApiModel(value = "User对象",description = "买家信息") public class User { private int userId; private String username; private String password; private String nickname; private String realname; private String userImg; private String userMobile; private String userEmail; private String userSex; private Date userBirth; private Date userRegtime; private Date userModtime; }
2.创建DAO接口,定义操作方法。
mapper-src-main-java-com.qfedu.fmmall.dao- UserDAO.java:
package com.qfedu.fmmall.dao; import com.qfedu.fmmall.entity.User; //@Repository //@Mapper public interface UserDAO { //用户注册 public int insertUser(User user); //根据用户名查询用户信息 public User queryUserByName(String name); }
3.创建DAO接口的mapper文件并完成配置。
mapper-src-main-resources-mappers- 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.qfedu.fmmall.dao.UserDAO"> <insert id="insertUser"> insert into users(username,password,user_img,user_regtime,user_modtime) values(#{username},#{password},#{userImg},#{userRegtime},#{userModtime}) </insert> <resultMap id="userMap" type="User"> <id column="user_id" property="userId"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="nickname" property="nickname"/> <result column="realname" property="realname"/> <result column="user_img" property="userImg"/> <result column="user_mobile" property="userMobile"/> <result column="user_email" property="userEmail"/> <result column="user_sex" property="userSex"/> <result column="user_birth" property="userBirth"/> <result column="user_regtime" property="userRegtime"/> <result column="user_modtime" property="userModtime"/> </resultMap> <select id="queryUserByName" resultMap="userMap"> select user_id,username,password,nickname,realname,user_img,user_mobile, user_email,user_sex,user_birth,user_regtime,user_modtime from users where username=#{name} </select> </mapper>
4.完成Service业务
在service子工程的UserService.java中编写用户注册和用户登录接口。
UserService.java:
package com.qfedu.fmmall.service; import com.qfedu.fmmal.vo.ResultVO; import com.qfedu.fmmall.entity.User; public interface UserService { //用户注册 public ResultVO userRegist(String name,String pwd); //用户登录 public ResultVO checkLogin(String name, String pwd); }
在mapper中的UserDAO.java中添加注解@Repository用来注解接口。
UserDAO.java:
package com.qfedu.fmmall.dao; import com.qfedu.fmmall.entity.User; import org.springframework.stereotype.Repository; @Repository //@Mapper public interface UserDAO { //用户注册 public int insertUser(User user); //根据用户名查询用户信息 public User queryUserByName(String name); }
编写UserServiceImpl.java来实现service接口。
先在common子工程的src - main - java - com.qfedu.fmmall 中新建一个util包,然后在包中创建一个加密工具类MD5Utils.java。
MD5Utils.java:
package com.qfedu.fmmal.utils; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; //MD5 生成器 public class MD5Utils { public static String md5(String password){ //生成一个md5加密器 try { MessageDigest md = MessageDigest.getInstance("MD5"); //计算MD5 的值 md.update(password.getBytes()); //BigInteger 将8位的字符串 转成16位的字符串 得到的字符串形式是哈希码值 //BigInteger(参数1,参数2) 参数1 是 1为正数 0为0 -1为负数 return new BigInteger(1, md.digest()).toString(16); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } }
UserServiceImpl.java:
package com.qfedu.fmmall.impl; import com.qfedu.fmmal.utils.MD5Utils; import com.qfedu.fmmal.vo.ResultVO; import com.qfedu.fmmall.dao.UserDAO; import com.qfedu.fmmall.entity.User; import com.qfedu.fmmall.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import sun.security.provider.MD5; import javax.annotation.Resource; import java.util.Date; @Service public class UserServiceImpl implements UserService { @Autowired private UserDAO userDAO; @Transactional public ResultVO userRegist(String name,String pwd){ synchronized (this) { //1.根据用户查询,这个用户是否已被注册 User user = userDAO.queryUserByName(name); //2.如果没有被注册,则进行保存操作 if (user == null) { String md5Pwd = MD5Utils.md5(pwd); user = new User(); user.setUsername(name); user.setPassword(md5Pwd);
user.setUserImg("img/default.png"); user.setUserRegtime(new Date()); user.setUserModtime(new Date()); int i = userDAO.insertUser(user); if (i > 0){ return new ResultVO(10000,"注册成功!",null); }else { return new ResultVO(10002,"注册失败!",null); } } else { return new ResultVO(10001, "用户名已经被注册", null); } } } @Override public ResultVO checkLogin(String name, String pwd) { //1.根据账号查询用户信息 User user = userDAO.queryUserByName(name); //2.判断。如果user=null则错误 if (user == null){ //用户名不存在 return new ResultVO(10001,"登录失败,用户名不存在",null); }else { //3.对输入的密码进行加密 String md5Pwd = MD5Utils.md5(pwd); //4.使用加密后的密码 和 user中的密码进行匹配。 if (md5Pwd.equals(user.getPassword())){ //验证成功 return new ResultVO(10000,"登录成功!",user); }else { //密码错误 return new ResultVO(10001,"登录失败,密码错误!",null); } } } }
5.完成Controller提供接口
UserController.java:
package com.qfedu.fmmall.controller; import com.qfedu.fmmal.vo.ResultVO; import com.qfedu.fmmall.entity.User; import com.qfedu.fmmall.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @RestController //@ResponseBody//异步请求,返回js数据 @RequestMapping("/user") @Api(value = "提供用户的登录和注册接口",tags = "用户管理") public class UserController {//接收和响应 @Resource private UserService userService; @ApiOperation("用户登录接口") @ApiImplicitParams({ @ApiImplicitParam(dataType = "string",name = "username",value = "用户登录账号",required = true), @ApiImplicitParam(dataType = "string",name = "password",value = "用户登录密码",required = true) }) @GetMapping(value = "/login") public ResultVO login(@RequestParam("username") String name,@RequestParam(value = "password") String pwd){ ResultVO resultVO = userService.checkLogin(name,pwd); return resultVO; } @ApiOperation("用户注册接口") @ApiImplicitParams({ @ApiImplicitParam(dataType = "string",name = "username",value = "用户注册账号",required = true), @ApiImplicitParam(dataType = "string",name = "password",value = "用户注册密码",required = true) }) @PostMapping("/regist") public ResultVO regist(String username,String password){ ResultVO resultVO = userService.userRegist(username,password); return resultVO; } }
在UserMapper.xml中添加默认用户图片:
UserMapper.xml:
<insert id="insertUser"> insert into users(username,password,user_img,user_regtime,user_modtime) values(#{user.username},#{user.password},#{user.userImg},#{user.userRegtime},#{userModtime}) </insert>
删去api子工程中的GoodsController.java。
6.在swagger中进行单元测试
注册测试:
再次注册同样的用户名:
登录测试:
正确用户名错误密码:
错误用户名:
正确用户名正确密码: