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

【第8章】SpringBoot实战篇之文章分类(上)

文章目录

  • 前言
  • 一、后端代码
    • 1. CategoryController
    • 2. service
    • 3. CategoryMapper
    • 4. Category
  • 二、测试
    • 1. 失败(校验)
    • 2.正常
  • 总结


前言

从这开始进入文章相关的接口开发,本章主要介绍定义文章分类接口和新增文章分类

建表语句和测试用例,在SpringBoot专栏首页,此处只涉及后端代码。


一、后端代码

1. CategoryController

package org.example.springboot3.bigevent.controller;import org.example.springboot3.bigevent.entity.Category;
import org.example.springboot3.bigevent.entity.Result;
import org.example.springboot3.bigevent.service.CategoryService;
import org.example.springboot3.bigevent.utils.ThreadLocalUtil;
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.RestController;
import java.util.Map;/*** Create by zjg on 2024/5/26*/
@RestController
public class CategoryController {@AutowiredCategoryService categoryService;@PostMapping("/category")public Result add(@RequestBody @Validated Category category){Map<String, Object> claims = ThreadLocalUtil.get();Integer userId = (Integer) claims.get("userId");category.setCreateUser(userId);int i = categoryService.add(category);if(i!=1){return Result.error("新增文章分类失败,请稍后重试!");}return Result.success("新增文章分类成功");}
}

2. service

package org.example.springboot3.bigevent.service;import org.example.springboot3.bigevent.entity.Category;/*** Create by zjg on 2024/5/26*/
public interface CategoryService {public int add(Category category);
}
package org.example.springboot3.bigevent.service;import org.example.springboot3.bigevent.entity.Category;
import org.example.springboot3.bigevent.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;/*** Create by zjg on 2024/5/26*/
@Service
public class CategoryServiceImpl implements CategoryService{@AutowiredCategoryMapper categoryMapper;@Overridepublic int add(Category category) {category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());return categoryMapper.insert(category);}
}

3. CategoryMapper

package org.example.springboot3.bigevent.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.springboot3.bigevent.entity.Category;/*** Create by zjg on 2024/5/26*/
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}

4. Category

package org.example.springboot3.bigevent.entity;import jakarta.validation.constraints.NotEmpty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;@Getter
@Setter
@ToString
public class Category {@TableId(type= IdType.AUTO)private Integer id;//主键ID@NotEmpty(message = "分类名称不能为空")private String categoryName;//分类名称@NotEmpty(message = "分类别名不能为空")private String categoryAlias;//分类别名private Integer createUser;//创建人IDprivate LocalDateTime createTime;//创建时间private LocalDateTime updateTime;//更新时间
}

二、测试

1. 失败(校验)

在这里插入图片描述

2.正常

在这里插入图片描述
在这里插入图片描述


总结

回到顶部

http://www.lryc.cn/news/364807.html

相关文章:

  • 【QT】Qt Plugin开发
  • 快速了解GPU分布通信技术:PCIe、NVLink与NVSwitch
  • Python对获取数据的举例说明
  • JVMの垃圾回收
  • 人工智能就业方向有哪些?
  • 自定义类型:枚举和联合体
  • 负载均衡加权轮询算法
  • PyTorch 相关知识介绍
  • 1千2初中英语语法题库ACCESS\EXCEL数据库
  • 高德面试:为什么Map不能插入null?
  • MySQL数据库主从配置
  • 测试工程师经常使用的Python中的库,以及对应常用的函数
  • 【frp】服务端配置与systemd启动
  • 计算机网络学习实践:模拟RIP动态路由
  • 详解 Flink 的常见部署方式
  • 【UE5.1 角色练习】11-坐骑——Part1(控制大象移动)
  • 数据结构严蔚敏版精简版-线性表以及c语言代码实现
  • 【react】react项目支持鼠标拖拽的边框改变元素宽度的组件
  • QT 创建文件 Ui 不允许使用不完整类型,可以尝试添加一下任何头文件
  • Python:深入探索其生态系统与应用领域
  • EXCEL从图片链接获取图片
  • Docker迁移默认存储目录(GPT-4o)
  • 植物大战僵尸杂交版2.0.88最新版安装包
  • MQ基础(RabbitMQ)
  • eclipse添加maven插件
  • 知识库系统:从认识到搭建
  • JVM双亲委派模型
  • Python语言与算法:深度探索与实战应用
  • Python实现连连看7
  • C#中的as和is