文章目录
前言
上篇文件介绍了新增文章分类,本章主要介绍:
- 获取文章分类列表
- 获取文章分类详情
- 更新文章分类
- 删除文章分类
- 更新和新增文章分类(分组校验)
一、获取文章分类列表
1. CategoryController
@RestController
public class CategoryController {
@Autowired
CategoryService categoryService;
@GetMapping("/category")
public Result<List<Category>> queryList(){
Map<String, Object> claims =ThreadLocalUtil.get();
Integer userId = (Integer) claims.get("userId");
List<Category> categories = categoryService.selectList(userId);
return Result.success(categories);
}
}
2. service
public interface CategoryService {
public List<Category> selectList(Integer userId);
}
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
public List<Category> selectList(Integer userId) {
QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("create_user",userId);
return categoryMapper.selectList(queryWrapper);
}
}
3. Category
package org.example.springboot3.bigevent.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
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
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;//创建时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;//更新时间
}
4. 测试
二、获取文章分类详情(单条)
1.CategoryController
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
CategoryService categoryService;
@GetMapping("detail")
public Result<Category> detail(Integer id){
Map<String, Object> claims =ThreadLocalUtil.get();
Integer userId = (Integer) claims.get("userId");
Category categories = categoryService.selectOne(id,userId);
return Result.success(categories);
}
}
2.CategoryService
public interface CategoryService {
public int add(Category category);
public List<Category> selectList(Integer userId);
Category selectOne(Integer id,Integer userId);
}
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
@Override
public Category selectOne(Integer id, Integer userId) {
QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",id);
queryWrapper.eq("create_user",userId);
return categoryMapper.selectOne(queryWrapper);
}
}
3. 测试
三、更新文章分类
1.CategoryController
@RestController
@RequestMapping("/category")
public class CategoryController {
@PutMapping
public Result update(@RequestBody @Validated Category category){
Map<String, Object> claims =ThreadLocalUtil.get();
Integer userId = (Integer) claims.get("userId");
category.setCreateUser(userId);
int i = categoryService.update(category);
if(i!=1){
return Result.error("更新文章分类失败");
}
return Result.success("更新文章分类成功");
}
}
2.CategoryService
public interface CategoryService {
public int add(Category category);
public List<Category> selectList(Integer userId);
Category selectOne(Integer id,Integer userId);
public int update(Category category) ;
}
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
@Override
public int update(Category category) {
UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
wrapper.set("category_name",category.getCategoryName());
wrapper.set("category_alias",category.getCategoryAlias());
wrapper.set("update_time",LocalDateTime.now());
wrapper.eq("id",category.getId());
wrapper.eq("create_user",category.getCreateUser());
return categoryMapper.update(wrapper);
}
}
3.测试
四、删除文章分类
1.CategoryController
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
CategoryService categoryService;
@DeleteMapping
public Result delete(Integer id){
Map<String, Object> claims =ThreadLocalUtil.get();
Integer userId = (Integer) claims.get("userId");
int i = categoryService.delete(id,userId);
if(i!=1){
return Result.error("删除文章分类失败");
}
return Result.success("删除文章分类成功");
}
}
2. CategoryService
public interface CategoryService {
public int delete(Integer id, Integer userId);
}
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
CategoryMapper categoryMapper;
@Override
public int delete(Integer id, Integer userId) {
UpdateWrapper<Category> wrapper = new UpdateWrapper<>();
wrapper.eq("id",id);
wrapper.eq("create_user",userId);
return categoryMapper.delete(wrapper);
}
}
3.测试
五、更新和新增文章分类(分组校验)
1. 定义分组
package org.example.springboot3.bigevent.valid;
import jakarta.validation.groups.Default;
/**
* Create by zjg on 2024/5/26
*/
public class ValidatedGroups {
public interface Insert extends Default {};
public interface Delete extends Default {};
public interface Update extends Default {};
public interface Selete extends Default {};
}
2. 使用分组
package org.example.springboot3.bigevent.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.example.springboot3.bigevent.valid.ValidatedGroups;
import java.time.LocalDateTime;
@Getter
@Setter
@ToString
public class Category {
@NotNull(message = "id不能为空",groups = {ValidatedGroups.Update.class})
@TableId(type= IdType.AUTO)
private Integer id;//主键ID
@NotEmpty(message = "分类名称不能为空")
private String categoryName;//分类名称
@NotEmpty(message = "分类别名不能为空")
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;//更新时间
}
3. 使用分组校验
查询和删除都是接收单个id参数,保证id不为空即可完成对应功能,多参数使用方式可参考新增。
@PostMapping
public Result add(@RequestBody @Validated(ValidatedGroups.Insert.class) 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("新增文章分类成功");
}
@PutMapping
public Result update(@RequestBody @Validated(ValidatedGroups.Update.class) Category category){
Map<String, Object> claims =ThreadLocalUtil.get();
Integer userId = (Integer) claims.get("userId");
category.setCreateUser(userId);
int i = categoryService.update(category);
if(i!=1){
return Result.error("更新文章分类失败");
}
return Result.success("更新文章分类成功");
}