接下来我将使用Java语言,和Spring框架,实现一个简单的网页五子棋。
主要功能包括用户登录注册,人机对战,在线匹配对局,房间邀请对局,积分排行版等。
这篇文件讲解用户模块的后端代码
1. 用户表与实体类
用户需要用户名,密码,以及自己的积分,对战次数以及获胜次数:
drop table if exists user;
create table user (user_id int primary key auto_increment,username varchar(16) unique,password varchar(16),score int, -- 积分total_count int, -- 比赛次数win_count int -- 获胜次数
);
java">public class User {private int id;private String username;private String password;private int score;private int totalCount;private int winCount;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}public int getTotalCount() {return totalCount;}public void setTotalCount(int totalCount) {this.totalCount = totalCount;}public int getWinCount() {return winCount;}public void setWinCount(int winCount) {this.winCount = winCount;}}
2. 后端接口实现
2.1 登录接口
mapper:
java">@Mapper
public interface UserMapper {@Select("select * from user where username = #{username}")User getUserByName(String username);
}
service:
java">@Service
public class UserService {@AutowiredUserMapper userMapper;public User login(String username, String password) {User user = userMapper.getUserByName(username);if(user == null || !user.getPassword().equals(password)) {//用户名不存在或者密码错误,返回一个空的用户信息return new User();}return user;}
}
controller:
java">public class UserController {@AutowiredUserService userService;@RequestMapping("/login")public User login(String username, String password, HttpServletRequest request) {if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {//用户名或密码为空,返回空用户信息return new User();}User user = userService.login(username, password);//把登录用户信息储存在session中,方便后续获取,或验证登录状态HttpSession session = request.getSession(true);//参数true表示回话不存在时允许创建新会话session.setAttribute("user", user);return user;}
}
使用postman测试:
可以看到用户名密码正确时能够返回正确的用户信息
2.2 注册接口
mapper:
java">@Mapper
public interface UserMapper {@Select("select * from user where username = #{username}")User getUserByName(String username);@Insert("insert into user(username, password) values (#{username}, #{password})")Integer insertUser(User user);
}
service:
java">public Integer register(User user) {return userMapper.insertUser(user);}
controller:
java"> @RequestMapping("/register")public User register(String username, String password) {if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {//用户名或密码为空,返回空用户信息return new User();}try {User user = new User();user.setUsername(username);user.setPassword(password);userService.register(user);return user;}catch (DuplicateKeyException e) {//用户名存在,引发异常,返回空对象return new User();}}
使用postman进行测试:
用户不存在时注册成功:
用户存在时注册失败:
2.3 获取用户信息接口
java"> @RequestMapping("/getLoginUser")public User getLoginUser(HttpServletRequest request) {HttpSession session = request.getSession(false);//回话不存在时不允许创建会话try{return (User)session.getAttribute("user");}catch(NullPointerException e) {//session为null返回空对象return new User();}}