文章目录
- 前言
- 一、后端代码
- 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.正常
总结
回到顶部