SpringBoot速成(12)文章分类P15-P20
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 对于update、delete、insert等操作的返回类型通常是int,表示影响的行数,而不是返回一个 POJO 类型。
5.BUG修改
bug描述:
第4中在category类增加了

再运行1.add,报错:

相关文章:
SpringBoot速成(12)文章分类P15-P20
1.新增文章分类 1.Postman登录不上,可以从头registe->login一个新的成员:注意,跳转多个url时,post/get/patch记得修改成controller类中对应方法上写的 2.postman运行成功: 但表中不更新:细节有问题: c是…...
RedHat8安装postgresql15和 postgis3.4.4记录及遇到的问题总结
安装包对照版本参考 UsersWikiPostgreSQLPostGIS – PostGIS 如果Red Hat系统上有旧版本的PostgreSQL需要卸载 在较新的Red Hat版本,使用dnf包管理器卸载:sudo dnf remove postgresql-server postgresql 旧版本,使用yum包管理器卸载 sudo y…...
深入解析计算机网络请求头:常见类型与安全性影响
目录 1. Host 2. User-Agent 3. Cookie 4. Referer(或 Referrer) 5. Authorization 6. Content-Type 7. Content-Length 8. Origin 9. X-Forwarded-For (XFF) 10. Upgrade-Insecure-Requests 11. X-Frame-Options 12. Cache-Control 13. Ac…...
VisoMaster整合包及汉化
VisoMaster是个图片及视频换脸工具,速度快,性能十分强大。 VisoMaster安装有2种方式,根据官网指引安装也十分简单,在此就不重复,只说说安装过程中要注意的事项: 1、自动安装:需要在网络十分畅…...
从安装软件到flask框架搭建可视化大屏(二)——创建一个flask页面,搭建可视化大屏,零基础也可以学会
附录:所有文件的完整代码 models.py # models/models.py from flask_sqlalchemy import SQLAlchemydb SQLAlchemy()class User(db.Model):__tablename__ user # 显式指定表名为 userid db.Column(db.Integer, primary_keyTrue)username db.Column(db.String(…...
【mybatis】基本操作:详解Spring通过注解和XML的方式来操作mybatis
mybatis 的常用配置 配置数据库连接 #驱动类名称 spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver #数据库连接的url spring.datasource.urljdbc:mysql://127.0.0.1:3306/mybatis_test characterEncodingutf8&useSSLfalse #连接数据库的名 spring.datasourc…...
[JVM篇]垃圾回收器
垃圾回收器 Serial Seral Old PartNew CMS(Concurrent Mark Sweep) Parallel Scavenge Parallel Old G1 ZGC...
DeepSeek专题:DeepSeek-V1核心知识点速览
AIGCmagic社区知识星球是国内首个以AIGC全栈技术与商业变现为主线的学习交流平台,涉及AI绘画、AI视频、大模型、AI多模态、数字人以及全行业AIGC赋能等100应用方向。星球内部包含海量学习资源、专业问答、前沿资讯、内推招聘、AI课程、AIGC模型、AIGC数据集和源码等…...
Ubuntu24.04更新国内源
24.04 源文件地址 已经更换为 /etc/apt/sources.list.d/ubuntu.sources sudo vim /etc/apt/sources.list.d/ubuntu.sources把内容替换为 # 阿里云 Types: deb URIs: http://mirrors.aliyun.com/ubuntu/ Suites: noble noble-updates noble-security Components: main restric…...
是时候说再见了
说再见 2018 to 2025 2018:学习 2018年开始读研。师兄师姐们说可以写写CSDN博客,对找工作也有帮助。于是在12月4日,发布了自己的第一篇文章[翻译] 神经网络与深度学习 首页 - Index。当时还在学习各种基础知识,看到了这个英文文…...
SpringBoot+shardingsphere实现按月分表功能
SpringBootshardingsphere实现按月分表功能 文章目录 前言 ShardingSphere 是一套开源的分布式数据库中间件解决方案,旨在简化数据库分片、读写分离、分布式事务等复杂场景的管理。它由 Apache 软件基金会支持,广泛应用于需要处理大规模数据的系统中 一…...
表情识别任务的复现 for 毕设
前言 为了带师弟做毕设,我们复现了表情识别任务。该文章仅用于记录 首先,感谢复现过程中所参考的分享: 【Bilibili】基于卷积神经网络实现的面部表情识别(pytorch)【Github】 facial-expression-recognition【Blog】…...
教程 | 从零部署到业务融合:DeepSeek R1 私有化部署实战指南
文章目录 1. 什么是 DeepSeek R1?a. 主要介绍a. 版本区别 2. 部署资源要求a. 硬件资源要求 3. 本地安装DeepSeek-R1a. 为什么选择本地部署?b. 部署工具对比c. 演示环境配置d. Ollama安装流程 4. 可视化工具a. 工具对比b. Open-WebUI部署 5. AI API应用a.…...
分布式 NewSQL 数据库(TiDB)
TiDB 是一个分布式 NewSQL 数据库。它支持水平弹性扩展、ACID 事务、标准 SQL、MySQL 语法和 MySQL 协议,具有数据强一致的高可用特性,是一个不仅适合 OLTP 场景还适合 OLAP 场景的混合数据库。 TiDB是 PingCAP公司自主设计、研发的开源分布式关系型数据…...
密码管理 - 使用BitWarden/VaultWarden自托管密码服务 - 折腾记录
密码管理 - 使用Bitwarden/Vaultwarden自托管密码服务 - 折腾记录 前言 某天(大约两周前了至少 一直没找到机会记录)逛Github发现了开源的密码管理服务Bitwarden,能自托管、能加密、多端适配。 这,,不是让人如获至宝吗。于是就开始折腾了。…...
C语言-章节 1:变量与数据类型 ——「未初始化的诅咒」
在那神秘且广袤无垠的「比特大陆」上,阳光奋力地穿过「内存森林」中错综复杂的代码枝叶缝隙,洒下一片片斑驳陆离、如梦似幻的光影。林间的空气里,弥漫着一股浓郁的十六进制锈蚀味,仿佛在诉说着这片森林中隐藏的古老秘密。 一位零基…...
机器视觉--Halcon If语句
引言 在机器视觉领域,Halcon 是一款功能强大且广泛应用的软件。在使用 Halcon 进行编程时,条件判断是不可或缺的一部分,而IF语句就是实现条件判断的核心工具之一。通过IF语句,我们能够根据不同的条件执行不同的代码块,…...
Linux基础之文件权限的八进制表示法
1. Linux 文件权限概述 在 Linux 中,每个文件或目录都有三种基本权限,分别是: 读权限 - r:允许查看文件内容。写权限 - w:允许修改文件内容。执行权限 - x:允许执行文件或进入目录。 每个文件或目录的权…...
HTML的入门
一、HTML HTML(HyperText Markup Language,超文本标记语言)是一种用来告知浏览器如何组织页面的标记语言。 超文本:就是超越了文本;HTML不仅仅可以用来显示文本(字符串、数字之类),还可以显示视频、音频等…...
Vue2/Vue3生命周期对比
Vue2的生命周期钩子 beforeCreate 在实例初始化之后,数据观测(data)和事件配置之前调用。此时无法访问 data、methods 等。 created 在实例创建完成后调用。此时可以访问 data、methods,但 DOM 还未生成。 beforeMount 在挂载…...
闭源大语言模型的怎么增强:提示工程 检索增强生成 智能体
闭源大语言模型的怎么增强 提示工程 检索增强生成 智能体 核心原理 提示工程:通过设计和优化提示词,引导大语言模型进行上下文学习和分解式思考,激发模型自身的思维和推理能力,使模型更好地理解和生成文本,增强其泛用性和解决问题的能力。检索增强生成:结合检索的准确…...
【图像加密解密】空间混沌序列的图像加密解密算法复现(含相关性检验)【Matlab完整源码 2期】
1、说明 本文给出详细完整代码、完整的实验报告和PPT。 环境:MATLAB2019a 复现文献:[1]孙福艳,吕宗旺.Digital image encryption with chaotic map lattices[J].Chinese Physics B,2011,20(04):136-142. 2、部分报告内容 3 部分源码与运行步骤 3.1 部…...
QxOrm生成json
下载Qxorm-1.5版本 使用vs打开项目,直接生成即可: lib目录中会生成dll和lib文件 新建Qt项目使用Qxorm: 将QxOrm中上面三个目录拷贝到新建的Qt项目中 pro文件添加使用QxOrm第三方库 INCLUDEPATH $$PWD/include/ LIBS -L"$$PWD/lib" LIBS…...
ASP.NET Core Web应用(.NET9.0)读取数据库表记录并显示到页面
1.创建ASP.NET Core Web应用 选择.NET9.0框架 安装SqlClient依赖包 2.实现数据库记录读取: 引用数据库操作类命名空间 创建查询记录结构类 查询数据并返回数据集合 3.前端遍历数据并动态生成表格显示 生成结果:...
uniapp商城之首页模块
文章目录 前言一、自定义导航栏1.静态结构2.修改页面配置3.组件安全区适配二、通用轮播组件1. 静态结构组件2.自动导入全局组件3.首页轮播图数据获取三、首页分类1.静态结构2.首页获取分类数据并渲染四、热门推荐1.静态结构2.首页获取推荐数据并渲染3.首页跳转详细推荐页五、猜…...
以若依移动端版为基础,实现uniapp的flowable流程管理
1.前言 此代码是若依移动端版为基础,实现flowable流程管理,支持H5、APP和微信小程序三端。其中,APP是在安卓在雷电模拟器环境下完成的,其他环境未测试,此文章中所提及的APP均指上述环境。移动端是需要配合若依前后端分…...
C++:高度平衡二叉搜索树(AVLTree) [数据结构]
目录 一、AVL树 二、AVL树的理解 1.AVL树节点的定义 2.AVL树的插入 2.1更新平衡因子 3.AVL树的旋转 三、AVL的检查 四、完整代码实现 一、AVL树 AVL树是什么?我们对 map / multimap / set / multiset 进行了简单的介绍,可以发现,这几…...
2D 游戏艺术、动画和光照
原文:https://unity.com/resources/2d-game-art-animation-lighting-for-artists-ebook 笔记 用Tilemap瓷砖大小为1单元,人物大小在0.5~2单元 PPU :单位像素 pixels per unit 2160 4K分辨率/ 正交相机size*2 完整屏幕显示像素点 有骨骼动…...
学习笔记-人脸识别相关编程基础
通过编程实现人脸识别功能,需要掌握一定的技术基础,包括编程语言、图像处理、机器学习以及相关的库和框架: 1. 编程语言 Python:Python 是实现人脸识别最常用的语言之一,因为它有大量的库和框架支持,如 Op…...
4、C#基于.net framework的应用开发实战编程 - 测试(四、二) - 编程手把手系列文章...
四、 测试; 四.二、实际运行; 在应用调试完毕,Bug基本解决的时候就需要对应用进行实际运行来进行查看使用体验及分发的准备工作。 1、 运行设置; 在启动项目上右键属性,点击生成,将顶部的配置改…...









