新增文章分类

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

完成:

相关推荐
IT_陈寒10 小时前
Vue这个坑我跳了两次,原来问题出在这
前端·人工智能·后端
kyriewen10 小时前
我用 50 行代码重写了 React Router 核心,终于搞懂了前端路由原理
前端·javascript·react.js
人活一口气11 小时前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc
WebInfra11 小时前
Rspack 2.1 发布:React Compiler 提速 10 倍!
前端
李明卫杭州11 小时前
CSS 媒体查询详解:一文掌握响应式设计的核心技术
前端
lichenyang45312 小时前
从 H5 按钮到 OpenHarmony 能力调用:我如何理解 ASCF 的运行链路
前端
下家12 小时前
我放弃了 Vue/React,选择自研框架
前端·前端框架
Asize13 小时前
HTML5 Canvas 基础:从按帧动画到 ECharts 数据可视化
前端·javascript·canvas
像我这样帅的人丶你还13 小时前
Java 后端详解(三):全局异常处理与 JPA 数据库映射
java·后端
默_笙13 小时前
🎄 后端给我一堆扁平数据,我 10 行代码把它变成了树
前端·javascript