欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 项目实战--网页五子棋(用户模块)(1)

项目实战--网页五子棋(用户模块)(1)

2025/4/23 14:25:04 来源:https://blog.csdn.net/2303_78892316/article/details/145078466  浏览:    关键词:项目实战--网页五子棋(用户模块)(1)

接下来我将使用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  -- 获胜次数
);
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:

@Mapper
public interface UserMapper {@Select("select * from user where username = #{username}")User getUserByName(String username);
}

service:

@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:

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:

@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:

public Integer register(User user) {return userMapper.insertUser(user);}

controller:

    @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 获取用户信息接口 

    @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();}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词