4.15
//controller
package com.di.bigevent.controller;
import com.di.bigevent.pojo.Article;
import com.di.bigevent.pojo.Result;
import com.di.bigevent.service.ArticleService;
import com.di.bigevent.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/article")
public class ArticleController {
@Autowired
private ArticleService articleService;
@PostMapping
public Result add(@RequestBody Article article){
articleService.add(article);
return Result.success();
}
}
//service
package com.di.bigevent.service;
import com.di.bigevent.pojo.Article;
public interface ArticleService {
//新增文章
void add(Article article);
}
//serviceImpl
package com.di.bigevent.service.impl;
import com.di.bigevent.mapper.ArticleMapper;
import com.di.bigevent.pojo.Article;
import com.di.bigevent.service.ArticleService;
import com.di.bigevent.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);
}
}
//mapper
package com.di.bigevent.mapper;
import com.di.bigevent.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);
}