前提一:SpringBoot整合MyBatis
前提二:SpringBoot返回值封装
1、Controller层
/**
* 根据 ID 获取学生信息(参数与取值名称相同)
* @param id
* @return
*/
@GetMapping("/getById/{id}")
public Result getStudentById(@PathVariable int id) {
return Result.success(service.getById(id));
}
/**
* 根据姓名模糊查询学生信息(参数与取值名称不同)
* @param name
* @return
*/
@GetMapping("/getByName/{names}")
public Result getStudentByName(@PathVariable(value = "names", required = false) String name) {
return Result.success(service.getByName(name));
}
/**
* 添加学生信息(多个参数)
* @param name
* @return
*/
@GetMapping("/add/{name}/{age}")
public Result add(@PathVariable String name, @PathVariable int age) {
int code = service.add(name, age);
if (code == 1) {
return Result.success("你已经成功添加了"+ name +"同学!");
}
return Result.error("添加错误!");
}
/**
* 添加学生信息(地址相同无参数)
* @return
*/
@GetMapping("/add")
public Result addStudent() {
StudentPo po = new StudentPo("张伟", 18);
int code = service.addStudent(po);
if (code == 1) {
return Result.success("你已经成功添加了"+ po.getName() +"同学!");
}
return Result.error("添加错误!");
}
/**
* 根据 ID 删除学生
* @param id
* @return
*/
@GetMapping("/delete/{id}")
public Result deleteStudentById(@PathVariable int id) {
int code = service.deleteById(id);
if (code == 1) {
return Result.success("你已经成功删除了ID为"+ id +"的学生!");
}
return Result.error("删除失败!");
}
/**
* 修改学生信息
* @param po
* @return
*/
@GetMapping("/update")
public Result update(StudentPo po) {
int code = service.update(po);
if (code == 1) {
return Result.success("你已经成功修改了"+ po.getName() +"的信息!");
}
return Result.error("修改失败!");
}
2、Service层
/**
* 根据 ID 获取学生信息
* @param id
* @return
*/
public StudentPo getById(int id) {
return dao.getById(id);
}
/**
* 根据 NAME 获取学生信息
* @param name
* @return
*/
public List<StudentPo> getByName(String name) {
return dao.getByName(name);
}
/**
* 添加学生
* @param name
* @param age
* @return
*/
public int add(String name, int age) {
return dao.add(name, age);
}
/**
* 根据 ID 删除
* @param id
* @return
*/
public int deleteById(int id) {
return dao.deleteById(id);
}
/**
* 添加学生(传入对象)
* @param po
* @return
*/
public int addStudent(StudentPo po) {
return dao.addStudent(po);
}
/**
* 修改学生信息
* @param po
* @return
*/
public int update(StudentPo po) {
StudentPo studentPo = dao.getById(po.getId());
studentPo.setName(po.getName());
studentPo.setAge(po.getAge());
return dao.update(studentPo);
}
3、Dao层
// 根据 ID 获取学生信息
@Select("select * from student where id = #{id}")
StudentPo getById(int id);
// 根据 NAME 获取学生信息
@Select("select * from student where name like \"%\"#{name}\"%\"")
List<StudentPo> getByName(String name);
// 添加学生
@Insert("insert into student (name, age) values (#{data1}, #{data2})")
int add(@Param("data1") String name, @Param("data2") int age);
// 添加学生(传入对象)
@Insert("insert into student (name, age) values (#{name}, #{age})")
int addStudent(StudentPo po);
// 根据 ID 删除
@Delete("delete from student where id = #{id}")
int deleteById(int id);
// 修改学生信息
@Update("update student set name = #{name}, age = #{age} where id = #{id}")
int update(StudentPo studentPo);
4、测试:
① http://localhost:8080/student/getById/1
② http://localhost:8080/student/getByName/李四
③ http://localhost:8080/student/add/王五/22
④ http://localhost:8080/student/add
⑤ http://localhost:8080/student/delete/8
⑥ http://localhost:8080/student/update?id=1&name=灰太狼&age=22
- 有关POST请求的一些用法:SpringBoot接收数据(POST)