Springboot新增文章

Article

java 复制代码
package com.lin.springboot01.pojo;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDateTime;

@Data
public class Article {
    private Integer id;
    @NotEmpty
    @Pattern(regexp = "^\\S{1,16}$")
    private String title;
    @NotEmpty
    private String content;
    @NotEmpty
    @URL
    private String coverImg;
    private String state;
    @NotNull
    private Integer categoryId;
    private Integer createUser;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

ArticleController

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

import com.lin.springboot01.pojo.Article;
import com.lin.springboot01.pojo.Result;
import com.lin.springboot01.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }
}

ArticleService

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

import com.lin.springboot01.pojo.Article;

public interface ArticleService {
    //新增文章
    void add(Article article);
}

ArticleServiceImpl

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


import com.lin.springboot01.mapper.ArticleMapper;
import com.lin.springboot01.pojo.Article;
import com.lin.springboot01.service.ArticleService;
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 ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void add(Article article) {
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
    }
}

ArticleMapper

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

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

@Mapper
public interface ArticleMapper {
    //新增文章
    @Insert("insert into article(title,content,cover_img,state,category_id,create_user,create_time,update_time) values (#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime})")
    void add(Article article);
}

完成:

相关推荐
羊锦磊5 分钟前
[ Redis ] SpringBoot集成使用Redis(补充)
java·数据库·spring boot·redis·spring·缓存·json
兮动人1 小时前
Maven 多配置文件的使用
java·maven·maven 多配置文件的使用
毕设源码-钟学长1 小时前
【开题答辩全过程】以 餐健一体化管理系统为例,包含答辩的问题和答案
java·eclipse
摇滚侠1 小时前
Spring Boot3零基础教程,整合 SSM,笔记52
java·spring boot·笔记
毕设源码-朱学姐2 小时前
【开题答辩全过程】以 查寝打卡系统为例,包含答辩的问题和答案
java·eclipse
QMY5205203 小时前
爬虫的意义
java·spring·tomcat·maven
lang201509283 小时前
Spring Boot Actuator深度解析与实战
java·spring boot·后端
重生之我是Java开发战士3 小时前
【Java EE】了解Spring Web MVC:请求与响应的全过程
spring boot·spring·java-ee·1024程序员节
懒羊羊不懒@4 小时前
Java—枚举类
java·开发语言·1024程序员节
zerolala4 小时前
Java容器常用方法
java·1024程序员节