一.新增部门-需求

二.部门管理-需求说明

三.添加部门-接口文档


响应数据


请求参数是一个Json格式的数据,因此要使用@RequestBody来修饰对象用来接受前端传过来的Json格式的数据,而Dept类中含有name属性,因此使用Dept的类的实例化对象来接受Json格式的数据。
四.Controller层
package com.gjw.controller;/*** 部门管理Controller*/import com.gjw.anno.Log;
import com.gjw.pojo.Dept;
import com.gjw.pojo.Result;
import com.gjw.service.DeptService;
import com.gjw.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@Slf4j // 记录日志使用
@RestController
@RequestMapping("/depts")
public class DeptController {@Autowiredprivate DeptService deptService;// @RequestMapping(value = "/depts",method = RequestMethod.GET) 指定请求方式为GET@GetMapping() // 指定请求方式为GETpublic Result list(){log.info("查询全部部门数据");// 调用service层查询全部部门数据List<Dept> deptList = deptService.list();return Result.success(deptList);}@DeleteMapping("{id}") // 指定请求方式为DELETEpublic Result delete(@PathVariable Integer id) throws Exception {log.info("根据id删除部门:{}",id);// 调用service删除部门deptService.deleteById(id);return Result.success();}@PostMapping() // 指定请求方式为Postpublic Result add(@RequestBody Dept dept) { //RequestBody注解可以将前端在请求时所传递的json格式的数据封装成一个实体类来接受log.info("新增部门:{}",dept);// 调用service新增部门deptService.add(dept);return Result.success();}
}
list()方法,delete()方法和add()方法的url地址都含有“depts”字段,为了使代码变得更加简洁,我们将“depts”字段使用@RequestMapping注解将"/depts"提出来简化代码。
五.Service层
package com.gjw.service.impl;import com.gjw.mapper.DeptLogMapper;
import com.gjw.mapper.DeptMapper;
import com.gjw.mapper.EmpMapper;
import com.gjw.pojo.Dept;
import com.gjw.pojo.DeptLog;
import com.gjw.service.DeptLogService;
import com.gjw.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.time.LocalDateTime;
import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;@Overridepublic List<Dept> list() {return deptMapper.list();}@Overridepublic void deleteById(Integer id) {deptMapper.deleteById(id);}@Overridepublic void add(Dept dept) {dept.setCreateTime(LocalDateTime.now());dept.setUpdateTime(LocalDateTime.now());deptMapper.insert(dept);}
}
在service层要增加Dept的实现类对象的创建时间和更新时间这两个属性。
六.mapper层
package com.gjw.mapper;import com.gjw.anno.Log;
import com.gjw.pojo.Dept;
import org.apache.ibatis.annotations.*;import java.util.List;/*** 部门管理*/
@Mapper
public interface DeptMapper {/*** 查询全部部门数据* @return*/@Select("select * from dept")List<Dept> list();/*** 根据id删除部门数据* @param id*/@Delete("delete from dept where id = #{id}")void deleteById(Integer id);/*** 根据部门名称添加部门* @param dept*/@Insert("insert into dept(name, create_time, update_time) VALUES (#{name},#{createTime},#{updateTime})")void insert(Dept dept);
}
将新创建的Dept类的实现类对象加入到数据库当中。