sprintboot notes
框架
- 创建空项目,sprintboot模块
- 使用三层架构设计
- 创建控制层、逻辑层接口、逻辑实现类、持久层
- 准备封装的对象和返回对象
手动封装
使用Results和Result
1
2
3
4
5
6@Results({
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updataTime"),
})
@select("select id, name, create_time, update_time, from tlias order by update_time desc")
public List<Dept> findAll();起别名
1
2@select("select id, name, create_time createTime, update_time updateTime, from tlias order by update_time desc")
public List<Dept> findAll();开启驼峰命名
1
2
3
4mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
使用nginx部署前端
通过HttpServletRequest对象获取
1
2
3
4
5
6@DeleteMapping("/depts")
public Result delete(HttpServletRequest request) {
String idstr = request.getParameter("id");
int id = Integer.parseInt(idstr);
deptService.deleteByid(id);
System.out.println("根据ID删除部门:"+id);后端使用@RequestParam接收参数
1
2
3
4
5
6@DeleteMapping("depts")
public Result deleteByid(@RequestParam("id") Integer deptId) {
deptService.deleteByid(deptId);
System.out.println("删除部门数据:" + deptId);
return Result.success();
}使用@RequestParam默认会绑定参数,且不能为空,可以设置
required属性为false代表参数可选当前端请求的参数名与服务端方法形参一样时,可省略@RequestParam注解 *
1
2
3
4
5
6@DeleteMapping("depts")
public Result deleteByid(Integer id) {
deptService.deleteByid(id);
System.out.println("删除部门数据:" + id);
return Result.success();
}
接收前端的 JSON 数据
1 | @PostMapping("depts") |
@RequestBody注解 接收JSON格式数据并封装到一个对象当中(注:该对象中必须有一个与形参相同的属性)
1 | public class Dept { |
接收路径参数
1 | @GetMapping("depts/{id}") |
@PathVariable注解 将路径参数赋值给形参
当路径名与形参一致时可以省略路径名
1
2
3
4
5
6 @GetMapping("depts/{id}")
public Result getById(@PathVariable Integer id) {
Dept dept = deptService.getById(id);
System.out.println("查询部门数据:" + dept.getName());
return Result.success(dept);
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Lathezero's Blog!
评论