一.根据ID查询-接口文档
二.根据ID查询-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);}@Log@DeleteMapping("{id}") // 指定请求方式为DELETEpublic Result delete(@PathVariable Integer id) throws Exception {log.info("根据id删除部门:{}",id);// 调用service删除部门deptService.deleteById(id);return Result.success();}@Log@PostMapping() // 指定请求方式为Postpublic Result add(@RequestBody Dept dept) { //RequestBody注解可以将前端在请求时所传递的json格式的数据封装成一个实体类来接受log.info("新增部门:{}",dept);// 调用service新增部门deptService.add(dept);return Result.success();}@GetMapping("{id}")public Result getById(@PathVariable Integer id) {log.info("根据id查询部门信息:{}",id);Dept dept = deptService.getById(id);return Result.success(dept);}
}
查询回来的部门要封装在部门对象Dept中,因此deptService通过getById方法的返回对象要封装在部门类的实现类对象dept中。 并传递给同意响应结果result中作为Result的data属性值,并将Result统一响应结果返回给前端。
三.根据ID查询-service层
package com.gjw.service;import com.gjw.pojo.Dept;import java.util.List;public interface DeptService {List<Dept> list();void deleteById(Integer id) throws Exception;void add(Dept dept);Dept getById(Integer id);
}
在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);}@Overridepublic Dept getById(Integer id) {return deptMapper.getById(id);}
}
通过deptMapper的mapper层的接口方法调用其接口的getById方法,从而封住查询到的部门数据,返回给controller层。
四.根据ID查询-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);/*** 根据id查询部门* @param id*/@Select("select * from dept where id = #{id}")Dept getById(Integer id);
}