• Springboot新增文章


     

    Article

    1. package com.lin.springboot01.pojo;
    2. import jakarta.validation.constraints.NotEmpty;
    3. import jakarta.validation.constraints.NotNull;
    4. import jakarta.validation.constraints.Pattern;
    5. import lombok.Data;
    6. import org.hibernate.validator.constraints.URL;
    7. import java.time.LocalDateTime;
    8. @Data
    9. public class Article {
    10. private Integer id;
    11. @NotEmpty
    12. @Pattern(regexp = "^\\S{1,16}$")
    13. private String title;
    14. @NotEmpty
    15. private String content;
    16. @NotEmpty
    17. @URL
    18. private String coverImg;
    19. private String state;
    20. @NotNull
    21. private Integer categoryId;
    22. private Integer createUser;
    23. private LocalDateTime createTime;
    24. private LocalDateTime updateTime;
    25. }

    ArticleController

    1. package com.lin.springboot01.controller;
    2. import com.lin.springboot01.pojo.Article;
    3. import com.lin.springboot01.pojo.Result;
    4. import com.lin.springboot01.service.ArticleService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.*;
    7. @RestController
    8. @RequestMapping("/article")
    9. public class ArticleController {
    10. @Autowired
    11. private ArticleService articleService;
    12. @PostMapping
    13. public Result add(@RequestBody @Validated Article article){
    14. articleService.add(article);
    15. return Result.success();
    16. }
    17. }

    ArticleService

    1. package com.lin.springboot01.service;
    2. import com.lin.springboot01.pojo.Article;
    3. public interface ArticleService {
    4. //新增文章
    5. void add(Article article);
    6. }

    ArticleServiceImpl

    1. package com.lin.springboot01.service.impl;
    2. import com.lin.springboot01.mapper.ArticleMapper;
    3. import com.lin.springboot01.pojo.Article;
    4. import com.lin.springboot01.service.ArticleService;
    5. import com.lin.springboot01.utils.ThreadLocalUtil;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. import java.time.LocalDateTime;
    9. import java.util.Map;
    10. @Service
    11. public class ArticleServiceImpl implements ArticleService {
    12. @Autowired
    13. private ArticleMapper articleMapper;
    14. @Override
    15. public void add(Article article) {
    16. article.setCreateTime(LocalDateTime.now());
    17. article.setUpdateTime(LocalDateTime.now());
    18. Map map = ThreadLocalUtil.get();
    19. Integer userId = (Integer) map.get("id");
    20. article.setCreateUser(userId);
    21. articleMapper.add(article);
    22. }
    23. }

    ArticleMapper

    1. package com.lin.springboot01.mapper;
    2. import com.lin.springboot01.pojo.Article;
    3. import org.apache.ibatis.annotations.Insert;
    4. import org.apache.ibatis.annotations.Mapper;
    5. @Mapper
    6. public interface ArticleMapper {
    7. //新增文章
    8. @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})")
    9. void add(Article article);
    10. }

    完成:

  • 相关阅读:
    Nginx的http启动流程分析
    RunnerGo:轻量级、全栈式、易用性和高效性的测试工具
    满足新能源三电系统气密和电性能测试的E10系列多功能电连接器
    星空投影仪美国亚马逊审核标准UL62368检测项目介绍
    alist配合onlyoffice 实现在线预览
    【中阳期货】如何判断入场点
    2022-09-16 Android app 让图片在ScrollView里面等比例完整显示不变形,继承ImageView ,对ImageView 进行修改。
    netlink原理及应用
    Javaweb基础浅聊下载文件
    spring 注解: 更加简单的存储 Bean
  • 原文地址:https://blog.csdn.net/m0_71088505/article/details/134494623