赞
踩
通过之前的文章可以快速的了解SpringBoot 项目,这是一个关于增删改查的案例,可以巩固之前学习到的知识。
案例开始
开发员工管理系统,提供增删改查功能。
emp、dept表导入
- # create database testpeople;
-
- -- 导入员工表
- CREATE TABLE if not exists `emp` (
- `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
- `username` varchar(20) NOT NULL COMMENT '用户名',
- `password` varchar(32) DEFAULT '123456' COMMENT '密码',
- `name` varchar(10) NOT NULL COMMENT '姓名',
- `gender` tinyint unsigned NOT NULL COMMENT '性别, 说明: 1 男, 2 女',
- `image` varchar(300) DEFAULT NULL COMMENT '图像',
- `job` tinyint unsigned DEFAULT NULL COMMENT '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
- `entrydate` date DEFAULT NULL COMMENT '入职时间',
- `dept_id` int unsigned DEFAULT NULL COMMENT '部门ID',
- `create_time` datetime NOT NULL COMMENT '创建时间',
- `update_time` datetime NOT NULL COMMENT '修改时间',
- PRIMARY KEY (`id`),
- UNIQUE KEY `username` (`username`)
- ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='员工表';
-
-
- INSERT INTO `emp` VALUES (1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,'2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `emp` VALUES (18,'Tom1','123456','汤姆1',1,'1.jpg',1,'2000-01-01',1,'2023-12-29 08:31:43','2023-12-29 09:03:51');
- INSERT INTO `emp` VALUES (19,'Tom2','123456','汤姆2',1,'1.jpg',1,'2000-01-01',1,'2023-12-29 08:35:50','2023-12-29 08:35:50');
- INSERT INTO `emp` VALUES (20,'Tom3','123456','汤姆3',1,'1.jpg',1,'2000-01-01',1,'2023-12-29 08:37:47','2023-12-29 08:37:47');
-
-
- -- 导入部门表
- CREATE TABLE if not exists `dept` (
- `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
- `name` varchar(10) NOT NULL COMMENT '部门名称',
- `create_time` datetime NOT NULL COMMENT '创建时间',
- `update_time` datetime NOT NULL COMMENT '修改时间',
- PRIMARY KEY (`id`),
- UNIQUE KEY `name` (`name`)
- ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='部门表';
-
-
- INSERT INTO `dept` VALUES (1,'学工部','2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `dept` VALUES (2,'教研部','2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `dept` VALUES (3,'咨询部','2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `dept` VALUES (4,'就业部','2023-12-28 20:23:44','2023-12-28 20:23:44');
- INSERT INTO `dept` VALUES (5,'人事部','2023-12-28 20:23:44','2023-12-28 20:23:44');
后端 (springboot)
web
mybatis
application.properties配置信息
实现实体类
mysql驱动
lombok
controller
service
mapper(dao)
前后端分离开发
Restful
表述性状态转换
软件架构风格
Tips
Cotruller层优化
请求页面相同得可以将方法注解公共部分注解到类上
Cotroller层
- //调用Service层
- @Autowired//依赖注入
- private DeptService deptService;
-
- //记录日志
- // private static Logger log = LoggerFactory.getLogger(DeptController.class);
-
- //查询全部部门数据
- // @RequestMapping(value = "/depts",method = RequestMethod.GET)//请求页面,指定为GET请求
- @GetMapping("/depts")//同上注解
- public Result list(){
- //记录日志
- log.info("查询全部数据");
-
- List<Dept> deptList = deptService.list();
-
- return Result.success(deptList);
-
- }
Service层
- //Mapper接口注入
- @Autowired
- private DeptMapper deptMapper;
-
-
- /**
- * 实现查询所有数据
- */
- @Override
- public List<Dept> list() {
- //返回全部信息
- return deptMapper.list();
- }
Mapper层
- /**
- * 查询所有部门数据
- * @return
- */
- @Select("select * from dept")
- List<Dept> list();
postman
前后端联调
Tips
日志小技巧
- //记录日志
- // private static Logger log = LoggerFactory.getLogger(DeptController.class);
-
- //可以使用
- @Slf4j //注解到类上
Cotroller层
- //调用Service层
- @Autowired//依赖注入
- private DeptService deptService;
- /**
- * 删除部门数据
- */
- @DeleteMapping("/depts/{id}")
- public Result delete(@PathVariable Integer id){
- //日志
- log.info("根据ID删除数据\n");
- log.info("删除部门id为{}",id);
- deptService.delete(id);
- return Result.success();
- }
Service层
- //Mapper接口注入
- @Autowired
- private DeptMapper deptMapper;
-
-
- /**
- * 实现删除部门方法
- */
- @Override
- public void delete(Integer id) {
- deptMapper.deldete(id);
- }
Mapper层
- /**
- * 根据ID 删除数据
- * @param id
- */
- @Delete("delete from dept where id = #{id}")
- void deldete(Integer id);
postman
前后端联调
通过ID查找部门
修改
Cotroller层
- //调用Service层
- @Autowired//依赖注入
- private DeptService deptService;
-
- //通过ID查找部门数据
- @GetMapping("/{id}")
- public Result findById(@PathVariable Integer id){
- //日志
- log.info("根据ID查询部门数据");
- Dept dept = deptService.findById(id);
- return Result.success(dept);
- }
-
- /**
- * 修改部门数据
- */
- @PutMapping
- public Result update(@RequestBody Dept dept){
- //日志
- log.info("修改部门数据");
- deptService.update(dept);
- return Result.success();
- }
Service层
- //Mapper接口注入
- @Autowired
- private DeptMapper deptMapper;
-
-
- /**
- * 实现通过ID查询部门方法
- */
- @Override
- public Dept findById(Integer id) {
- Dept dept = deptMapper.findById(id);
- return dept;
- }
-
- /**
- * 实现更新部门方法
- */
- @Override
- public void update(Dept dept) {
- dept.setUpdateTime(LocalDateTime.now());
- deptMapper.update(dept);
- }
- }
Mapper层
- /**
- * 通过id查询部门
- * @param id
- * @return
- */
- @Select("select * from dept where id = #{id}")
- Dept findById(Integer id);
-
- /**
- * 修改部门
- * @param dept
- */
- @Update("update dept set name = #{name},update_time = #{updateTime} where id = #{id}")
- void update(Dept dept);
postman
通过ID查找
修改
前端联调
通过ID查找
修改
Cotroller层
- //调用Service层
- @Autowired//依赖注入
- private DeptService deptService;
-
- /**
- * 添加部门数据
- */
- @PostMapping("/depts")
- public Result add(@RequestBody Dept dept){
- //日志
- log.info("添加部门名称为{}",dept);
- deptService.add(dept);
- return Result.success();
- }
Service层
- //Mapper接口注入
- @Autowired
- private DeptMapper deptMapper;
-
-
- /**
- * 实现添加部门方法
- */
- @Override
- public void add(Dept dept) {
- //补充数据
- dept.setCreateTime(LocalDateTime.now());
- dept.setUpdateTime(LocalDateTime.now());
- log.info(dept.toString());
- deptMapper.add(dept);
-
- }
Mapper层
- /**
- * 添加部门
- * @param dept
- */
- @Insert("insert into dept(name,create_time,update_time) values (#{name},#{createTime},#{updateTime});")
- void add(Dept dept);
postman
前后端联调
以上为部门管理,小编很快会更新员工管理和登录功能的,敬请期待。
项目地址
admin_web_project: 黑马程序员项目javaWebjavaWeb开发学习仓库,前后端分离项目前端Vue后端springboot数据库Mysql
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。