关于博饼web开的的记录
类
TUser
我们创建了一个TUser类用来表示网站使用者,现在结识一些注解的用法
1.@Data是lombok的注解,用于自动生成属性的set(),get()和toString() 方法
2.@EqualsAndHashCode(callSuper = false):当使用@Data注解时,则有了@EqualsAndHashCode注解,那么就会在此类中存在equals(Object other) 和 hashCode()方法,且不会使用父类的属性,这就导致了可能的问题。比如,有多个类有相同的部分属性,把它们定义到父类中,恰好id(数据库主键)也在父类中,那么就会存在部分对象在比较时,它们并不相等,却因为lombok自动生成的equals(Object other) 和 hashCode()方法判定为相等,从而导致出错。
3.@TableName("T_user"):用来指定模式名称。如果你使用的是 mysql 数据库,则指定数据库名称。
4.@ApiModel是Swagger用来描述类的一些基本信息的。是用来进行前后端分离的
5.@ApiModelProperty是Swagger中用来描述类的属性的
6.TableField("Upassword")是MybatisPlus中与数据库表中的字段进行对应的,例如这个就是Upassword字段进行匹配的
package com.example.ee302lab2.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.util.Date;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("T_user")
@ApiModel(value = "TUser对象", description = "")
public class TUser implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "用户ID", example = "1")
@TableId(value = "PK_UID", type = IdType.AUTO)
private Integer pkUid;
@ApiModelProperty(value = "昵称", example = "1")
@TableField("Uname")
private String uname;
@ApiModelProperty(value = "密码", example = "1")
@TableField("Upassword")
private String upassword;
@ApiModelProperty(value = "电话", example = "1")
@TableField("Uphone")
private String uphone;
@ApiModelProperty(value = "数据", example = "1")
@TableField("Udata")
private String Udata;
@ApiModelProperty(value = "状态", example = "0是默认用户状态,1是已经被拉黑的用户状态,9是管理员状态")
@TableField("Ustate")
private Integer ustate;
}
room
package com.example.ee302lab2.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("T_room")
@ApiModel(value = "房间对象", description = "")
public class room implements Serializable {
@ApiModelProperty(value = "房间ID", example = "1")
@TableId(value = "PK_RID")
private int id;
@ApiModelProperty(value = "状态", example = "0是未开启状态,1是已经开启状态")
@TableField("state")
private Integer ustate;
@ApiModelProperty(value = "密码", example = "")
@TableField("password")
private String password;
}
DAO层
Mybatis-Plus
有了这个之后我们就可以不写一些通用的Mapper的内容,节省了很多的时间和精力
我们只需要在TUserMapper接口中写入我们需要的方法和他的输入参数
@Mapper
public interface TUserMapper extends BaseMapper<TUser> {
TUser getOneUser(int PK_UID);
List<TUser> getAllUsers();
TUser getUserInf(String Unumber);
void changeUser(String Uname, String Uphone, String Uintroduction, String Uaddress, int PK_UID);
void changeOneUserState(int Ustate ,int PK_UID);
}
然后在mapper文件中先做好对应的接口文件<mapper namespace="com.example.ee302lab2.mapper.TUserMapper">
做好相应的CRUD语句就行了,这是mybatis的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.ee302lab2.mapper.TUserMapper">
<!-- 通过id查找用户,返回的是TUser类对象-->
<select id="getOneUser" resultType="com.example.ee302lab2.entity.TUser">
select *
from T_user
where PK_UID = #{PK_UID}
</select>
<!-- 查找所有用户,返回的是TUser类对象-->
<select id="getAllUsers" resultType="com.example.ee302lab2.entity.TUser">
select *
from T_user
</select>
<!-- 使用电话号码查询用户的信息,这是我们最常用的验证用户信息的方法-->
<select id="getUserInf" resultType="com.example.ee302lab2.entity.TUser">
select *
from T_user
where Uphone = #{Uphone}
</select>
<!-- 通过ID修改用户的信息-->
<update id="changeUser">
update T_user
set Uname=#{Uname},
Uphone=#{Uphone},
Udata=#{Udata}
where PK_UID = #{PK_UID}
</update>
<!-- 更改一个用户的状态-->
<update id="changeOneUserState">
update T_user
set Ustate=#{Ustate}
where PK_UID = #{PK_UID}
</update>
</mapper>
Controller层
Controller层的主要作用就是前端与后端连接的部分,拿到前端的请求,经过处理传递给后端
diceController骰子
在这里我们写了判断结果的方法
package com.example.ee302lab2.controller;
import com.example.ee302lab2.entity.room;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/dice", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "diceController", tags = "扔色子")
public class diceController {
@ApiOperation("创建房间")
@PostMapping("/createRoom")
public int createRoom(
) {
room r1 = new room();
int id = r1.getId();
return id;
}
@ApiOperation("验证结果")
@PostMapping("/checkResult")
public String checkResult(@ApiParam("名称") @RequestParam("Uname") String temp
) {
System.out.println(temp);
if (temp.length() != 6) {
return "传入参数有误";
}
int i1 = Integer.parseInt(temp);
int[] dice_array = new int[6];
for (int i = 0; i < 6; i++) {
dice_array[i] = i1%10;
i1/=10;
}
String result;
int one_flag = 0;
int two_flag = 0;
int three_flag = 0;
int four_flag = 0;
int five_flag = 0;
int six_flag = 0;
for (int i = 0; i < 6; i++) {
if (dice_array[i] == 1) {
one_flag++;
}
if (dice_array[i] == 2) {
two_flag++;
}
if (dice_array[i] == 3) {
three_flag++;
}
if (dice_array[i] == 4) {
four_flag++;
}
if (dice_array[i] == 5) {
five_flag++;
}
if (dice_array[i] == 6) {
six_flag++;
}
}
//状元
if ((two_flag == 2 && four_flag == 4) || (four_flag == 6) || (one_flag == 6) || (four_flag == 5) || (two_flag == 5) || (four_flag == 4)) {
result = "状元";
}
//对堂
else if (one_flag == 1 && two_flag == 1 && three_flag == 1 && four_flag == 1 && five_flag == 1 && six_flag == 1) {
result = "对堂";
}
//四进
else if (two_flag == 4) {
result = "四进";
}
//三红
else if (four_flag == 3) {
result = "三红";
}
//二举
else if (four_flag == 2) {
result = "二举";
}
//一秀
else if (four_flag == 1) {
result = "一秀";
} else {
result = "不中";
}
return result;
}
}
IndexController
package com.example.ee302lab2.controller;
import com.example.ee302lab2.mapper.TUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@Autowired
TUserMapper tUserMapper;
@Controller
public class HelloController {
@RequestMapping("/Hi")
public ModelAndView sayHello() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("register01.html");
return modelAndView;
}
}
}
UserController
package com.example.ee302lab2.controller;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.example.ee302lab2.entity.TUser;
import com.example.ee302lab2.mapper.TUserMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@Controller
@CrossOrigin//处理跨域问题
@RequestMapping("/user")
public class UserController {
@Autowired
TUserMapper tUserMapper;
@PostMapping("/login")
public String selectUser(@RequestParam("Unumber") String Unumber,
@RequestParam("password") String password
) {
TUser userInf = tUserMapper.getUserInf(Unumber);
if (null == userInf) {
return "redirect:/register01.html";
} else {
if (password.equals(userInf.getUpassword())) {
return "redirect:/bobing-master/bobing/index.html";
} else {
return "redirect:/register01.html";
}
}
}
@ApiOperation(value = "用户注册,学工号在数据库是唯一元素,成功message返回“用户注册成功”,否则返回“该学工号已注册“")
@PostMapping("/registerUserWithPhone")
@ApiResponses({
@ApiResponse(code = 200, message = "用户注册成功", response = TUser.class),
@ApiResponse(code = 999, message = "该学工号已注册", response = TUser.class)
})
public String registerUser(@ApiParam("姓名") @RequestParam("Uname") String Uname,
@ApiParam("密码") @RequestParam("Upassword") String Upassword,
@ApiParam("电话") @RequestParam("Uphone") String Uphone) throws IOException {
System.out.println(Uname+Upassword+Uphone);
if (null == tUserMapper.getUserInf(Uphone)) {
TUser tUser = new TUser();
tUser.setUname(Uname);
tUser.setUphone(Uphone);
tUser.setUstate(0);
tUser.setUpassword(Upassword);
tUserMapper.insert(tUser);
return "redirect:/bobing-master/bobing/index.html";
} else {
return "redirect:/register01.html";
}
}
}

浙公网安备 33010602011771号