Tlias智能学习辅助系统 04 部门管理 删除 和 新增
- 删除部门API
- DeptController.java
- DeptService.java
- DeptServiceImpl.java
- DeptMapper.java
- 前端联调
- 新增部门API
- 有一步简化
- DeptController.java
- DeptService.java
- DeptServiceImpl.java
- DeptMapper.java
- 前端联调
删除部门API
请求路径:/depts/{id}
请求方式:DELETE
接口描述:该接口用于根据ID删除部门数据
参数 id必须
DeptController.java
/*** 删除部门* @param id*/
// 上次讲到过衍生注释
@DeleteMapping("/depts/{id}")
// @PathVariable 注解用于将 URL 中的模板变量绑定到方法的参数上
public Result delete(@PathVariable Integer id) {log.info("根据ID删除部门: {}", id);// 调用service删除部门deptService.delete(id);return Result.success();
}
DeptService.java
package com.futao.tliasspringbootweb.service;import com.futao.tliasspringbootweb.pojo.Dept;import java.util.List;/*** 部门管理*/
public interface DeptService {/*** 删除部门* @param id*/void delete(Integer id);
}
DeptServiceImpl.java
package com.futao.tliasspringbootweb.service.impl;import com.futao.tliasspringbootweb.mapper.DeptMapper;
import com.futao.tliasspringbootweb.pojo.Dept;
import com.futao.tliasspringbootweb.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;// ....@Overridepublic void delete(Integer id) {deptMapper.deleteById(id);}
}
DeptMapper.java
package com.futao.tliasspringbootweb.mapper;import com.futao.tliasspringbootweb.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);
}
前端联调
新增部门API
请求路径:/depts
请求方式:POST
接口描述:该接口用于添加部门数据
参数 name必须
有一步简化
package com.futao.tliasspringbootweb.controller;import com.futao.tliasspringbootweb.pojo.Dept;
import com.futao.tliasspringbootweb.pojo.Result;
import com.futao.tliasspringbootweb.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;/*** 部门管理Controller*/
@Slf4j
@RestController
@RequestMapping("/depts") //将公共的路径直接抽取到类上
public class DeptController {@Autowiredprivate DeptService deptService;/*** 查询部门数据* @return*/@GetMappingpublic Result list() {log.info("查询全部部门数据");List<Dept> deptList = deptService.list();return Result.success(deptList);}/*** 删除部门* @param id*/@DeleteMapping("/{id}")public Result delete(@PathVariable Integer id) {log.info("根据ID删除部门: {}", id);deptService.delete(id);return Result.success();}/*** 新增部门* @param dept*/@PostMappingpublic Result add(@RequestBody Dept dept) {log.info("新增了部门: {}", dept);deptService.add(dept);return Result.success();}
}
DeptController.java
/*** 新增部门* @param dept*/
@PostMapping
public Result add(@RequestBody Dept dept) {log.info("新增了部门: {}", dept);deptService.add(dept);return Result.success();
}
DeptService.java
package com.futao.tliasspringbootweb.service;import com.futao.tliasspringbootweb.pojo.Dept;
import java.util.List;/*** 部门管理*/
public interface DeptService {/*** 新增部门* @param dept*/void add(Dept dept);
}
DeptServiceImpl.java
package com.futao.tliasspringbootweb.service.impl;import com.futao.tliasspringbootweb.mapper.DeptMapper;
import com.futao.tliasspringbootweb.pojo.Dept;
import com.futao.tliasspringbootweb.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 {@Autowiredprivate DeptMapper deptMapper;@Overridepublic void add(Dept dept) {dept.setCreateTime(LocalDateTime.now());dept.setUpdateTime(LocalDateTime.now());deptMapper.insert(dept);}
}
DeptMapper.java
package com.futao.tliasspringbootweb.mapper;import com.futao.tliasspringbootweb.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 {/*** 新增部门* @param dept*/@Insert("insert into dept(name, create_time, update_time) values (#{name}, #{createTime}, #{updateTime})")void insert(Dept dept);
}
前端联调