先查看页面原型明确需求,再根据接口文档开发。
部门查询基本实现
需求:不考虑分页展示;根据最后修改时间倒序排序。
mapper执行SQL语句。
Service调用mapper接口方法。
Controller接收请求,调用service层,响应结果。
controller定义一个方法,方法的返回值意味着给前端返回什么数据,Result。加注解@RequestMapping("/depts")
。接下来调用service,由于service已经交给IOC容器管理,直接注入:面向接口编程,声明deptService接口,使用@AutoWired
注入,然后在方法中调用接口方法。
对于@RequestMapping("/depts")
,没有限定请求方式,应该加属性。@RequestMapping(value="/depts", method=RequestMethod.GET)
。
这样太麻烦,使用@GetMapping @DeleteMapping @Postmapping @PutMapping
结果封装
实体类属性名 和 数据库表查询返回的字段名一致,mybatis会自动封装。
如果实体类属性名 和 数据库表查询返回的字段名不一致,不能自动封装。
比如 LocalDateTime createTime
和数据库中的create_time
,无法自动封装。mapper中从数据查询到后,时间数据并没有封装到实体类里。
解决方法一:手动结果映射
通过 @Results
及@Result
进行手动结果映射。
@Results({@Result(column = "create_time", property = "createTime"),@Result(column = "update_time", property = "updateTime")
})
@Select("select id, name ,create_time, update_time from dept order by update_time desc")
public List<Dept> findAll();
解决方法二:起别名
在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样。
@Select("select id, name ,create_time createTime, update_time updateTime from dept order by update_time desc")
public List<Dept> findAll();
解决方法三:开启驼峰命名-推荐
如果字段名与属性名符合下划线-驼峰命名规则,mybatis会自动通过驼峰命名规则映射。
mybatis:configuration:map-underscore-to-camel-case: true
部门查询前后端联调
前端工程运行在前端服务器 NGINX
nginx部署完得到文件夹,有三个文件夹需要介绍:conf-nginx的配置;html-部署的前端项目;logs-nginx的运行日志。
后端项目开启,tomcat服务器运行,浏览器访问nginx所在ip:90,访问到nginx,那么nginx如何访问到tomcat服务器呢?
nginx反向代理-加中间层的又一次胜利
反向代理是一种网络架构,通过代理服务器为后端的服务器做代理,客户端的请求直接请求代理服务器,然后转发给后端的服务器。
安全、灵活、负载均衡。
反向代理的配置
nginx.conf
server {listen 90;server_name localhost;client_max_body_size 10m;location / {root html;index index.html index.htm;try_files $uri $uri/ /index.html;}location ^~ /api/ { // 匹配/api/开头的路径,匹配到了执行大括号里的规则rewrite ^/api/(.*)$ /$1 break; //将前面的路径重写为后面的路径 ^正则开始,$正则结束,.单个任意字符,*任意次数proxy_pass http://localhost:8080;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
location
:用于定义匹配路径匹配的规则。^~ /api/
:表示精确匹配,即只匹配以/api/开头的路径。-
rewrite
:该指令用于重写匹配到的路径。 proxy_pass
:该指令用于代理转发,它将匹配到的请求转发给位于后端的指令服务器。
部门删除
controller接收参数
/depts?id=2
方式一
通过原始的 HttpServletRequest
对象获取请求参数。
@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();
}
方式二
通过Spring提供的 @RequestParam
注解,将请求参数绑定给方法形参。
@DeleteMapping("/depts")
public Result delete(@RequestParam("id") Integer deptId){System.out.println("根据ID删除部门:"+ deptId);return Result.success();
}
此时必须传递参数,否则会报错。原因在于注解的属性
required = true
,如果想改的话可设为false。不传递为null。
方式三:二的简化-推荐
如果请求参数名与形参变量名相同,直接定义方法形参即可接收。
@DeleteMapping("/depts")
public Result delete(Integer id){System.out.println("根据ID删除部门:"+ id);return Result.success();
}
新增部门
Controller接收请求参数和请求数据。
Service补全基础属性。
Controller接收JSON格式参数
JSON格式的参数,通常会使用一个实体对象进行接收。
规则:JSON数据的键名与方法形参对象的属性名相同,并需要使用@RequestBody
注解标识。如下,这样接收到的JSON数据就会封装到dept这个对象中。
@PostMapping("/depts")
public Result add(@RequestBody Dept dept){System.out.println("添加部门:"+ dept);deptService.add(dept);return Result.success();
}
Mapper传递对象值
mapper在协sql语句传递对象值时,#{}里写的应该是对应的属性名。
@Insert("insert into dept(name,create_time,update_time) values(#{name},#{createTime},#{updateTime})")
void insert(Dept dept);
修改部门
- 查询回显,根据ID查询,使用Restful风格
- 修改数据
Controller接收路径参数
/dept/1
路径参数:通过请求URL直接传递参数,使用{}
来标识该路径参数,需要使用@PathVariable
获取。
@GetMapping("/depts/{id}")
public Result getInfo(@PathVariable("id") Integer deptId){System.out.println("根据ID查询部门数据:"+ deptId);Dept dept = deptService.getInfo(deptId);return Result.success(dept);
}
简化方式,如果路径参数的参数名与方法形参名称一致,省略注解的value值。
@GetMapping("/depts/{id}")
public Result getInfo(@PathVariable Integer id){System.out.println("根据ID查询部门数据:"+ id);Dept dept = deptService.getInfo(id);return Result.success(dept);
}
同一类下路径的抽取
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;@GetMapping("/depts")@DeleteMapping("/depts")@PostMapping("/depts")@GetMapping("/depts/{id}")@PutMapping("/depts")
}
统一抽取,一个完整的请求路径,应该是类上的@RequestMapping
的value属性 + 方法上的@RequestMapping
的value属性
@RequestMapping("/depts")
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;@GetMapping@DeleteMapping@PostMapping@GetMapping("/{id}")@PutMapping
}