当前位置: 首页 > news >正文

案例实战-Spring boot Web

准备工作

需求&环境搭建

需求:

部门管理:

        查询部门列表

        删除部门

        新增部门

        修改部门

员工管理

        查询员工列表(分页、条件)

        删除员工

        新增员工

        修改员工

 环境搭建

        准备数据库表(dept、emp)

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

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

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


#?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#??????url
spring.datasource.url=jdbc:mysql://localhost:3306/tlias
#?????????
spring.datasource.username=root
#????????
spring.datasource.password=1234#??mybatis???, ????????
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl#??mybatis??????????? a_column ------> aCloumn
mybatis.configuration.map-underscore-to-camel-case=true

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

开发规范

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

Restful

        REST,表述性状态转换,它是一种软件架构分格

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

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

前后端交互统一响应结果Result

package com.itbignyi.tliaswebmanagement.pojp;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {private Integer code;//响应码,1 代表成功; 0 代表失败private String msg;  //响应信息 描述字符串private Object data; //返回的数据//增删改 成功响应public static Result success() {return new Result(1, "success", null);}//查询 成功响应public static Result success(Object data) {return new Result(1, "success", data);}//失败响应public static Result error(String msg) {return new Result(0, msg, null);}
}

部门管理

查询部门

代码如下

package com.itbignyi.tliaswebmanagement.controller;import com.itbignyi.tliaswebmanagement.pojp.Dept;
import com.itbignyi.tliaswebmanagement.pojp.Result;
import com.itbignyi.tliaswebmanagement.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;@GetMapping("/depts")public Result list() {log.info("查询全部部门数据");List<Dept> deptList = deptService.list();return Result.success(deptList);}}
package com.itbignyi.tliaswebmanagement;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class TliasWebManagementApplication {public static void main(String[] args) {SpringApplication.run(TliasWebManagementApplication.class, args);}}
package com.itbignyi.tliaswebmanagement.service;import com.itbignyi.tliaswebmanagement.pojp.Dept;import java.util.List;public interface DeptService {List<Dept> list();
}
package com.itbignyi.tliaswebmanagement.service.impl;import com.itbignyi.tliaswebmanagement.mapper.DeptMapper;
import com.itbignyi.tliaswebmanagement.pojp.Dept;
import com.itbignyi.tliaswebmanagement.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;@Overridepublic List<Dept> list() {return deptMapper.list();}
}
package com.itbignyi.tliaswebmanagement.mapper;import com.itbignyi.tliaswebmanagement.pojp.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface DeptMapper {@Select("select *from tlias.dept")List<Dept> list();
}

运行代码:

前后端联调

删除部门

代码如下:

 @DeleteMapping("/depts/{id}")public Result delete(@PathVariable Integer id) {log.info("根据id删除部门:{}", id);deptService.delete(id);return Result.success();}
   void delete(Integer id);
  @Overridepublic void delete(Integer id) {deptMapper.deleteById(id);}
@Delete("delete from tlias.dept where id=#{id}")void deleteById(Integer id);

 运行如下:

 

新增部门

    @GetMapping("/depts/{id}")public Result selectById(@PathVariable Integer id){log.info("获取id为"+id+"的数据");Dept dept = deptService.selectById(id);return Result.success(dept);}/** 修改部门* */@PutMapping("/depts")public Result update(@RequestBody Dept dept){log.info("修改部门"+dept);deptService.update(dept);return Result.success();}
   @Select("select * from tlias.dept where id=#{id};")Dept selectById(Integer id);@Update("update tlias.dept set name = #{name}, update_time = #{updateTime} where id = #{id}")void update(Dept dept);
    Dept selectById(Integer id);void update(Dept dept);
   @Overridepublic Dept selectById(Integer id){Dept dept = deptMapper.selectById(id);return dept;}@Overridepublic void update(Dept dept){dept.setUpdateTime(LocalDateTime.now());deptMapper.update(dept);}

http://www.lryc.cn/news/164484.html

相关文章:

  • Spring6.1之RestClient分析
  • 冒泡排序、选择排序、插入排序、希尔排序
  • OpenCV(二十三):中值滤波
  • Prompt Tuning训练过程
  • 装备制造企业是否要转型智能装备后服务型公司?
  • day-49 代码随想录算法训练营(19) 动态规划 part 10
  • 检查文件名是否含不可打印字符的C++代码源码
  • 学习笔记-正则表达式
  • Wireshark TS | 网络路径不一致传输丢包问题
  • CMake高级用法实例分析(学习paddle官方的CMakeLists)
  • 数据采集: selenium 自动翻页接口调用时的验证码处理
  • IDEA安装翻译插件
  • DBeaver使用
  • Nougat:一种用于科学文档OCR的Transformer 模型
  • redis八股1
  • 人工智能基础-趋势-架构
  • Date日期工具类(数据库日期区间问题)
  • 为什么需要 TIME_WAIT 状态
  • Linux——(第七章)文件权限管理
  • Scala在大数据领域的崛起:当前趋势和未来前景
  • 前端面试经典题--页面布局
  • 【webrtc】接收/发送的rtp包、编解码的VCM包、CopyOnWriteBuffer
  • Bash常见快捷键
  • 软件验收测试
  • Java 与零拷贝
  • AI性能指标解析:误触率与错误率
  • count(*) 和 count(1) 有什么区别?哪个性能最好?
  • 橡胶密封件为什么会老化?
  • Uboot中bootargs以及bootcmd设置
  • 冠达管理:减肥药概念再度爆发,常山药业两连板,翰宇药业等大涨