0729_新闻管理条件查询分页展示+新增+编辑

1.分页展示(包含条件查询)
①News实体类

package com.zr0726.myspring.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "t_news")
public class News {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    @Basic(fetch = FetchType.LAZY)
    @Lob
    private String content;
    private String firstPicture;
    private String flag;
    private String views;
    private boolean appreciation;
    private boolean shareStatement;
    private boolean commentabled;
    private boolean published;
    private boolean recommend;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    @ManyToOne
    private Type type;

    @ManyToOne
    private User user;

    @ManyToMany(cascade = CascadeType.PERSIST) //级联
    private List<Tag> tags = new ArrayList<>();

    @Transient
    private String tagIds;

    private String description;

    public News(){
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFirstPicture() {
        return firstPicture;
    }

    public void setFirstPicture(String firstPicture) {
        this.firstPicture = firstPicture;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getViews() {
        return views;
    }

    public void setViews(String views) {
        this.views = views;
    }

    public boolean isAppreciation() {
        return appreciation;
    }

    public void setAppreciation(boolean appreciation) {
        this.appreciation = appreciation;
    }

    public boolean isShareStatement() {
        return shareStatement;
    }

    public void setShareStatement(boolean shareStatement) {
        this.shareStatement = shareStatement;
    }

    public boolean isCommentabled() {
        return commentabled;
    }

    public void setCommentabled(boolean commentabled) {
        this.commentabled = commentabled;
    }

    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    public String getTagIds() {
        return tagIds;
    }

    public void setTagIds(String tagIds) {
        this.tagIds = tagIds;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void init(){
        this.tagIds = tagsToIds(this.getTags());
    }

    private String tagsToIds(List<Tag> tags){
        if(!tags.isEmpty()){
            StringBuffer ids = new StringBuffer();
            boolean flag = false;
            for(Tag tag:tags){
                if(flag){
                    ids.append(",");
                }else{
                    flag=true;
                }
                ids.append(tag.getId());
            }
            return ids.toString();
        }else {
            return tagIds;
        }
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", firstPicture='" + firstPicture + '\'' +
                ", flag='" + flag + '\'' +
                ", views='" + views + '\'' +
                ", appreciation=" + appreciation +
                ", shareStatement=" + shareStatement +
                ", commentabled=" + commentabled +
                ", published=" + published +
                ", recommend=" + recommend +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", type=" + type +
                ", user=" + user +
                ", tags=" + tags +
                ", tagIds='" + tagIds + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    //    @Override
//    public String toString() {
//        return "News{" +
//                "id=" + id +
//                ", title='" + title + '\'' +
//                ", content='" + content + '\'' +
//                ", firstPicture='" + firstPicture + '\'' +
//                ", flag='" + flag + '\'' +
//                ", views='" + views + '\'' +
//                ", appreciation=" + appreciation +
//                ", shareStatement=" + shareStatement +
//                ", commentabled=" + commentabled +
//                ", published=" + published +
//                ", recommend=" + recommend +
//                ", createTime=" + createTime +
//                ", updateTime=" + updateTime +
//                ", type=" + type +
//                ", user=" + user +
//                '}';
//    }
}

②Tag实体类

public List<News> getNewsList() {
    return newsList;
}

public void setNewsList(List<News> newsList) {
    this.newsList = newsList;
}

③编写NewQuery类

package com.zr0726.myspring.vo;

public class NewQuery {

    private String title;

    private Long typeId;

    private boolean recommend;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getTypeId() {
        return typeId;
    }

    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }

    @Override
    public String toString() {
        return "NewQuery{" +
                "title='" + title + '\'' +
                ", typeId=" + typeId +
                ", recommend=" + recommend +
                '}';
    }
}

④编写NewService类

Page<News> listNew(Pageable pageable, NewQuery newQuery);

⑤编写NewServiceImpl类

//新闻管理中的列表(包含了查询)
@Override
public Page<News> listNew(Pageable pageable, NewQuery newQuery){
    return newRepository.findAll(new Specification<News>() {
        @Override
        public Predicate toPredicate(Root<News> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            if(!"".equals(newQuery.getTitle())&&newQuery.getTitle()!=null){
                predicates.add(cb.like(root.<String>get("title"),"%"+newQuery.getTitle()+"%"));
            }
            if(newQuery.getTypeId()!=null){
                predicates.add(cb.equal(root.get("type").get("id"),newQuery.getTypeId()));
            }
            if(newQuery.isRecommend()){
                predicates.add(cb.equal(root.get("recommend"),newQuery.isRecommend()));
            }
            cq.where(predicates.toArray(new Predicate[predicates.size()]));
            return null;
        }
    },pageable);
}

⑥编写NewRepository类

package com.zr0726.myspring.dao;

import com.zr0726.myspring.po.News;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

public interface NewRepository extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {

//    @Query("select n from News n where n.title like ?1 or n.content like ?1")
//    Page<News> findByQuery(String query, Pageable pageable);

}

⑦编写NewController类

@GetMapping("/news")
public String news(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                   Pageable pageable, NewQuery newQuery, Model model){

    model.addAttribute("types",typeService.listType());
    model.addAttribute("page",newService.listNew(pageable,newQuery));
    return LIST;
}
@PostMapping("/news/search")
public String search(@PageableDefault(size=3,sort={"updateTime"},direction = Sort.Direction.DESC)
                     Pageable pageable,NewQuery newQuery,Model model){

    model.addAttribute("page",newService.listNew(pageable,newQuery));
    return "admin/news :: newsList";
}

⑧界面展示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.新增新闻
①编写NewService类

News saveNew(News news);

②编写NewServiceImpl类

@Override
public News saveNew(News news) {
    if(news.getId()==null){
        news.setCreateTime(new Date());
        news.setUpdateTime(new Date());
    }
    return newRepository.save(news);
}

③编写NewController类

public void setTypeAndTag(Model model){
    model.addAttribute("types",typeService.listType());
    model.addAttribute("tags",tagService.listTag());
}

@GetMapping("/news/input")
public String input(Model model){
    setTypeAndTag(model);
    model.addAttribute("news",new News());
    return INPUT;
}

④界面展示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.编辑新闻
①编写NewService类

News getNew(Long id);

News updateNew(Long id,News news);

②编写NewServiceImpl类

@Override
public News getNew(Long id) {
    return newRepository.findById(id).orElse(null);
}

@Override
public News updateNew(Long id, News news) {
    News news1 = newRepository.findById(id).orElse(null);
    if(news1==null){
        System.out.println("未获得更新对象");
    }
    BeanUtils.copyProperties(news,news1);
    news1.setUpdateTime(new Date());
    return newRepository.save(news1);
}

③编写NewController类

@GetMapping("/news/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
    setTypeAndTag(model);
    News news = newService.getNew(id);
    news.init();
    model.addAttribute("news",news);
    return INPUT;
}

@PostMapping("/news/add")
public String post(News news, RedirectAttributes attributes, HttpSession session){
    System.out.println("controller接收的news为:"+news);
    news.setUser((User)session.getAttribute("user"));
    news.setType(typeService.getType(news.getType().getId()));
    news.setTags(tagService.listTag(news.getTagIds()));
    News news1;
    if(news.getId()==null){
        news1 = newService.saveNew(news);
    }else{
        news1 = newService.updateNew(news.getId(),news);
    }
    if(news1==null){
        attributes.addFlashAttribute("message","操作失败");
    }else{
        attributes.addFlashAttribute("message","操作成功");
    }
    return REDIRECT_LIST;

}

④界面展示
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值