6--SpringBootWeb案例(详解)

news2024/9/20 22:35:24

目录

环境搭建

部门管理

查询部门

接口文档

 代码

删除部门

接口文档

代码

新增部门

接口文档

代码


已有前端,根据接口文档完成后端功能的开发

成品如下:

环境搭建

1. 准备数据库表 (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());

2. 创建springboot工程,引入对应的起步依赖(webmybatismysql驱动、lombok

项目工程目录结构:

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

#数据库连接

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

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.configuration.map-underscore-to-camel-case=true

 实体类

/*部门类*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class Dept {

private Integer id;

private String name;

private LocalDateTime createTime;

private LocalDateTime updateTime;

}

/*员工类*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class Emp {

private Integer id;

private String username;

private String password;

private String name;

private Short gender;

private String image;

private Short job;

private LocalDate entrydate;

private Integer deptId;

private LocalDateTime createTime;

private LocalDateTime updateTime;

}

4. 准备对应的 Mapper Service( 接口、实现类 ) Controller 基础结构
访问数据层:
DeptMapper:
package com.itheima.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DeptMapper {
}

EmpMapper:

package com.itheima.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmpMapper {
}

 业务层:

DeptService
package com.itheima.service;
//部门业务规则
public interface DeptService {
}

 DeptServiceImpl:

package com.itheima.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
//部门业务实现类
@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
}
EmpService
package com.itheima.service;
//员工业务规则
public interface EmpService {
}

EmpServiceImpl

package com.itheima.service.impl;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
//员工业务实现类
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
}

控制层:

DeptController
package com.itheima.controller;
import org.springframework.web.bind.annotation.RestController;
//部门管理控制器
@RestController
public class DeptController {
}
EmpController
package com.itheima.controller;
import org.springframework.web.bind.annotation.RestController;
//员工管理控制器
@RestController
public class EmpController {
}

项目工程结构:

在前后端进行交互的时候,我们基于当前主流的REST风格的API接口进行交互。

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

通过URL定位要操作的资源,通过HTTP动词(请求方式)来描述具体的操作。

REST风格的URL中,通过四种请求方式,来操作数据的增删改查。

  • GET : 查询
  • POST :新增
  • PUT :修改
  • DELETE :删除

 前后端工程在进行交互时,使用统一响应结果 Result

package com.itheima.pojo;
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);
}
}

部门管理

查询部门

接口文档

基本信息

请求路径:/depts

请求方式:GET

请求参数:无

响应数据:json格式

 请求参数

响应数据

参数格式application/json

参数说明

参数名类型是否必须备注
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject[ ]非必须返回的数据
|- idnumber非必须id
|- namestring非必须部门名称
|- createTimestring非必须创建时间
|- updateTimestring非必须修改时间

响应数据样例

 {
  "code": 1,
  "msg": "success",
  "data": [
    {
      "id": 1,
      "name": "学工部",
      "createTime": "2022-09-01T23:06:29",
      "updateTime": "2022-09-01T23:06:29"
    },
    {
      "id": 2,
      "name": "教研部",
      "createTime": "2022-09-01T23:06:29",
      "updateTime": "2022-09-01T23:06:29"
    }
  ]
}

 代码

DeptController 

@Slf4j
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
//@RequestMapping(value = "/depts" , method = RequestMethod.GET)
@GetMapping("/depts")
public Result list(){
log.info("查询所有部门数据");
List<Dept> deptList = deptService.list();
return Result.success(deptList);
}
}

 DeptService(业务接口)

public interface DeptService {
/**
* 查询所有的部门数据
* @return 存储Dept对象的集合
*/
List<Dept> list();
}

DeptServiceImpl(业务实现类) 

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> list() {
List<Dept> deptList = deptMapper.list();
return deptList;
}
}

 DeptMapper

@Mapper
public interface DeptMapper {
//查询所有部门数据
@Select("select id, name, create_time, update_time from dept")
List<Dept> list();
}

删除部门

接口文档

基本信息

请求路径:/depts/{id}

请求方式:DELETE

接口描述:该接口用于根据ID删除部门数据

参数格式:路径参数

参数说明:

参数名类型是否必须备注
idnumber必须部门ID

请求参数样例:

/depts/1

响应数据

参数格式:application/json

参数说明:

参数名类型是否必须备注
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject非必须返回的数据

响应数据样例: 

 {
    "code":1,
    "msg":"success",
    "data":null
}

代码

在controller中接收请求路径中的路径参数:@PathVariable

DeptController

@Slf4j
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@DeleteMapping("/depts/{id}")
public Result delete(@PathVariable Integer id) {
//日志记录
log.info("根据id删除部门");
//调用service层功能
deptService.delete(id);
//响应
return Result.success();
}
//省略...
}

DeptService

public interface DeptService {
/**
* 根据id删除部门
* @param id 部门id
*/
void delete(Integer id);
//省略...
}

DeptServiceImpl  

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public void delete(Integer id) {
//调用持久层删除功能
deptMapper.deleteById(id);
}
//省略...
}

DeptMapper

@Mapper
public interface DeptMapper {
/**
* 根据id删除部门信息
* @param id 部门id
*/
@Delete("delete from dept where id = #{id}")
void deleteById(Integer id);
//省略...
}

新增部门

接口文档

基本信息

请求路径:/depts

请求方式:POST

口描述接:该接口用于添加部门数据

请求参数

格式:application/json

参数说明:

参数名类型是否必须备注
namestring必须部门名称

请求参数样例:

{
    "name": "教研部"

响应数据

参数格式:application/json

参数说明:

参数名类型是否必须备注
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject非必须返回的数据

响应参数样例:

参数名类型是否必须备注
codenumber必须响应码,1 代表成功,0 代表失败
msgstring非必须提示信息
dataobject非必须返回的数据

代码

在controller中接收json格式的请求参数:

@RequestBody //把前端传递的json数据填充到实体类中

DeptController

@Slf4j
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/depts")
public Result add(@RequestBody Dept dept){
//记录日志
log.info("新增部门:{}",dept);
//调用service层添加功能
deptService.add(dept);
//响应
return Result.success();
}
//省略...
}

 DeptService

public interface DeptService {
/**
* 新增部门
* @param dept 部门对象
*/
void add(Dept dept);
//省略...
}

DeptServiceImpl 

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public void add(Dept dept) {
//补全部门数据
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
//调用持久层增加功能
deptMapper.insert(dept);
}
//省略...
}
DeptMapper
@Mapper
public interface DeptMapper {
@Insert("insert into dept (name, create_time, update_time) values (#{name},#
{createTime},#{updateTime})")
void insert(Dept dept);
//省略...
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2143009.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

云计算第四阶段------CLOUD Day4---Day6

Cloud DAY4 项目架构图&#xff1a; 环境准备&#xff1a; 主机名称IP地址配置logstash192.168.1.27最低配置4核8G #书接上文&#xff0c;我们在华为云平台租了几台云服务器&#xff0c;这次买一台性能好的服务器&#xff0c;作为logstash软件部署的载体。 今天给小伙伴们带来…

【装机教程】Visual Studio Community 2019离线安装

Visual Studio 2019离线安装 由于现在 官网只支持在线安装最新版的Visual Studio 2022&#xff0c;因此 Visual Studio Community 2019需要离线安装。 下载离线安装镜像&#xff0c;并解压。点击vs_setup.exe运行。 选择安装位置&#xff0c;四处位置需要确定。 选择语言包&…

CSP-J 2019 入门级 第一轮(初赛) 完善程序(1)

【题目】 CSP-J 2019 入门级 第一轮&#xff08;初赛&#xff09; 完善程序&#xff08;1&#xff09; 1.&#xff08;矩阵变幻&#xff09;有一个奇幻的矩阵&#xff0c;在不停的变幻&#xff0c;其变幻方式为&#xff1a; 数字 0 变成矩阵 0 0 0 1 数字 1 变成矩阵 1 1 1 0 …

沉浸式利用自然语言无代码开发工具生成式AI产品应用(下)

背景 小伙伴们过去在开发应用时&#xff0c;经常需要编写大量代码文件以实现业务逻辑&#xff0c;想必肯定有小伙伴开发过类似于快消行业索赔处理、订单库存跟踪和项目审批等系统。去解决这些业务实际问题&#xff0c;我们需要定制地开发业务应用程序为这些问题提供解决方案。…

手写redis实现分布式锁详细教程,满足可续锁、可重入等分布式锁条件

前言 本文将讨论的做一个高并发场景下避不开的话题&#xff0c;即redis分布式锁。比如在淘宝 的秒杀场景、热点新闻和热搜排行榜等。可见分布式锁是一个程序员面向高级的一门必修课&#xff0c;下面请跟着本篇文章好好学习。 redis分布式锁有哪些面试题 1.Redis做分布式的时…

C语言 | Leetcode C语言题解之第404题左叶子之和

题目&#xff1a; 题解&#xff1a; bool isLeafNode(struct TreeNode *node) {return !node->left && !node->right; }int sumOfLeftLeaves(struct TreeNode *root) {if (!root) {return 0;}struct TreeNode **q malloc(sizeof(struct TreeNode *) * 2001);in…

JVM 一个对象是否已经死亡?

目录 前言 引用计数法 可达性分析法 引用 finalize() 方法区回收 前言 虚拟机中垃圾回收器是掌握对象生死的判官, 只要是垃圾回收器认为需要被回收的, 那么这个对象基本可以宣告"死亡". 但是也不是所有的对象, 都需要被回收, 因此, 我们在学习垃圾回收的时候…

Linux系统应用之知识补充——OpenEuler(欧拉)的安装和基础配置

前言 这篇文章将会对OpenEuler的安装进行详解&#xff0c;一步一步跟着走下去就可以成功 注意 &#xff1a;以下的指令操作最好在root权限下进行&#xff08;即su - root&#xff09; ☀️工贵其久&#xff0c;业贵其专&#xff01; 1、OpenEuler的安装 这里我不过多介绍&a…

markdown 使用技巧

文章目录 markdown使用技巧1.标题快捷键设置2.文档可读性设置 markdown使用技巧 1.标题快捷键设置 ctl 1:一级标题 ctl 2:二级标题 ctl 3:三级标题 ctl 4:四级标题 ...2.文档可读性设置 输入~~~pro 可选择代码框&#xff0c;并且可以选择不同的字体 ctrl shift ] : 可…

Flink学习2

创建一个无界流 package com.qyt; import org.apache.flink.api.java.functions.KeySelector; import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.Keye…

《微信小程序实战(2) · 组件封装》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; CSDN入驻不久&#xff0c;希望大家多多支持&#xff0c;后续会继续提升文章质量&#xff0c;绝不滥竽充数…

138、Java内部类源码

01.代码如下&#xff1a; package TIANPAN;class Outer { // 外部类private String msg "Hello World !";class Inner { // 定义一个内部类private String info "世界&#xff0c;你好&#xff0…

【深度分析】OpenAI o1是最强的推理模型,却不是最强模型!

大家好&#xff0c;我是木易&#xff0c;一个持续关注AI领域的互联网技术产品经理&#xff0c;国内Top2本科&#xff0c;美国Top10 CS研究生&#xff0c;MBA。我坚信AI是普通人变强的“外挂”&#xff0c;专注于分享AI全维度知识&#xff0c;包括但不限于AI科普&#xff0c;AI工…

磁盘写操作压力测试工具的设计与实现

磁盘写操作压力测试工具的设计与实现 1. 设计概述2. 关键技术点3. 伪代码设计4. C代码实现5. 运行与测试6. 结论在进行磁盘性能评估时,写操作压力测试是不可或缺的一部分。本篇文章将介绍如何使用C语言结合系统调用,设计并实现一个针对磁盘写操作的压力测试工具。这个工具将模…

【设计模式-桥接】

定义 桥接模式&#xff08;Bridge Pattern&#xff09;是一种结构型设计模式&#xff0c;它通过将抽象部分与实现部分分离&#xff0c;使它们都可以独立地变化。桥接模式的关键在于将类的抽象部分与其实现部分解耦&#xff0c;以便两者可以独立地变化。这种设计模式的一个主要…

湖北产教融合教育研究院成功协办武汉工程大学2024年同等学力申硕开学典礼

9月7日&#xff0c;武汉工程大学&#xff08;流芳校区&#xff09;教育教学综合楼102报告厅内庄严肃穆&#xff0c;近百位怀揣梦想、追求卓越的学子与校领导、教师代表汇聚一堂&#xff0c;共同迎接“乘风破浪 逐光前行”武汉工程大学2024年同等学力申请硕士学位人员开学典礼的…

【觅图网-注册安全分析报告-无验证方式导致安全隐患】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 1. 暴力破解密码&#xff0c;造成用户信息泄露 2. 短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉 3. 带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造…

搜索二叉树的认识以及底层实现

如果说到对一个数组进行查找相应的数据&#xff0c;要求效率最高&#xff0c;大家会想到什么方式呢&#xff1f;二分查找&#xff1f;二分查找的效率确实很高&#xff0c;时间复杂度为O(logN)。但是如果我们想要在数组当中添加新的数据呢&#xff1f;加上这一功能之后二分查找的…

KVM创建的虚拟机无法访问外网

基础环境如下&#xff1a; [rootlocalhost ~]# virsh domifaddr CentOS7_YFName MAC address Protocol Address -------------------------------------------------------------------------------vnet0 52:54:00:cb:a6:0d ipv4 192.168.…

后台数据管理系统 - 项目架构设计-Vue3+axios+Element-plus(0917)

十一、登录注册页面 [element-plus 表单 & 表单校验] 注册登录 静态结构 & 基本切换 安装 element-plus 图标库 pnpm i element-plus/icons-vue静态结构准备 <script setup> import { User, Lock } from element-plus/icons-vue import { ref } from vue cons…