目录
2.postman接口测试:注意请求路径/请求方式/请求参数
2.postman接口测试:注意请求路径/请求方式/请求参数
2.postman接口测试:注意请求路径/请求方式/请求参数

一、查询部门
(一)需求

接口文档




(二)思路

前端发起查询部门的请求
该请求被DeptController进行处理,不能进行数据逻辑处理和服务操作,调用Service层DeptService
而Service是进行逻辑处理的,要操作数据库需要调用Mapper层
通过Mapper接口访问数据库,执行SQL语句,将回去的结果返回给Service
Service将结果进行逻辑处理再返回给Controller
Controller将查询结果封装在同一响应结果封装在Result中,响应给前端
(三)查询部门
①Controller层
DeptController.java
java
package com.itheima.controller;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Slf4j
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/depts")
public Result list(){
log.info("查询全部部门数据");
List <Dept> deptList=deptService.list();
return Result.success(deptList);
}
}
1.注解
@RequestMapping需要指定请求路径和指定请求方式,较为繁琐
@GetMapping只需要指定请求路径即可(PostMapping/DeleteMapping/)
2.输出Mybatis日志
@Slf4j
3.调用方法
创建一个private私有的Service层对象deptService,调用其方法list()
②Service层
1.DeptService.java接口
java
package com.itheima.service;
import com.itheima.pojo.Dept;
import java.util.List;
public interface DeptService {
/**
* 查询全部部门数据
* @return
*/
List<Dept> list();
}
在Service层的接口DeptService中声明方法即可
2.DeptServiceImpl.java实现类
java
package com.itheima.service.impl;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> list() {
return deptMapper.list();
}
}
在Service层的DeptServiceImpl实现类中定义list()方法体
创建一个Mapper层对象,调用其list()方法
③Mapper层
DeptMapper.java接口
java
package com.itheima.mapper;
import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 部门管理
*/
@Mapper
public interface DeptMapper {
/**
* 查询全部部门
*
* @return
*/
@Select("select * from dept")
List<Dept> list();
}
Mapper层仅仅是一个接口,面向注解编程,@Select中编写SQL语句即可
结果


PostMan报错
404:找不到资源,无非就是接口路径,提交方式、参数类型、返回结果类型有问题。
刚开始是请求路径写错了
500:检查数据库密码和名称是否配置正确
(四)、前后端联调
将前后端过程启动起来,然后访问前端工程,通过前端工程访问服务端程序,进而进行调试

二、删除
(一)需求





(二)思路

(三)删除部门
①controller层
DeptController.java
java
package com.itheima.controller;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Slf4j
public class DeptController {
@Autowired
private DeptService deptService;
@DeleteMapping("/depts/{id}")
public Result delete(@PathVariable Integer id){
log.info("根据id删除部门:{}",id);
deptService.delete(id);
//删除操作不需要返回数据,data返回null,调用success
return Result.success();
}
}
②mapper层(接口)
DeptMapper.java
java
package com.itheima.mapper;
import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 部门管理
*/
@Mapper
public interface DeptMapper {
/**
* 根据id删除部门
* @param id
*/
@Delete("delete from dept where id=#{id}")
void deleteById(Integer id);
}
③service层
1.接口
DeptService.java
java
package com.itheima.service;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import java.util.List;
public interface DeptService {
/**
* 根据id删除部门信息
* @return
*/
void delete(Integer id);
}
2.实现类
DepyServiceImpl.java
java
package com.itheima.service.impl;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptMapper deptMapper;
@Override
public void delete(Integer id) {
deptMapper.deleteById(id);
}
}
④结果
注意:同时启动前后端服务器
1.后端数据库

2.postman接口测试:注意请求路径/请求方式/请求参数

3.后端数据库(已删除)

4.前端操作
此时访问前端页面,id=5的部门已经删除

接下来在前端界面执行删除操作,删除id=4的部门

5.后端数据库
此时查看数据库,id=4的部门已删除

注意级联删除
三、新增
(一)需求

(二)思路

我们只需要接收页面传递过来的参数,将参数保存到数据库即可,底层就是Insert语句




(三)新增部门
①controller层
DeptController.java
java
package com.itheima.controller;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Slf4j
public class DeptController {
@Autowired
private DeptService deptService;
/**
* 新增部门
*
* @return
*/
@PostMapping("/depts")
public Result add(@RequestBody Dept dept){
log.info("新增部门:{}",dept);
deptService.add(dept);
return Result.success();
}
}
②service层
1.接口
DeptService.java
java
package com.itheima.service;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import java.util.List;
public interface DeptService {
/**
* 新增部门数据
* @return
*/
void add(Dept dept);
}
2.实现类
DeptServiceImpl.java
java
package com.itheima.service.impl;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptMapper deptMapper;
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
}
③mapper层
DeptMapper.java
java
package com.itheima.mapper;
import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 部门管理
*/
@Mapper
public interface DeptMapper {
/**
* 新增部门信息
*
* @return
*/
@Insert("insert into dept (name,create_time,update_time)values(#{name},#{createTime},#{updateTime}) ")
void insert(Dept dept);
}
④结果
注意:同时启动前后端服务器
1.后端数据库

2.postman接口测试:注意请求路径/请求方式/请求参数

3.后端数据库(已新增)

4.前端操作
此时访问前端页面,就业部已经新增

接下来在前端界面执行新增操作,
5.后端数据库
此时查看数据库,销售部已增加

四、修改
(一)需求

(二)思路
关于P139的修改问题,原理很简单,我们从控制台可以看到,当点击修改按钮时,会发送一个get查询id请求,

然后你需要返回那一个ID的单行数据,然后返回完之后会显示当前修改的部门名称,然后你就可以写修改部门的Put方法,对应了 编辑按钮和确定按钮的请求方式,前端的代码是不需要我们改的,参照接口文档写路径就行
(三)修改部门
①Controller层
DeptController.java
java
package com.itheima.controller;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
log.info("查询部门:{}",id);
Dept dept=deptService.getById(id);
return Result.success(dept);
}
@PutMapping
public Result update(@RequestBody Dept dept){
log.info("修改部门信息:{}",dept);
deptService.update(dept);
return Result.success();
}
}
②Mapper层
DeptMapper.java
java
package com.itheima.mapper;
import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* 部门管理
*/
@Mapper
public interface DeptMapper {
/**
* 根据id查询部门信息
* @param id
* @return
*/
@Select("select * from dept where id=#{id}")
Dept getById(Integer id);
/**
* 更新部门信息
* @param dept
*/
@Update("update dept set name=#{name},create_time=#{createTime},update_time=#{updateTime} where id=#{id}")
void update(Dept dept);
}
③Service层
1.接口
DeptService.java
java
package com.itheima.service;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import java.util.List;
public interface DeptService {
/**
* 根据id查询部门信息
* @param id
* @return
*/
Dept getById(Integer id);
/**
* 更新部门信息
* @param dept
*/
void update(Dept dept);
}
2.实现类
DeptServiceImpl.java
java
package com.itheima.service.impl;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptMapper deptMapper;
/**
* 根据id查询部门信息
* @param id
* @return
*/
@Override
public Dept getById(Integer id) {
return deptMapper.getById(id);
}
/**
* 更新部门信息
* @param dept
*/
@Override
public void update(Dept dept) {
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
④结果
注意:同时启动前后端服务器
1.后端数据库

2.postman接口测试:注意请求路径/请求方式/请求参数
按下编辑,根据id查询部门信息

按下确认修改信息

3.后端数据库(失败)
这里我也不知道为什么postman报错,而前端是正确的

按道理CreateTime不为空才对,@RequestBody把前端响应的JSON参数封装成Dept对象,只传递了id和name,要修改的只有name和updateTime,而前端请求的时候后端响应包括数据库都是在取得,这里我也不知道为什么????????
4.前端操作
点击编辑,发出查询请求,然后回显信息

接下来在前端界面执行修改操作,

5.后端数据库
此时查看数据库,学工部名称已修改

五、简化

公共路径抽取出来
RequestMapping既可以用在类上也可以用在方法上
五、总结
