首先是分层
pojo层 ,将数据库中的表对应成Java中的class,表名定义成我们class的名称就可以了,列名就可以定义在class的属性里面,pojo层就是直接将table翻译成class
mapper 层,将pojo层的class中的操作,转化成sql语句,数据映射成class之后,还需要包含一些方法,增删改查,我们对于class里的增删改查(crud create read update delete)转化成mysql语句
service 层,负责写具体的业务逻辑,组合使用mapper中的操作,service层很多业务并不会只处理一张表,比如一个人创建一个AI,可以用到多个不同的mapper操作,service可以将基本操作进行一些组合实现一些业务
controller 层,负责请求转发,接收页面传过来的参数,传给service处理,接到返回值,再传给页面,调动service,返回给前端
这些都是习惯上的区分,如果不写service层其实也没有问题,这么写不容易出错
在backend文件夹里面创建 pojo文件夹
创建User类,在user类里面使用依赖 @Data,@NoArgContructor @ AllArgsConstructor
然后就会自动填补以下构造函数
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.kos.backend.pojo;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
public void setId(final Integer id) {
this.id = id;
}
public void setUsername(final String username) {
this.username = username;
}
public void setPassword(final String password) {
this.password = password;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label47;
}
} else if (this$id.equals(other$id)) {
break label47;
}
return false;
}
Object this$username = this.getUsername();
Object other$username = other.getUsername();
if (this$username == null) {
if (other$username != null) {
return false;
}
} else if (!this$username.equals(other$username)) {
return false;
}
Object this$password = this.getPassword();
Object other$password = other.getPassword();
if (this$password == null) {
if (other$password != null) {
return false;
}
} else if (!this$password.equals(other$password)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $username = this.getUsername();
result = result * 59 + ($username == null ? 43 : $username.hashCode());
Object $password = this.getPassword();
result = result * 59 + ($password == null ? 43 : $password.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", username=" + this.getUsername() + ", password=" + this.getPassword() + ")";
}
public User() {
}
public User(final Integer id, final String username, final String password) {
this.id = id;
this.username = username;
this.password = password;
}
}
下一步实现我们的mapper层
package com.kos.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kos.backend.pojo.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
一个表对应一个pojo,对应一个mapper
@RequestMapping 只映射Request类型的请求
@Autowired UserMapper userMapper; 如果要用到数据库的mapper,就需要添加注解Autowired
查询每一个用户
@GetMapping("/user/{userId}/")
public User getuser(@PathVariable int userId){
return userMapper.selectById(userId);
}
范围查询
@GetMapping("/user/{userId}/")
public User getuser(@PathVariable int userId){
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.ge("id",2).le("id",3);
return userMapper.selectOne(queryWrapper);
}
但是会报错
改成
@GetMapping("/user/{userId}/")
public List<User> getuser(@PathVariable int userId){
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.ge("id",2).le("id",3);
return userMapper.selectList(queryWrapper);
}
插入用户
@GetMapping("/user/add/{userId}/{username}/{password}/")
public String addUser(@PathVariable int userId,
@PathVariable String username,
@PathVariable String password){
User user=new User(userId,username,password);
userMapper.insert(user);
return "Add user successfully";
}
删除用户
@GetMapping("/user/delete/{userId}")
public String deleteUser(@PathVariable int userId){
userMapper.deleteById(userId);
return "Delete User Successfully";
}