springboot+vue项目实战2024第四集修改文章信息

07-14 1091阅读

1.添加文章信息

    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }
        void add(Article article);
@Override
public void add(Article article) {
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map map = ThreadLocalUtil.get();
        Integer id =(Integer) map.get("id");
        article.setUserId(id);
        articleMapper.add(article);
        }
@Insert("insert into article(title,content,cover_img,state,cate_id," +
        "create_time,update_time,user_id)" +
        " values (#{title},#{content},#{coverImg},#{state},#{cateId}," +
        "#{createTime},#{updateTime},#{userId})")
    void add(Article article);

2.查询文章信息

 @GetMapping
    public Result list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer cateId,
            @RequestParam(required = false) String state
    ){
        PageBean pb=articleService.list(pageNum,pageSize,cateId,state);
        return Result.success(pb);
    }
         PageBean list(Integer pageNum, Integer pageSize, Integer cateId, String state);
 @Override
 public PageBean list(Integer pageNum, Integer pageSize, Integer cateId, String state) {
         PageBean pageBean = new PageBean();
         PageHelper.startPage(pageNum,pageSize);
         Map map = ThreadLocalUtil.get();
         Integer id = (Integer) map.get("id");
         List as=articleMapper.list(id,cateId,state);
         Page p=(Page) as;
         pageBean.setTotal(p.getTotal());
         pageBean.setItems(p.getResult());
         return pageBean;
         }
         List list(Integer userId, Integer cateId, String state);
 @Data
 public class Article {
     private Integer id;
     @NotEmpty
     @Pattern(regexp = "^\\S{1,10}$")
     private String title;
     @NotEmpty
     private String content;
     @NotEmpty
     @URL
     private String coverImg;
     @State
     private String state;
     private Integer userId;
     @NotNull
     private Integer cateId;
     private LocalDateTime createTime;
     private LocalDateTime updateTime;
 }
 @Data
 @NoArgsConstructor
 @AllArgsConstructor
 public class PageBean {
     private Long total;
     private List items;
 }
 @Documented
 @Constraint(
         validatedBy = {StateValidation.class}
 )
 @Target({ ElementType.FIELD})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface State {
     String message() default "state参数的值只能是已经发布或者草稿";
     Class[] groups() default {};
     Class

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]