延安网站设计,文昌网站 做炸饺子,海外营销平台有哪些,网站推荐正能量一.查询部门-需求 二.查询部门-思路 API接口文档 三.代码实现
1.controller层#xff1a;负责与前端进行交互#xff0c;接收前端所发来的请求
注#xff1a;Slf4j用于记录日志使用#xff0c;可以省略private static Logger log LoggerFactory.getLogger(DeptControlle…一.查询部门-需求 二.查询部门-思路 API接口文档 三.代码实现
1.controller层负责与前端进行交互接收前端所发来的请求
注Slf4j用于记录日志使用可以省略private static Logger log LoggerFactory.getLogger(DeptController.class);这行代码从而直接调用log对象。
注RequestMapping(value /depts,method RequestMethod.GET) 指定请求方式为GET 但是这种请求方式过于麻烦因此使用GetMapping()注解其含义也是请求方式为Get
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 // 记录日志使用
RestControllerpublic class DeptController {Autowiredprivate DeptService deptService;// RequestMapping(value /depts,method RequestMethod.GET) 指定请求方式为GETGetMapping(/depts) // 指定请求方式为GETpublic Result list(){log.info(查询全部部门数据);// 调用service层查询全部部门数据ListDept deptList deptService.list();return Result.success(deptList);}}设置Controller层接收前端发来的Get请求方式url请求地址为/depts的请求后controller层负责调用service层由service层进行逻辑处理。因此通过依赖注入Autowired来注入Service层的对象deptService。最后返回给前端的是一个统一响应结果Result。Result中封装的数据是查询出来的全部部门数据封装在一个list集合当中。
2.service层用来进行逻辑处理并连接dao层将从Dao层获得到的数据返回给controller层
service层接口
package com.gjw.service;import com.gjw.pojo.Dept;import java.util.List;public interface DeptService {ListDept list();
}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 ListDept list() {return deptMapper.list();}}service层中的list方法使用注入的deptMapper对象调用list方法来进行数据的获取。
3.Dao层连接数据库进行数据的获取并返回给service层
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)ListDept list();}使用list方法查询全部的部门数据并以ListDept集合的方式由service层返回到controller层。并在controller层通过统一响应方式Result响应给前端