新增文章分类

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

完成:

相关推荐
城南云小白12 分钟前
Linux网络服务只iptables防火墙工具
linux·服务器·网络
从心归零13 分钟前
sshj使用代理连接服务器
java·服务器·sshj
咩咩大主教14 分钟前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
羌俊恩19 分钟前
视频服务器:GB28181网络视频协议
服务器·网络·音视频
运维小白。。43 分钟前
Nginx 反向代理
运维·服务器·nginx·http
科技互联人生1 小时前
中国数据中心服务器CPU行业发展概述
服务器·硬件架构
IT毕设梦工厂1 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Jiaberrr1 小时前
前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)
前端·javascript·vue.js·微信小程序·uni-app
everyStudy2 小时前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
城南云小白2 小时前
web基础+http协议+httpd详细配置
前端·网络协议·http