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

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是…...

C++17中的clamp函数

一、std::clamp() 其实在前面简单介绍过这个函数&#xff0c;但当时只是一个集中的说明&#xff0c;为了更好的理解std::clamp的应用&#xff0c;本篇再详细进行阐述一次。std::clamp在C17中其定义的方式为&#xff1a; template< class T > constexpr const T& cl…...

配置Open-R1,评测第三方蒸馏模型的性能1

年前DeepSeek不温不火&#xff0c;问题的响应极。一回车&#xff0c;就看模型如口吐莲花般&#xff0c;先是输出思维过程&#xff0c;虽然中间绕来绕去&#xff0c;但是输出回答时还是准确而简洁的。比如&#xff0c;用它来读当时出来的几篇文章&#xff0c;确实大大提升了效率…...

Chrome插件开发流程

Chrome插件开发流程可以分为以下几个主要步骤&#xff1a; ### 1. 确定插件功能和目标 在开始开发之前&#xff0c;首先需要明确插件的功能和目标。这包括&#xff1a; - **功能定义**&#xff1a;确定插件要解决的问题或提供的功能。 - **市场分析**&#xff1a;了解目标用户群…...

物联网行业通识:从入门到深度解析

物联网行业通识&#xff1a;从入门到深度解析 &#xff08;图1&#xff1a;物联网生态示意图&#xff09; 一、引言&#xff1a;万物互联时代的到来 根据IDC最新预测&#xff0c;到2025年全球物联网设备连接数将突破410亿&#xff0c;市场规模达1.1万亿美元。物联网&#xff…...

【做一个微信小程序】校园事件页面实现

前言 为了进一步扩展校园事件页面的功能,我们可以添加 搜索、分类筛选 和 渐变卡片色 等特性。以下是详细的方案和源码实现。 扩展功能设计 1. 搜索功能 在页面顶部添加搜索框,用户输入关键词后,筛选出匹配的事件。2. 分类筛选 在页面顶部添加分类标签(如“全部”、“活动…...

C++基础系列【14】继承与多态

博主介绍&#xff1a;程序喵大人 35- 资深C/C/Rust/Android/iOS客户端开发10年大厂工作经验嵌入式/人工智能/自动驾驶/音视频/游戏开发入门级选手《C20高级编程》《C23高级编程》等多本书籍著译者更多原创精品文章&#xff0c;首发gzh&#xff0c;见文末&#x1f447;&#x1f…...

DeepSeek-R1 大模型本地部署指南

文章目录 一、系统要求硬件要求软件环境 二、部署流程1. 环境准备2. 模型获取3. 推理代码配置4. 启动推理服务 三、优化方案1. 显存优化技术2. 性能加速方案 四、部署验证健康检查脚本预期输出特征 五、常见问题解决1. CUDA内存不足2. 分词器警告处理3. 多GPU部署 六、安全合规…...

在conda环境下,安装Pytorch和CUDA

系统 : Ubuntu20.04 显卡&#xff1a;NVIDIA GTX1650 显卡驱动已经装好&#xff08;命令 nvidia-smi 查看显卡配置&#xff09; &#xff08;主要看一下第一行的参数&#xff0c;最大支持的CUDA版本为12.4 &#xff09; Aanconda 版本&#xff08;安装指南&#xff09;(似乎…...

Java里int和Integer的区别?

大家好&#xff0c;我是锋哥。今天分享关于【Java里int和Integer的区别&#xff1f;】面试题。希望对大家有帮助&#xff1b; Java里int和Integer的区别&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 在 Java 中&#xff0c;int 和 Integer 都是用来表…...

【第13章:自监督学习与少样本学习—13.4 自监督学习与少样本学习的未来研究方向与挑战】

凌晨三点的实验室里,博士生小张盯着屏幕上的训练曲线——他设计的跨模态少样本学习模型在医疗影像诊断任务上突然出现了诡异的性能断崖。前一秒还在92%的准确率高位运行,下一秒就暴跌到47%。这个看似灾难性的现象,却意外揭开了自监督学习与少样本学习技术深藏的核心挑战… 一…...

【NLP】文本预处理

目录 一、文本处理的基本方法 1.1 分词 1.2 命名体实体识别 1.3 词性标注 二、文本张量的表示形式 2.1 one-hot编码 2.2 word2vec 模型 2.2.1 CBOW模式 2.2.2 skipgram模式 2.3 词嵌入word embedding 三、文本数据分析 3.1 标签数量分布 3.2 句子长度分布 3.3 词…...

deepseek r1从零搭建本地知识库10:嵌入模型和知识库建设

一、嵌入模型&#xff08;Embedding Model&#xff09;是什么&#xff1f; 1. 定义 嵌入模型是一种将文本、图像、音频等非结构化数据转化为**低维稠密向量&#xff08;Dense Vector&#xff09;**的算法模型&#xff0c;这些向量&#xff08;通常几百到几千维&#xff09;能够…...

Linux-文件IO

1.open函数 【1】基本概念和使用 #include <fcntl.h> int open(const char *pathname&#xff0c;int flags); int open(const char *pathname&#xff0c;int flags&#xff0c;mode_t mode); 功能: 打开或创建文件 参数: pathname //打开的文件名 f…...

3d pose 学习笔记2025

目录 champ nlf 3dpose 2025 55个关键点 推理代码: 要设置环境变量: 依赖项metrabs 渲染代码: tram4d 脚也不是特别好 GVHMR脚对不齐 推理代码: multiperson 2023年 genhmr还没开源: champ https://zhuanlan.zhihu.com/p/700326554 nlf 3dpose 2025 55个关键点…...

LC-随机链表的复制、排序链表、合并K个升序链表、LRU缓存

随机链表的复制 为了在 O(n) 时间复杂度内解决这个问题&#xff0c;并且使用 O(1) 的额外空间&#xff0c;可以利用以下技巧&#xff1a; 将新节点插入到原节点后面&#xff1a;我们可以将复制节点插入到原节点后面。例如&#xff0c;如果链表是 A -> B -> C&#xff0c…...

静态页面在安卓端可以正常显示,但是在ios打开这个页面就需要刷新才能显示全图片

这个问题可能有几个原因导致,我来分析一下并给出解决方案: 首要问题是懒加载实现方式的兼容性问题。当前的懒加载实现可能在 iOS 上不够稳定。建议修改图片懒加载的实现方式: // 使用 Intersection Observer API 实现懒加载 function initLazyLoading() {const imageObserver…...

四元数如何用于 3D 旋转(代替欧拉角和旋转矩阵)【ESP32指向鼠标】

四元数如何用于 3D 旋转&#xff08;代替欧拉角和旋转矩阵&#xff09; 在三维空间中&#xff0c;物体的旋转可以用 欧拉角、旋转矩阵 或 四元数 来表示。 四元数相比于欧拉角和旋转矩阵有 计算更高效、避免万向锁、存储占用少 等优点&#xff0c;因此广泛用于 游戏开发、机器…...

JavaScript 内置对象-日期对象

在JavaScript中&#xff0c;处理日期和时间是一个常见的需求。无论是显示当前时间、计算两个日期之间的差异&#xff0c;还是格式化日期字符串&#xff0c;Date 对象都能提供强大的支持。本文将详细介绍 Date 对象的使用方法&#xff0c;包括创建日期实例、获取和设置日期值、以…...

本地大模型编程实战(19)RAG(Retrieval Augmented Generation,检索增强生成)(3)

文章目录 准备创建矢量数据库对象创建 LangGraph 链将检索步骤转化为工具定义节点构建图 见证效果qwen2.5llama3.1MFDoom/deepseek-r1-tool-calling:7b 总结代码参考 上一篇文章我们演练了一个 用 langgraph 实现的 RAG(Retrieval Augmented Generation,检索增强生成) 系统。本…...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

Python爬虫实战:研究MechanicalSoup库相关技术

一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

基于大模型的 UI 自动化系统

基于大模型的 UI 自动化系统 下面是一个完整的 Python 系统,利用大模型实现智能 UI 自动化,结合计算机视觉和自然语言处理技术,实现"看屏操作"的能力。 系统架构设计 #mermaid-svg-2gn2GRvh5WCP2ktF {font-family:"trebuchet ms",verdana,arial,sans-…...

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1)&#xff1a;从基础到实战的深度解析-CSDN博客&#xff0c;但实际面试中&#xff0c;企业更关注候选人对复杂场景的应对能力&#xff08;如多设备并发扫描、低功耗与高发现率的平衡&#xff09;和前沿技术的…...

cf2117E

原题链接&#xff1a;https://codeforces.com/contest/2117/problem/E 题目背景&#xff1a; 给定两个数组a,b&#xff0c;可以执行多次以下操作&#xff1a;选择 i (1 < i < n - 1)&#xff0c;并设置 或&#xff0c;也可以在执行上述操作前执行一次删除任意 和 。求…...

数据链路层的主要功能是什么

数据链路层&#xff08;OSI模型第2层&#xff09;的核心功能是在相邻网络节点&#xff08;如交换机、主机&#xff09;间提供可靠的数据帧传输服务&#xff0c;主要职责包括&#xff1a; &#x1f511; 核心功能详解&#xff1a; 帧封装与解封装 封装&#xff1a; 将网络层下发…...

HarmonyOS运动开发:如何用mpchart绘制运动配速图表

##鸿蒙核心技术##运动开发##Sensor Service Kit&#xff08;传感器服务&#xff09;# 前言 在运动类应用中&#xff0c;运动数据的可视化是提升用户体验的重要环节。通过直观的图表展示运动过程中的关键数据&#xff0c;如配速、距离、卡路里消耗等&#xff0c;用户可以更清晰…...

掌握 HTTP 请求:理解 cURL GET 语法

cURL 是一个强大的命令行工具&#xff0c;用于发送 HTTP 请求和与 Web 服务器交互。在 Web 开发和测试中&#xff0c;cURL 经常用于发送 GET 请求来获取服务器资源。本文将详细介绍 cURL GET 请求的语法和使用方法。 一、cURL 基本概念 cURL 是 "Client URL" 的缩写…...

抽象类和接口(全)

一、抽象类 1.概念&#xff1a;如果⼀个类中没有包含⾜够的信息来描绘⼀个具体的对象&#xff0c;这样的类就是抽象类。 像是没有实际⼯作的⽅法,我们可以把它设计成⼀个抽象⽅法&#xff0c;包含抽象⽅法的类我们称为抽象类。 2.语法 在Java中&#xff0c;⼀个类如果被 abs…...

9-Oracle 23 ai Vector Search 特性 知识准备

很多小伙伴是不是参加了 免费认证课程&#xff08;限时至2025/5/15&#xff09; Oracle AI Vector Search 1Z0-184-25考试&#xff0c;都顺利拿到certified了没。 各行各业的AI 大模型的到来&#xff0c;传统的数据库中的SQL还能不能打&#xff0c;结构化和非结构的话数据如何和…...