以下是常见的 HTTP 请求类型:
-
GET: 请求指定的资源。通常用于获取数据,不会改变服务器状态。
-
POST: 向指定资源提交数据,通常用于创建资源。
-
PUT: 更新指定资源。如果资源不存在,可能会创建一个新的资源。
-
DELETE: 删除指定的资源。
删除
方法一
注意:前端发送的要是Delete请求且携带参数
// 删除部门
@DeleteMapping("/depts")
public Result delete(HttpServletRequest request){String idStr = request.getParameter("id");int id = Integer.parseInt(idStr);System.out.println("根据ID删除部门: " + id);return Result.success(); // 没有参数可以传
}
这种方法比较繁琐,还要手动转换数据格式
方法二
@DeleteMapping("/depts") //映射HTTP DELETE请求到/depts路径 //从请求中获取名为"id"的参数并转换为Integer类型
public Result delete(@RequestParam(value = "id",required = true) Integer deptId){System.out.println("根据ID删除部门: " + deptId);//打印要删除的部门ID return Result.success();//返回成功结果
}
注意事项:如果声明了@RequestParam,该参数在请求时必须传递,如果不传递将会报错。(默认required为true)
如果请求参数名与形参变量名相同,直接定义方法形参即可收取。(省略@RequestParam)
为了方便,就最好使前端和后端参数名一致
@DeleteMapping("/depts")
public Result delete(Integer id){ System.out.println("根据ID删除部门:"+ id); return Result.success();
}
增加
Controller接收参数
- 接收json格式的请求参数: POST /depts {"name":"教研部"}
- JSON格式的参数,通常会使用一个实体对象进行接收。
- 规则:JSON数据的键名与方法形参对象的属性名称相同,并需要使用@RequestBody注解识别。
使用对象来接收
@PostMapping("/depts") // 接收post请求发送响应
public Result add(@RequestBody Dept dept){ // 方法接受一个JSON格式的请求体,并映射到Dept对象 System.out.println("添加部门: " + dept); // 打印添加的部门信息 deptService.add(dept); // 调用服务层方法添加部门 return Result.success(); // 返回成功的结果
}
// 在service层处理dept的time
@Overridepublic void add(Dept dept) {dept.setUpdateTime(LocalDateTime.now());dept.setCreateTime(LocalDateTime.now());deptMapper.insert(dept);}
mapper文件里的sql语句
@Insert("insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime})")void insert(Dept dept);
注意:添加的数据不要和数据库里已有的数据重名
修改
把按值查询完成
@GetMapping("/depts/{id}")
public Result getInfo(@PathVariable Integer id){ System.out.println("根据ID查询部门数据:" + id); Dept dept = deptService.getInfo(id); return Result.success(dept);
} @Override
public Dept getInfo(Integer id) { return deptMapper.getById(id);
} @Select("select id, name, create_time, update_time from dept where id = #{id}")
Dept getById(Integer id);
修改数据库里的数据
@Update("update dept set name = #{name}, update_time = #{updateTime} where id = #{id}")
void update(Dept dept);
@Override
public void update(Dept dept) { dept.setUpdateTime(LocalDateTime.now()); deptMapper.update(dept);
}
@PutMapping("/depts")
public Result update(@RequestBody Dept dept){ System.out.println("修改部门数据: " + dept); deptService.update(dept); return Result.success();
}
整体代码调整
即Controller类的整体添加
@RequestMapping("/depts")注解
这个是公共路径,而类中的每一个方法前的@XXXMapper表示子路径
@RequestMapping("/depts") // 公共路径
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;// 查询全部部门数据@GetMappingpublic Result list(){System.out.println("查询全部部门数据");List<Dept> deptList = deptService.findAll();System.out.println(deptList);return Result.success(deptList);}// 删除部门@DeleteMappingpublic Result delete(@RequestParam(value = "id",required = true) Integer deptId){System.out.println("根据ID删除部门: " + deptId);deptService.DelByDi(deptId);return Result.success();}@PostMapping // 映射HTTP POST请求到/depts路径public Result add(@RequestBody Dept dept){ // 方法接受一个JSON格式的请求体,并映射到Dept对象System.out.println("添加部门: " + dept); // 打印添加的部门信息deptService.add(dept);// 调用服务层方法添加部门return Result.success(); // 返回成功的结果}// 根据id查询部门@GetMapping("/{id}")public Result getDeptById(@PathVariable Integer id){System.out.println("要查询的是" + id);Dept dept = deptService.SearchById(id);return Result.success(dept);}// 修改部门@PutMapping// @RequestBody用于把json转为对象public Result update(@RequestBody Dept dept){System.out.println("修改部门数据" + dept);deptService.update(dept);return Result.success();}
}