自学内容网 自学内容网

项目实战--网页五子棋(用户模块)(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 {
    @Autowired
    UserMapper 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 {
    @Autowired
    UserService 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();
        }

    }


原文地址:https://blog.csdn.net/2303_78892316/article/details/145078466

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!