1. 实现流程
2. 后端接口开发
2.1 完成DAO操作
- 创建实体类
@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;
}
- 创建DAO接口、定义操作?法
public interface UserDAO {
public int insert(User user);
public User query(String name);
};
- 创建DAO接口的mapper?件并完成配置
<mapper namespace="com.qfedu.fmmall.dao.UserDAO">
<insert id="insertUser">
insert into users(username,password,user_regtime,user_modtime)
values(#{username},#{password},#{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>
2.2 完成Service业务
- 创建service接口
public interface UserService {
public ResultVO userResgit(String name, String pwd);
public ResultVO checkLogin(String name, String pwd);
}
- 创建service接口实现类,完成业务实现
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Transactional
public ResultVO userResgit(String name, String pwd) {
synchronized (this) {
User user = userDAO.queryUserByName(name);
if (user == null) {
String md5Pwd = MD5Utils.md5(pwd);
user = new User();
user.setUsername(name);
user.setPassword(md5Pwd);
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) {
User user = userDAO.queryUserByName(name);
if(user == null){
return new ResultVO(10001,"登录失败,?户名不存在!",null);
}else{
String md5Pwd = MD5Utils.md5(pwd);
if(md5Pwd.equals(user.getPassword())){
return new ResultVO(10000,"登录成功!",user);
}else{
return new ResultVO(10001,"登录失败,密码错误!",null);
}
}
2.3 完成Controller提供接口
- 创建controller,调?service
- 添加接口注解
@RestController
@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("/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.userResgit(username, password);
return resultVO;
}
}
2.4 接口测试
基于swagger进行测试
|