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


前言

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

建表语句和测试用例,在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 {
    @Autowired
    CategoryService 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{
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public 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;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

二、测试

1. 失败(校验)

在这里插入图片描述

2.正常

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


总结

回到顶部

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值