总说
过程参考黑马程序员SpringBoot3+Vue3全套视频教程,springboot+vue企业级全栈开发从基础、实战到面试一套通关_哔哩哔哩_bilibili
目录
一、功能实现
就是往数据库中添加一个category类,类似于前面写的注册用户操作
1.1 Controller层
创建一个CategoryController
代码如下:
//这里路径直接写到整个controller类的上面,根据接口类型来区分如:Post、Get、Put、Patch
@RestController
@RequestMapping("/category")
public class CategoryController {
//先声明一个service
@Autowired
private CategoryService categoryService;
//添加文章分类
@PostMapping
public Result add(@RequestBody Category category){
categoryService.add(category); //调用service中的add方法
return Result.success();
}
}
1.2 Service层
创建一个CategoryService,注意是接口类型
代码如下:
public interface CategoryService {
//添加文章分类
void add(Category category);
}
1.3 Impl层
创建一个CategoryServiceImpl
要补充创建时间、更新时间、创建者id
@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(); //在线程中获取用户id 就是要传入的创建者id
Integer id = (Integer) map.get("id");
category.setCreateUser(id);
categoryMapper.add(category);
}
}
1.4 Mapper层
创建一个CategoryMapper
代码如下:
@Mapper
public interface CategoryMapper {
//添加文章类型
@Insert("insert into category (category_name,category_alias,create_user,create_time,update_time) " +
"values (#{categoryName},#{categoryAlias},#{createUser},#{createTime},#{updateTime})")
void add(Category category);
}
1.5 测试接口
新建一个接口如下:
注意:
我们上一节学习的更新密码时,可能把登录的那个用户的密码改了,导致token对不上,然后测试的时候就一直401
可以在登录接口修改一下登录密码参数,比如原来是123456改为234567
然后登录获得token,修改一下apifox的全局变量,然后再测试这个接口
成功
二、优化
2.1 参数校验
来到pojo层,在categoryName和categoryAlias字段上添加2个注解@NotWmpty,代码如下:
@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;//更新时间
}
来到CategoryController,添加一个注解,如图:
2.2 查重(自己添加的)——未修改
额看了后面一节,这里写的有点问题,后面再改。
我看视频代码中,添加分类的时候没有查询数据库中是否已经有这个分类,这样可能导致同一个名字的分类被添加多次,我自己添加了查重功能。
类似于用户注册时查重的写法,写一个根据分类名categoryName查找的接口即可
CategoryController层
修改后如下:
//这里路径直接写到整个controller类的上面,根据接口类型来区分如:Post、Get、Put、Patch
@RestController
@RequestMapping("/category")
public class CategoryController {
//先声明一个service
@Autowired
private CategoryService categoryService;
//添加文章分类
@PostMapping
public Result add(@RequestBody @Validated Category category){
Category category1 = categoryService.findByName(category.getCategoryName());
if (category1 == null) {
//没找到
categoryService.add(category); //调用service中的add方法
return Result.success();
}
else { //找到了
return Result.error("已存在");
}
}
}
CategoryService层
添加代码如下:
//根据分类名查询
Category findByName(String categoryName);
CategoryServiceImpl层
添加代码如下:
@Override
public Category findByName(String CategoryName) {
Category category = categoryMapper.findByName(CategoryName);
return category;
}
CategoryMapper层
添加代码如下:
//根据类型名称查询
@Select("select * from category where category_name = #{categoryName}")
Category findByName(String categoryName);
接口测试:
写完上传git保存版本