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

完成:

相关推荐
yangminlei6 分钟前
基于 Java 的消息队列选型年度总结:RabbitMQ、RocketMQ、Kafka 实战对比
java·java-rocketmq·java-rabbitmq
张np6 分钟前
java基础-ListIterator 接口
java·开发语言
韩立学长8 分钟前
【开题答辩实录分享】以《足球球员数据分析系统开题报告》为例进行选题答辩实录分享
java·数据库·mysql
牧小七16 分钟前
Java CompletableFuture 使用详解
java
jiayong2319 分钟前
MINA框架面试题 - 实战篇
java·io·mina
数智工坊20 分钟前
【操作系统-处理器调度】
java·linux·服务器·windows·ubuntu
Coder_Boy_29 分钟前
基于SpringAI的在线考试系统-考试管理功能布局+交互优化方案
java·数据库·人工智能·spring boot·交互·ddd·tdd
Tao____36 分钟前
可以本地部署的物联网平台
java·开发语言·物联网·mqtt·低代码
码界奇点36 分钟前
基于DDD与CQRS的Java企业级应用框架设计与实现
java·开发语言·c++·毕业设计·源代码管理