主要写controller层和html页面
因为其余层都是用EasyCode生成的
一、controller层
package cn.gsm.demo01.controller;
import cn.gsm.demo01.entity.Student;
import cn.gsm.demo01.service.StudentService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* (Student)表控制层
*
* @author makejava
* @since 2023-04-10 15:48:42
*/
@RestController
@RequestMapping("student")
public class StudentController {
/**
* 服务对象
*/
@Resource
private StudentService studentService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public Student selectOne(Integer id) {
return this.studentService.queryById(id);
}
//查询所有
@GetMapping("selectAll")
public List<Student> selectAll() {
return studentService.queryAll();
}
//插入
@PostMapping("insertStudent")
public void insertStudent(Student student) {
studentService.insert(student);
}
//修改
@PostMapping("updateStudent")
public Student updateStudent(Student student) {
if (student.getId() == null) {
throw new RuntimeException("没有找到该学生Id");
}
return studentService.update(student);
}
@GetMapping("deleteById")
public boolean deleteById(Integer id) {
return studentService.deleteById(id);
}
//分页
@GetMapping("findPage")
public Map<String, Object> findPage(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "5") int size) {
//分页
List<Student> students = studentService.queryAllByLimit((page - 1) * size, size);
long count = studentService.countAll();
long pageCount = count % size == 0 ? count / size : (count / size + 1);//总页数
Map<String, Object> map = new HashMap<>();
map.put("list", students);//对象数据
map.put("count", count