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

SpringBoot速成(12)文章分类P15-P19

1.新增文章分类

1.Postman登录不上,可以从头registe->login一个新的成员:注意,跳转多个url时,post/get/patch记得修改成controller类中对应方法上写的

2.postman运行成功:

但表中不更新:细节有问题:

c是小写

拼写错误

3.sql层报错,已运行到mapper层->controller层没有对参数校验

参数校验:

1.pojo层

2.controller层

运行后 :

代码展示:

pojo:

package com.itheima.springbootconfigfile.pojo;import jakarta.validation.constraints.NotEmpty;
import lombok.Data;import java.time.LocalDateTime;
@Data
public class Category {private Integer id;//主键ID@NotEmptyprivate String categoryName;//分类名称@NotEmptyprivate String categoryAlias;//分类别名private Integer createUser;//创建人IDprivate LocalDateTime createTime;//创建时间private LocalDateTime updateTime;//更新时间@Overridepublic String toString() {return "Category{" +"id=" + id +", categoryName='" + categoryName + '\'' +", categoryAlias='" + categoryAlias + '\'' +", createUser=" + createUser +", createTime=" + createTime +", updateTime=" + updateTime +'}';}public Category() {}public Category(Integer id, String categoryName, String categoryAlias, Integer createUser, LocalDateTime createTime, LocalDateTime updateTime) {this.id = id;this.categoryName = categoryName;this.categoryAlias = categoryAlias;this.createUser = createUser;this.createTime = createTime;this.updateTime = updateTime;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCategoryName() {return categoryName;}public void setCategoryName(String categoryName) {this.categoryName = categoryName;}public String getCategoryAlias() {return categoryAlias;}public void setCategoryAlias(String categoryAlias) {this.categoryAlias = categoryAlias;}public Integer getCreateUser() {return createUser;}public void setCreateUser(Integer createUser) {this.createUser = createUser;}public LocalDateTime getCreateTime() {return createTime;}public void setCreateTime(LocalDateTime createTime) {this.createTime = createTime;}public LocalDateTime getUpdateTime() {return updateTime;}public void setUpdateTime(LocalDateTime updateTime) {this.updateTime = updateTime;}
}

controller:

package com.itheima.springbootconfigfile.controller;import com.itheima.springbootconfigfile.pojo.Category;
import com.itheima.springbootconfigfile.pojo.Result;
import com.itheima.springbootconfigfile.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;//文章分类
@RestController
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;//新增文章分类@PostMapping("/add")public Result add(@RequestBody @Validated Category category){categoryService.add(category);return  Result.success();}}

categoryServiceIMpl:

package com.itheima.springbootconfigfile.service.impl;import com.itheima.springbootconfigfile.mapper.CategoryMapper;
import com.itheima.springbootconfigfile.pojo.Category;
import com.itheima.springbootconfigfile.service.CategoryService;
import com.itheima.springbootconfigfile.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;@Service
public class CategoryServiceImpl implements CategoryService {@Autowiredprivate CategoryMapper categoryMapper;@Overridepublic void add(Category category) {category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());Map<String,Object> map= ThreadLocalUtil.get();Integer userId= (Integer) map.get("id");category.setCreateUser(userId);//连接user表中的id,即userId=createUser=idcategoryMapper.add(category);}
}

categoryMapper:

package com.itheima.springbootconfigfile.mapper;import com.itheima.springbootconfigfile.pojo.Category;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface CategoryMapper {@Insert("insert into category (categoryName,categoryAlias,createUser,createTime,updateTime) values (#{categoryName},#{categoryAlias},#{createUser},now(),now())")void add(Category category);
}


2.文章分类列表(显示当前用户已有的所有文章分类)

代码展示:

controller:

 //文章分类列表@GetMapping()public  Result<List<Category>> list(){List<Category> cs=categoryService.list();return Result.success(cs);}

categoryServiceImpl:

 @Overridepublic List<Category> list() {Map<String,Object> map=ThreadLocalUtil.get();Integer userId= (Integer) map.get("id");return categoryMapper.list(userId);}

categoryMapper:

  @Select("select * from category where createUser=#{userId}")List<Category> list(Integer userId);

 createUser和userId:

运行:

优化:时间表达不是常规表达

对属性的限制可加在controller类的方法的参数上 或 实体类上

修改:

@Data
public class Category {private Integer id;//主键ID@NotEmptyprivate String categoryName;//分类名称@NotEmptyprivate String categoryAlias;//分类别名private Integer createUser;//创建人ID@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime createTime;//创建时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime updateTime;//更新时间

运行:

回忆下User类对属性的注解:

@Data
public class User {
    @NotNull
    private Integer id;//主键ID
    private String username;//用户名
    @JsonIgnore
    //当前对象转变为json字符串时,忽略password,最终的json 字符串就无password这个属性
    private String password;//密码
    @NotEmpty
    @Pattern(regexp = "^\\S{1,10}$")
    private String nickname;//昵称
    @NotEmpty
    @Email
    private String email;//邮箱


3.获取文章分类详情 

代码展示:

controller:

  //获取文章详情@GetMapping("/detail")public Result<Category> detail(Integer id){Category c=categoryService.findById(id);return Result.success(c);}

categoryServiceImpl:

  @Overridepublic Category findById(Integer id) {Category c =categoryMapper.findById(id);return c;}

categoryMapper:

   @Select("select * from category where id=#{id}")Category findById(Integer id);

运行: 

可优化:category表和user表是联通的,但该方法没要求必须是本用户才可以查询文章分类详情,即可以查到其他用户的文章分类

但若以文章分类是公共的,也可以不限制


4.更新文章分类 

代码展示:

//更新文章分类
@PutMapping("update")
public Result update(@RequestBody @Validated Category category){categoryService.update(category);return Result.success();
}
 @Overridepublic void update(Category category) {category.setUpdateTime(LocalDateTime.now());categoryMapper.update(category);}

 @Update("update category set categoryName=#{categoryName},categoryAlias=#{categoryAlias},updateTime=#{updateTime} where id=#{id}")void update(Category category);
 @NotNullprivate Integer id;//主键ID

运行:

postman修改后,idea重新运行才会成功

可优化:createUser=userId

思考:

为什么不能这样写:

@PutMapping("update")
public Result<Category> update(Integer id){Category c= categoryService.update(id);return Result.success(c);
}

 运行报错:

问题的核心在于 CategoryMapper.update 方法的返回类型不被 MyBatis 支持。MyBatis 对于 updatedeleteinsert 等操作的返回类型通常是 int,表示影响的行数,而不是返回一个 POJO 类型。 


5.BUG修改 

bug描述:

第4中在category类增加了

再运行1.add,报错:

修改:分组校验:

定义分组 对校验项分组 指定分组 默认属于什么组

代码展示:

category:

@Data
public class Category {@NotNull(groups = Update.class)private Integer id;//主键ID@NotEmpty(groups = {Add.class,Update.class})private String categoryName;//分类名称@NotEmpty(groups = {Add.class,Update.class})private String categoryAlias;//分类别名private Integer createUser;//创建人ID@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime createTime;//创建时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime updateTime;//更新时间//分组校验//添加组public interface Add{}//更新组public interface Update{}

categoryController:

//新增文章分类@PostMapping("/add")public Result add(@RequestBody @Validated(Category.Add.class) Category category){categoryService.add(category);return  Result.success();}//更新文章分类@PutMapping("update")public Result update(@RequestBody @Validated(Category.Update.class) Category category){categoryService.update(category);return Result.success();}

 

运行,成功:

优化:如上面的categoryName,categoryAlias(校验项)在add,update中都有,属于default 或 A类继承B类则A继承B所有校验项

category:id属于update(update时notnull),其他属性属于default

@Data
public class Category {@NotNull(groups = Update.class)private Integer id;//主键ID@NotEmptyprivate String categoryName;//分类名称@NotEmptyprivate String categoryAlias;//分类别名private Integer createUser;//创建人ID@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime createTime;//创建时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime updateTime;//更新时间//分组校验//添加组public interface Add extends Default {}//更新组public interface Update extends Default{}

运行:add()

update()

相关文章:

SpringBoot速成(12)文章分类P15-P19

1.新增文章分类 1.Postman登录不上&#xff0c;可以从头registe->login一个新的成员:注意&#xff0c;跳转多个url时&#xff0c;post/get/patch记得修改成controller类中对应方法上写的 2.postman运行成功&#xff1a; 但表中不更新&#xff1a;细节有问题&#xff1a; c是…...

notepad++右键菜单不见了

卸载时没点击完成&#xff0c;又重新安装了一个&#xff0c;最终导致了一些bug&#xff0c;导致右键没有notepad菜单。 解决方式&#xff1a; 新建一个register.reg文件&#xff0c;加入以下代码&#xff0c;然后双击执行即可 代码说明&#xff1a;Open with Notepad 是右…...

Spring 接入 DeepSeek

引入依赖 <dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency>2.yml配置 spring:ai:openai:api-key: sk-xxxxx // 填写自己申请的keybase-url: http…...

开题报告——基于Spring Boot的社区居民健康管理平台的设计与实现

关于本科毕业设计(论文)开题报告的规定 为切实做好本科毕业设计(论文)的开题报告工作,保证论文质量,特作如下规定: 一、开题报告是本科毕业设计(论文)的必经过程,所有本科生在写作毕业设计(论文)之前都必须作开题报告。 二、开题报告主要检验学生对专业知识的驾…...

(leetcode42 前缀后缀最值)接雨水

记忆化&#xff1a;打比方说前缀和 dp数组每个值代表了某一段计算过程 直接取值无需再计算就是记忆化 问题的核心思路 为了计算每个位置能接住多少水&#xff0c;我们需要知道在每个位置上方的水的容量。假设位置 i 是某个柱子的底部&#xff0c;要计算它能接多少水&#xff…...

SpringBoot+uniApp日历备忘录小程序系统 附带详细运行指导视频

文章目录 一、项目演示二、项目介绍三、运行截图四、主要代码1.日历渲染代码&#xff1a;2.保存备忘录代码&#xff1a;3.删除备忘录代码&#xff1a; 一、项目演示 项目演示地址&#xff1a; 视频地址 二、项目介绍 项目描述&#xff1a;这是一个基于SpringBootuniApp框架开…...

分类预测 | MFO-LSSVM飞蛾扑火算法优化最小二乘支持向量机多特征分类预测Matlab实现

分类预测 | MFO-LSSVM飞蛾扑火算法优化最小二乘支持向量机多特征分类预测Matlab实现 目录 分类预测 | MFO-LSSVM飞蛾扑火算法优化最小二乘支持向量机多特征分类预测Matlab实现分类效果基本介绍程序设计参考资料 分类效果 基本介绍 1.Matlab实现MFO-LSSVM飞蛾扑火算法优化最小二…...

Linux 和 Windows 区别

1. 文件组织 (1)目录结构 Linux:采用**单一根目录(/)**结构,所有文件和设备都挂载在这个目录下。 典型目录: /home/(用户目录)/etc/(配置文件)/bin/(系统可执行文件)/dev/(设备文件)/mnt/(挂载点)Windows:采用多个驱动器(C:\, D:\),每个分区是一个独立的…...

Android - Handler使用post之后,Runnable没有执行

问题&#xff1a;子线程创建的Handler。如果 post 之后&#xff0c;在Handler.removeCallbacks(run)移除了&#xff0c;下次再使用Handler.postDelayed(Runnable)接口或者使用post时&#xff0c;Runnable是没有执行。导致没有收到消息。 解决办法&#xff1a;只有主线程创建的…...

vscode通过ssh连接服务器实现免密登录+删除

文章目录 参考&#xff1a; 1、 vscode通过ssh连接服务器实现免密登录删除&#xff08;吐血总结&#xff09;...

Redis未授权访问漏洞原理

redis未授权访问漏洞 目录 redis未授权访问漏洞一、Redis介绍二、redis环境安装三、漏洞原理四、漏洞复现4.1 webshell提权4.2redis写入计划任务反弹shell4.3 ssh key免密登录4.4 Redis基于主从复制的RCE方式 五、Redis加固建议 一、Redis介绍 Redis&#xff0c;全称为Remote …...

喜报!博睿数据案例获经观传媒“2024年度数字转型创新案例”!

本文已在“经观”APP中发表&#xff0c;点击下方文章链接查看原文&#xff1a; 2024科技创变纪&#xff1a;创新破局 变量启新 近日&#xff0c;经济观察报“2024年度卓越创新实践案例”榜单评选结果正式公布。博睿数据选送的案例“从零到一&#xff1a;可观测体系建设的探索…...

【从0做项目】Java搜索引擎(4)——性能优化~烧脑~~~

本篇文章将对项目搜索引擎&#xff08;1&#xff09;~&#xff08;3&#xff09;进行性能优化&#xff0c;包括测试&#xff0c;优化思路&#xff0c;优化前后对比 目录 一&#xff1a;文件读取 二&#xff1a;实现多线程制作索引 1&#xff1a;代码分析 2&#xff1a;代码…...

什么是网络安全审计?网络安全审计的作用...

网络安全审计通过对网络数据的采集、分析、识别&#xff0c;实时动态监测通信内容、网络行为和网络流量&#xff0c;发现和捕获各种敏感信息、违规行为&#xff0c;实时报警响应&#xff0c;全面记录网络系统中的各种会话和事件&#xff0c;实现对网络信息的智能关联分析、评估…...

目前(2025年2月)计算机视觉(CV)领域一些表现优异的深度学习模型

按任务类型分类介绍&#xff1a; 图像分类 CoCa&#xff1a;结合对比学习和生成学习&#xff0c;通过对比损失对齐图像和文本嵌入&#xff0c;并使用标题生成损失预测文本标记。它在图像分类、跨模态检索和图像描述等任务中表现出色&#xff0c;且仅需极少的任务特定微调。 P…...

【核心算法篇十三】《DeepSeek自监督学习:图像补全预训练方案》

引言:为什么自监督学习成为AI新宠? 在传统监督学习需要海量标注数据的困境下,自监督学习(Self-Supervised Learning)凭借无需人工标注的特性异军突起。想象一下,如果AI能像人类一样通过观察世界自我学习——这正是DeepSeek图像补全方案的技术哲学。根据,自监督学习通过…...

【详解】神经网络的发展历程

在人工智能与机器学习的漫长演进史中&#xff0c;神经网络一直扮演着引领创新的关键角色。从最早的生物学启发到当代“深度学习”浪潮&#xff0c;神经网络的发展历程波澜壮阔。随着计算机硬件水平的提升与海量数据的激增&#xff0c;神经网络不仅在学术界受到高度关注&#xf…...

【Linux专栏】find命令+同步 实验

Linux & Oracle相关文档,希望互相学习,共同进步 风123456789~-CSDN博客 1.实验背景 需要把一个目录中所有文件,按照目录把某个时间点之前的同步到一个盘中,之后的同步备份到另一个盘中,实现不同时间段的备份。 本次实现目标:把common文件夹中 2025年之后的含文件夹…...

【弹性计算】虚拟机云服务器

虚拟机云服务器 1.云计算技术概述2.虚拟机云服务器2.1 功能特点2.2 适用场景 “计算” 位居弹性计算的三大件之首&#xff0c;也是弹性计算的主题词。在公共云上&#xff0c;计算产品不仅有既基础又重要的 虚拟机云服务器&#xff0c;而且包含了近年来为了满足用户的多样化需求…...

C++ 中的public、private 和 protected

在 C 里&#xff0c;public、private 和 protected 是用于控制类成员&#xff08;属性和方法&#xff09;访问权限的访问修饰符。合理使用这些访问修饰符能实现数据封装和信息隐藏&#xff0c;增强代码的安全性和可维护性。下面详细介绍它们的特性和用法。 public&#xff08;…...

vite配置scss全局变量

vite配置scss全局变量 创建单独文件variable.scss在其中定义变量 vite.config.ts中配置 import { defineConfig } from vite import vue from vitejs/plugin-vue import path from path// https://vite.dev/config/ export default defineConfig({plugins: [vue()],resolve:…...

Qt开发①Qt的概念+发展+优点+应用+使用

目录 1. Qt的概念和发展 1.1 Qt的概念 1.2 Qt 的发展史&#xff1a; 1.3 Qt 的版本 2. Qt 的优点和应用 2.1 Qt 的优点&#xff1a; 2.2 Qt 的应用场景 2.3 Qt 的应用案例 3. 搭建 Qt 开发环境 3.1 Qt 的开发工具 3.2 Qt SDK 的下载和安装 3.3 Qt 环境变量配置和使…...

FastGPT快速将消息发送至飞书

欢迎关注【AI技术开发者】 在很多企业内部场景下&#xff0c;都需要发送数据到内部交流软件&#xff0c;如飞书、钉钉、企业微信 本文就以飞书为例&#xff0c;企业内部其他同事上报故障后&#xff0c;自动发送消息到飞书&#xff0c; 并相关人员 前文中&#xff0c;使用coz…...

qsort介绍与实现

qsort qsort 是 C 标准库中的一个通用排序函数&#xff0c;位于 <stdlib.h> 头文件中。它可以对任意类型的数组进行排序&#xff0c;使用的是快速排序&#xff08;Quick Sort&#xff09;算法的变种。 参数说明 base&#xff1a;指向要排序的数组的第一个元素的指针。由…...

WPF创建自定义类和控件及打包成dll引用

WPF创建自定义类和控件及打包成dll引用 一、前言二、创建自定义类和控件并生成dll文件2.1创建类库项目2.2创建自定义类和控件2.3生成dll文件 三、在其他项目中引用3.1添加dll文件引用3.2cs文件中引用命名空间3.3XAML文件中引用命名空间 一、前言 出于一些代码复用的需求&#…...

DVWA-DOM型XSS全等级绕过方法

DOM型XSS全等级绕过 前言一、LOW级别二、Medium级别 图片插入语句法 三、High级别 字符 # 绕过服务端过滤 四、Impossible级别 前言 DOM&#xff0c;全称Document Object Model&#xff0c;是一个平台和语言都中立的接口&#xff0c;可以使程序和脚本能够动态访问和更新文档…...

《[含文档+PPT+源码等]精品基于Python实现的Django中药材在线学习系统的设计与实现

基于Python实现的Django中药材在线学习系统的设计与实现背景&#xff0c;可以从以下几个方面进行阐述&#xff1a; 一、行业背景 随着中医药在全球范围内的不断推广和普及&#xff0c;中药材的知识普及和在线学习需求日益增长。传统的中药材学习方式往往受限于地域、时间和资…...

halcon激光三角测量(二十三)inspect_3d_surface_intersections

目录 一、inspect_3d_surface_intersections代码第一部分二、inspect_3d_surface_intersections代码第二部分三、inspect_3d_surface_intersections代码第三部分 一、inspect_3d_surface_intersections代码第一部分 1、创建一个未标定的激光三角测量模型 2、获得参考3D Model&…...

数组和指针常见笔试题(深度剖析)

strlen和sizeof的区别 strlen是统计\0之前的字符个数&#xff0c;传递的是地址 sizeof是计算类型的字节数&#xff0c;其实在编译期间会通过类型来确定大小 下边我来讲一下常见的面试题&#xff0c;过程很详细放心观看 #include<stdio.h>#include <string.h>int …...

【Python爬虫(21)】从0到1:Python与MySQL的深度融合

【Python爬虫】专栏简介&#xff1a;本专栏是 Python 爬虫领域的集大成之作&#xff0c;共 100 章节。从 Python 基础语法、爬虫入门知识讲起&#xff0c;深入探讨反爬虫、多线程、分布式等进阶技术。以大量实例为支撑&#xff0c;覆盖网页、图片、音频等各类数据爬取&#xff…...