SpringBoot+vue+实战项目之第4集
Posted Panda_Java
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot+vue+实战项目之第4集相关的知识,希望对你有一定的参考价值。
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) {
//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.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进行测试
以上是关于SpringBoot+vue+实战项目之第4集的主要内容,如果未能解决你的问题,请参考以下文章
个人项目实战2,springboot集成(Html+vue.js)前端框架
个人项目实战2,springboot集成(Html+vue.js)前端框架