当前位置: 首页 > 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,检索增强生成) 系统。本…...

19c补丁后oracle属主变化,导致不能识别磁盘组

补丁后服务器重启&#xff0c;数据库再次无法启动 ORA01017: invalid username/password; logon denied Oracle 19c 在打上 19.23 或以上补丁版本后&#xff0c;存在与用户组权限相关的问题。具体表现为&#xff0c;Oracle 实例的运行用户&#xff08;oracle&#xff09;和集…...

rknn优化教程(二)

文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK&#xff0c;开始写第二篇的内容了。这篇博客主要能写一下&#xff1a; 如何给一些三方库按照xmake方式进行封装&#xff0c;供调用如何按…...

视频字幕质量评估的大规模细粒度基准

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 摘要 视频字幕在文本到视频生成任务中起着至关重要的作用&#xff0c;因为它们的质量直接影响所生成视频的语义连贯性和视觉保真度。尽管大型视觉-语言模型&#xff08;VLMs&#xff09;在字幕生成方面…...

解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错

出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上&#xff0c;所以报错&#xff0c;到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本&#xff0c;cu、torch、cp 的版本一定要对…...

自然语言处理——循环神经网络

自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元&#xff08;GRU&#xff09;长短期记忆神经网络&#xff08;LSTM&#xff09…...

ArcGIS Pro制作水平横向图例+多级标注

今天介绍下载ArcGIS Pro中如何设置水平横向图例。 之前我们介绍了ArcGIS的横向图例制作&#xff1a;ArcGIS横向、多列图例、顺序重排、符号居中、批量更改图例符号等等&#xff08;ArcGIS出图图例8大技巧&#xff09;&#xff0c;那这次我们看看ArcGIS Pro如何更加快捷的操作。…...

pikachu靶场通关笔记22-1 SQL注入05-1-insert注入(报错法)

目录 一、SQL注入 二、insert注入 三、报错型注入 四、updatexml函数 五、源码审计 六、insert渗透实战 1、渗透准备 2、获取数据库名database 3、获取表名table 4、获取列名column 5、获取字段 本系列为通过《pikachu靶场通关笔记》的SQL注入关卡(共10关&#xff0…...

AI+无人机如何守护濒危物种?YOLOv8实现95%精准识别

【导读】 野生动物监测在理解和保护生态系统中发挥着至关重要的作用。然而&#xff0c;传统的野生动物观察方法往往耗时耗力、成本高昂且范围有限。无人机的出现为野生动物监测提供了有前景的替代方案&#xff0c;能够实现大范围覆盖并远程采集数据。尽管具备这些优势&#xf…...

STM32---外部32.768K晶振(LSE)无法起振问题

晶振是否起振主要就检查两个1、晶振与MCU是否兼容&#xff1b;2、晶振的负载电容是否匹配 目录 一、判断晶振与MCU是否兼容 二、判断负载电容是否匹配 1. 晶振负载电容&#xff08;CL&#xff09;与匹配电容&#xff08;CL1、CL2&#xff09;的关系 2. 如何选择 CL1 和 CL…...

MFE(微前端) Module Federation:Webpack.config.js文件中每个属性的含义解释

以Module Federation 插件详为例&#xff0c;Webpack.config.js它可能的配置和含义如下&#xff1a; 前言 Module Federation 的Webpack.config.js核心配置包括&#xff1a; name filename&#xff08;定义应用标识&#xff09; remotes&#xff08;引用远程模块&#xff0…...