KyleWang
发布于 2024-03-21 / 32 阅读
0
0

SpringBoot(九)文章分类添加 三层代码 +非空校验

SpringBoot(九)

文章分类添加 三层代码 +非空校验

@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;//更新时间
}
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

//参数校验
    @PostMapping
    public Result add(@RequestBody @Validated Category category){

        categoryService.add(category);
        return Result.success();
    }

}
public interface CategoryService {

    //新增分类
    void add(Category category);

}
@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> o = ThreadLocalUtil.get();
        Integer id = (Integer) o.get("id");
        category.setCreateUser(id);
        categoryMapper.add(category);
    }
}

@Repository
@Mapper
public interface CategoryMapper {

    @Insert("insert into category(category_name, category_alias, create_user, create_time, update_time)" +
            "value(#{categoryName},#{categoryAlias},#{createUser},#{createTime},#{updateTime}) ")
    void add(Category category);
}


评论