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

news2025/2/22 7:45:58

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
    @NotEmpty
    private String categoryName;//分类名称
    @NotEmpty
    private String categoryAlias;//分类别名
    private Integer createUser;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间

    @Override
    public 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 {
    @Autowired
    private 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 {
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public 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=id
            categoryMapper.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:

 @Override
    public 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
    @NotEmpty
    private String categoryName;//分类名称
    @NotEmpty
    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;//更新时间

运行:

回忆下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:

  @Override
    public 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();
}
 @Override
    public 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);
 @NotNull
    private 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,报错:

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

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

相关文章

RedHat8安装postgresql15和 postgis3.4.4记录及遇到的问题总结

安装包对照版本参考 UsersWikiPostgreSQLPostGIS – PostGIS 如果Red Hat系统上有旧版本的PostgreSQL需要卸载 在较新的Red Hat版本&#xff0c;使用dnf包管理器卸载&#xff1a;sudo dnf remove postgresql-server postgresql 旧版本&#xff0c;使用yum包管理器卸载 sudo y…

深入解析计算机网络请求头:常见类型与安全性影响

目录 1. Host 2. User-Agent 3. Cookie 4. Referer&#xff08;或 Referrer&#xff09; 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是个图片及视频换脸工具&#xff0c;速度快&#xff0c;性能十分强大。 VisoMaster安装有2种方式&#xff0c;根据官网指引安装也十分简单&#xff0c;在此就不重复&#xff0c;只说说安装过程中要注意的事项&#xff1a; 1、自动安装&#xff1a;需要在网络十分畅…

从安装软件到flask框架搭建可视化大屏(二)——创建一个flask页面,搭建可视化大屏,零基础也可以学会

附录&#xff1a;所有文件的完整代码 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(…

[JVM篇]垃圾回收器

垃圾回收器 Serial Seral Old PartNew CMS(Concurrent Mark Sweep) Parallel Scavenge Parallel Old G1 ZGC

DeepSeek专题:DeepSeek-V1核心知识点速览

AIGCmagic社区知识星球是国内首个以AIGC全栈技术与商业变现为主线的学习交流平台&#xff0c;涉及AI绘画、AI视频、大模型、AI多模态、数字人以及全行业AIGC赋能等100应用方向。星球内部包含海量学习资源、专业问答、前沿资讯、内推招聘、AI课程、AIGC模型、AIGC数据集和源码等…

SpringBoot+shardingsphere实现按月分表功能

SpringBootshardingsphere实现按月分表功能 文章目录 前言 ShardingSphere 是一套开源的分布式数据库中间件解决方案&#xff0c;旨在简化数据库分片、读写分离、分布式事务等复杂场景的管理。它由 Apache 软件基金会支持&#xff0c;广泛应用于需要处理大规模数据的系统中 一…

教程 | 从零部署到业务融合:DeepSeek R1 私有化部署实战指南

文章目录 1. 什么是 DeepSeek R1&#xff1f;a. 主要介绍a. 版本区别 2. 部署资源要求a. 硬件资源要求 3. 本地安装DeepSeek-R1a. 为什么选择本地部署&#xff1f;b. 部署工具对比c. 演示环境配置d. Ollama安装流程 4. 可视化工具a. 工具对比b. Open-WebUI部署 5. AI API应用a.…

分布式 NewSQL 数据库(TiDB)

TiDB 是一个分布式 NewSQL 数据库。它支持水平弹性扩展、ACID 事务、标准 SQL、MySQL 语法和 MySQL 协议&#xff0c;具有数据强一致的高可用特性&#xff0c;是一个不仅适合 OLTP 场景还适合 OLAP 场景的混合数据库。 TiDB是 PingCAP公司自主设计、研发的开源分布式关系型数据…

C语言-章节 1:变量与数据类型 ——「未初始化的诅咒」

在那神秘且广袤无垠的「比特大陆」上&#xff0c;阳光奋力地穿过「内存森林」中错综复杂的代码枝叶缝隙&#xff0c;洒下一片片斑驳陆离、如梦似幻的光影。林间的空气里&#xff0c;弥漫着一股浓郁的十六进制锈蚀味&#xff0c;仿佛在诉说着这片森林中隐藏的古老秘密。 一位零基…

HTML的入门

一、HTML HTML&#xff08;HyperText Markup Language&#xff0c;超文本标记语言&#xff09;是一种用来告知浏览器如何组织页面的标记语言。 超文本&#xff1a;就是超越了文本&#xff1b;HTML不仅仅可以用来显示文本(字符串、数字之类)&#xff0c;还可以显示视频、音频等…

闭源大语言模型的怎么增强:提示工程 检索增强生成 智能体

闭源大语言模型的怎么增强 提示工程 检索增强生成 智能体 核心原理 提示工程:通过设计和优化提示词,引导大语言模型进行上下文学习和分解式思考,激发模型自身的思维和推理能力,使模型更好地理解和生成文本,增强其泛用性和解决问题的能力。检索增强生成:结合检索的准确…

【图像加密解密】空间混沌序列的图像加密解密算法复现(含相关性检验)【Matlab完整源码 2期】

1、说明 本文给出详细完整代码、完整的实验报告和PPT。 环境&#xff1a;MATLAB2019a 复现文献&#xff1a;[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打开项目&#xff0c;直接生成即可&#xff1a; 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.前言 此代码是若依移动端版为基础&#xff0c;实现flowable流程管理&#xff0c;支持H5、APP和微信小程序三端。其中&#xff0c;APP是在安卓在雷电模拟器环境下完成的&#xff0c;其他环境未测试&#xff0c;此文章中所提及的APP均指上述环境。移动端是需要配合若依前后端分…

C++:高度平衡二叉搜索树(AVLTree) [数据结构]

目录 一、AVL树 二、AVL树的理解 1.AVL树节点的定义 2.AVL树的插入 2.1更新平衡因子 3.AVL树的旋转 三、AVL的检查 四、完整代码实现 一、AVL树 AVL树是什么&#xff1f;我们对 map / multimap / set / multiset 进行了简单的介绍&#xff0c;可以发现&#xff0c;这几…

2D 游戏艺术、动画和光照

原文&#xff1a;https://unity.com/resources/2d-game-art-animation-lighting-for-artists-ebook 笔记 用Tilemap瓷砖大小为1单元&#xff0c;人物大小在0.5~2单元 PPU &#xff1a;单位像素 pixels per unit 2160 4K分辨率/ 正交相机size*2 完整屏幕显示像素点 有骨骼动…

4、C#基于.net framework的应用开发实战编程 - 测试(四、二) - 编程手把手系列文章...

四、 测试&#xff1b; 四&#xff0e;二、实际运行&#xff1b; 在应用调试完毕&#xff0c;Bug基本解决的时候就需要对应用进行实际运行来进行查看使用体验及分发的准备工作。 1、 运行设置&#xff1b; 在启动项目上右键属性&#xff0c;点击生成&#xff0c;将顶部的配置改…