新增文章分类

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

完成:

相关推荐
DoraBigHead3 分钟前
React 中的代数效应:从概念到 Fiber 架构的落地
前端·javascript·react.js
LuckySusu3 分钟前
【vue篇】Vue 性能优化全景图:从编码到部署的优化策略
前端·vue.js
卓伊凡8 分钟前
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓
前端
笨笨鸟慢慢飞10 分钟前
Vue3后退不刷新,前进刷新
前端
流星白龙12 分钟前
【Qt】3.认识 Qt Creator 界面
java·开发语言·qt
LuckySusu12 分钟前
【vue篇】SSR 深度解析:服务端渲染的“利”与“弊”
前端·vue.js
LuckySusu13 分钟前
【vue篇】SPA 单页面应用:现代 Web 的革命与挑战
前端·vue.js
LuckySusu14 分钟前
【vue篇】Vue 初始化页面闪动(FOUC)问题终极解决方案
前端·vue.js
fruge16 分钟前
从 0 到 1 理解前端工程化:图表化解析核心逻辑
前端
LuckySusu16 分钟前
【vue篇】技术分析:Template 与 JSX 的本质区别与选型指南
前端·vue.js