当前位置:   article > 正文

Java Web —— 第七天(Mybatis案例 部门管理)

Java Web —— 第七天(Mybatis案例 部门管理)

环境搭建

准备数据库表(dept、emp)

  1. -- 部门管理
  2. create table dept(
  3. id int unsigned primary key auto_increment comment '主键ID',
  4. name varchar(10) not null unique comment '部门名称',
  5. create_time datetime not null comment '创建时间',
  6. update_time datetime not null comment '修改时间'
  7. ) comment '部门表';
  8. insert into dept (id, name, create_time, update_time) values(1,'学工部',now(),now()),(2,'教研部',now(),now()),(3,'咨询部',now(),now()), (4,'就业部',now(),now()),(5,'人事部',now(),now());
  9. -- 员工管理(带约束)
  10. create table emp (
  11. id int unsigned primary key auto_increment comment 'ID',
  12. username varchar(20) not null unique comment '用户名',
  13. password varchar(32) default '123456' comment '密码',
  14. name varchar(10) not null comment '姓名',
  15. gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
  16. image varchar(300) comment '图像',
  17. job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
  18. entrydate date comment '入职时间',
  19. dept_id int unsigned comment '部门ID',
  20. create_time datetime not null comment '创建时间',
  21. update_time datetime not null comment '修改时间'
  22. ) comment '员工表';
  23. INSERT INTO emp
  24. (id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES
  25. (1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),
  26. (2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),
  27. (3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),
  28. (4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),
  29. (5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),
  30. (6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),
  31. (7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),
  32. (8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),
  33. (9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),
  34. (10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),
  35. (11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,now(),now()),
  36. (12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,now(),now()),
  37. (13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,now(),now()),
  38. (14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,now(),now()),
  39. (15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),
  40. (16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2007-01-01',2,now(),now()),
  41. (17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());

创建springboot工程,引入对应的起步依赖 (web、mybatis、mysql驱动、lombok)

配置文件application.properties中引入mybatis的配置信息,准备对应的实体类

  1. # 驱动类名称
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. # 数据库连接的URL
  4. spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
  5. # 连接数据库的用户名
  6. spring.datasource.username=root
  7. # 连接数据库的密码
  8. spring.datasource.password=123456
  9. # 配置MyBatis的日志,指定输出到控制台
  10. mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
  11. #开启mybatis的驼峰命名自动映射开关
  12. mybatis.configuration.map-underscore-to-camel-case=true

导入准备对应的Mapper、Service(接口、实现类)、Controller基础结构

开发规范

案例基于当前最为主流的前后端分离模式进行开发

开发规范-Restful

REST(REpresentational State Transfer) ,表述性状态转换,它是一种软件架构风格

注意事项

REST是风格,是约定方式,约定不是规定,可以打破

描述模块的功能通常使用复数,也就是加s的格式来描述,表示此类资源,而非单个资源。如: users、emps、books...

查询部门数据

DeptController 类

  1. package com.example.controller;
  2. import com.example.pojo.Dept;
  3. import com.example.pojo.Result;
  4. import com.example.service.DeptService;
  5. import lombok.RequiredArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import java.util.List;
  15. /**
  16. * @author hyk~
  17. */
  18. @Slf4j
  19. @RestController
  20. public class DeptController {
  21. @Autowired
  22. private DeptService deptService;
  23. //private static Logger log = LoggerFactory.getLogger(DeptController.class);
  24. //@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
  25. @GetMapping("/depts")
  26. public Result list(){
  27. log.info("查询全部部门数据");
  28. //调用Service查询部门数据
  29. List<Dept> deptList = deptService.list();
  30. return Result.success(deptList);
  31. }
  32. }

DeptServiceImpl 类 

  1. @Service
  2. public class DeptServiceImpl implements DeptService {
  3. @Autowired
  4. private DeptMapper deptMapper;
  5. @Override
  6. public List<Dept> list() {
  7. return deptMapper.list();
  8. }
  9. }

DeptMapper 类 

  1. @Mapper
  2. public interface DeptMapper {
  3. //查询全部部门数据
  4. @Select("select * from dept")
  5. List<Dept> list();
  6. }

根据id删除部门数据

DeptController类
  1. //根据id删除部门
  2. @DeleteMapping("/{id}")
  3. public Result deleteDept(@PathVariable Integer id) { //@PathVariable 通过该注解来获取路径中id的值/{id}
  4. log.info("根据id删除部门数据:{}", id);
  5. //调用Service删除部门
  6. deptService.DeleteDept(id);
  7. return Result.success();
  8. }
DeptService接口
  1. //根据id删除部门信息
  2. void DeleteDept(Integer id);
DeptServiceImpl实现类
  1. @Override
  2. public void DeleteDept(Integer id) {
  3. deptMapper.DeleteDept(id);
  4. }
DeptMapper接口
  1. //根据Id删除部门信息
  2. @Delete("delete from dept where id = #{id}")
  3. void DeleteDept(Integer id);

整体代码(增删改查)

DeptController类(处理请求,返回响应)
  1. package com.example.controller;
  2. import com.example.pojo.Dept;
  3. import com.example.pojo.Emp;
  4. import com.example.pojo.Result;
  5. import com.example.service.DeptService;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.apache.ibatis.annotations.Insert;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import java.util.List;
  14. /**
  15. * @author hyk~
  16. */
  17. @Slf4j
  18. @RestController
  19. @RequestMapping("/depts")
  20. public class DeptController {
  21. @Autowired
  22. private DeptService deptService;
  23. //private static Logger log = LoggerFactory.getLogger(DeptController.class);
  24. //@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
  25. @GetMapping
  26. public Result list() {
  27. log.info("查询全部部门数据");
  28. //调用Service查询部门数据
  29. List<Dept> deptList = deptService.list();
  30. return Result.success(deptList);
  31. }
  32. //根据id删除部门
  33. @DeleteMapping("/{id}")
  34. public Result deleteDept(@PathVariable Integer id) { //@PathVariable 通过该注解来获取路径中id的值/{id}
  35. log.info("根据id删除部门数据:{}", id);
  36. //调用Service删除部门
  37. deptService.DeleteDept(id);
  38. return Result.success();
  39. }
  40. //添加部门
  41. @PostMapping
  42. public Result addDept(@RequestBody Dept dept) {
  43. log.info("添加部门数据{}", dept);
  44. deptService.addDept(dept);
  45. return Result.success();
  46. }
  47. @GetMapping("/{id}")
  48. public Result selectByDeptId(@PathVariable Integer id) {
  49. //日志记录
  50. log.info("根据id查询部门:{}", id);
  51. //调用service层功能
  52. Dept dept = deptService.selectByDeptId(id);
  53. //响应
  54. return Result.success(dept);
  55. }
  56. @PutMapping
  57. public Result update(@RequestBody Dept dept) {
  58. //日志记录
  59. log.info("修改部门:{}", dept);
  60. //调用service层功能
  61. deptService.update(dept);
  62. //响应
  63. return Result.success();
  64. }
  65. }
DeptService接口(业务逻辑)
  1. package com.example.service;
  2. import com.example.pojo.Dept;
  3. import org.springframework.stereotype.Service;
  4. import java.util.List;
  5. /**
  6. * @author hyk~
  7. */
  8. @Service
  9. public interface DeptService {
  10. //查询全部部门数据
  11. List<Dept> list();
  12. //根据id删除部门信息
  13. void DeleteDept(Integer id);
  14. //添加部门信息
  15. void addDept(Dept dept);
  16. //根据ID查询部门
  17. Dept selectByDeptId(Integer id);
  18. //修改部门
  19. void update(Dept dept);
  20. }

DeptServiceImpl实现类
  1. package com.example.service.impl;
  2. import com.example.mapper.DeptMapper;
  3. import com.example.mapper.EmpMapper;
  4. import com.example.pojo.Dept;
  5. import com.example.service.DeptService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.time.LocalDateTime;
  9. import java.util.List;
  10. /**
  11. * @author hyk~
  12. */
  13. @Service
  14. public class DeptServiceImpl implements DeptService {
  15. @Autowired
  16. private DeptMapper deptMapper;
  17. //查询
  18. @Override
  19. public List<Dept> list() {
  20. return deptMapper.list();
  21. }
  22. //删除
  23. @Override
  24. public void DeleteDept(Integer id) {
  25. deptMapper.DeleteDept(id);
  26. }
  27. //新增
  28. @Override
  29. public void addDept(Dept dept) {
  30. dept.setCreateTime(LocalDateTime.now());
  31. dept.setUpdateTime(LocalDateTime.now() );
  32. deptMapper.addDept(dept);
  33. }
  34. //根据id查询
  35. @Override
  36. public Dept selectByDeptId(Integer id) {
  37. return deptMapper.selectByDeptId(id);
  38. }
  39. //修改
  40. @Override
  41. public void update(Dept dept) {
  42. //设置修改时间为当前
  43. dept.setUpdateTime(LocalDateTime.now());
  44. deptMapper.update(dept);
  45. }
  46. }
DeptMapper接口(数据访问)
  1. package com.example.mapper;
  2. import com.example.pojo.Dept;
  3. import org.apache.ibatis.annotations.*;
  4. import java.util.List;
  5. /**
  6. * @author hyk~
  7. */
  8. @Mapper
  9. public interface DeptMapper {
  10. //查询全部部门数据
  11. @Select("select * from dept")
  12. List<Dept> list();
  13. //根据Id删除部门信息
  14. @Delete("delete from dept where id = #{id}")
  15. void DeleteDept(Integer id);
  16. //添加部门信息
  17. @Insert(" insert into dept(name, create_time, update_time) values(#{name},#{createTime},#{updateTime});")
  18. void addDept(Dept dept);
  19. //根据id查询数据 数据回显 方便用户修改
  20. @Select("select * from dept where id=#{id}")
  21. Dept selectByDeptId(Integer id);
  22. //修改数据
  23. @Update("update dept set name = #{name},update_time=now() where id =#{id}")
  24. void update(Dept dept);
  25. }

总结

1.开发流程

明确需求

接口文档

思路分析

接口开发

2.接口调试

postman测试

前后端联调

3.日志小技巧
  1. @slf4j
  2. @RestController
  3. public class DeptController {
  4. }

注意事项

一个完整的请求路径,应该是类上的 @RequestMapping 的value属性 + 方法上的 @RequestMapping的value属性。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1020876
推荐阅读
相关标签
  

闽ICP备14008679号