新增文章分类

pojo.Category

java 复制代码
package com.lin.springboot01.pojo;

import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class Category {
    private Integer id;//主键
    @NotEmpty
    private String categoryName;//分类名称
    @NotEmpty
    private String categoryAlias;//分类别名
    private Integer createUser;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

controller.CategoryController

java 复制代码
package com.lin.springboot01.controller;

import com.lin.springboot01.pojo.Category;
import com.lin.springboot01.pojo.Result;
import com.lin.springboot01.service.CategoryService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,
// 返回json数据不需要在方法前面加@ResponseBody注解了,
// 但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    //@RequestBody,请求参数形式必须是json格式,表格提单不可以用。去掉就可以使用表单提交,不能用json提交
    public Result add(@RequestBody @Validated Category category){
        categoryService.add(category);
        return Result.success();
    }
}

service.CategoryService

java 复制代码
package com.lin.springboot01.service;

import com.lin.springboot01.pojo.Category;

public interface CategoryService {
    void add(Category category);
}

service.impl.CategoryServiceImpl

java 复制代码
package com.lin.springboot01.service.impl;

import com.lin.springboot01.mapper.CategoryMapper;
import com.lin.springboot01.pojo.Category;
import com.lin.springboot01.service.CategoryService;
import com.lin.springboot01.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

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

mapper.CategoryMapper

java 复制代码
package com.lin.springboot01.mapper;

import com.lin.springboot01.pojo.Category;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

@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);
}

完成:

相关推荐
gnip9 小时前
企业级配置式表单组件封装
前端·javascript·vue.js
一只叫煤球的猫10 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
excel11 小时前
Three.js 材质(Material)详解 —— 区别、原理、场景与示例
前端
掘金安东尼11 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
皮皮林55111 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
hj5914_前端新手15 小时前
javascript基础- 函数中 this 指向、call、apply、bind
前端·javascript
薛定谔的算法15 小时前
低代码编辑器项目设计与实现:以JSON为核心的数据驱动架构
前端·react.js·前端框架
Hilaku15 小时前
都2025年了,我们还有必要为了兼容性,去写那么多polyfill吗?
前端·javascript·css
yangcode15 小时前
iOS 苹果内购 Storekit 2
前端
LuckySusu15 小时前
【js篇】JavaScript 原型修改 vs 重写:深入理解 constructor的指向问题
前端·javascript