主页 > 开源代码  > 

案例-06.部门管理-根据ID查询

案例-06.部门管理-根据ID查询
一.根据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 { @Autowired private DeptService deptService; // @RequestMapping(value = "/depts",method = RequestMethod.GET) 指定请求方式为GET @GetMapping() // 指定请求方式为GET public Result list(){ log.info("查询全部部门数据"); // 调用service层查询全部部门数据 List<Dept> deptList = deptService.list(); return Result.success(deptList); } @Log @DeleteMapping("{id}") // 指定请求方式为DELETE public Result delete(@PathVariable Integer id) throws Exception { log.info("根据id删除部门:{}",id); // 调用service删除部门 deptService.deleteById(id); return Result.success(); } @Log @PostMapping() // 指定请求方式为Post public 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 { @Autowired private DeptMapper deptMapper; @Override public List<Dept> list() { return deptMapper.list(); } @Override public void deleteById(Integer id) { deptMapper.deleteById(id); } @Override public void add(Dept dept) { dept.setCreateTime(LocalDateTime.now()); dept.setUpdateTime(LocalDateTime.now()); deptMapper.insert(dept); } @Override public 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); }

标签:

案例-06.部门管理-根据ID查询由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“案例-06.部门管理-根据ID查询