Java-中软-9 springboot——新闻的分页、新增、编辑和搜索

实现
从TNewsApplication类中的main方法启动

@SpringBootApplication
public class TNewsApplication {
    public static void main(String[] args) {
        SpringApplication.run(TNewsApplication.class,args);
    }
}

实体类News

@Entity
@Table(name = "t_News")
public class News {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    @Basic(fetch = FetchType.LAZY)
    @Lob
    private String content;
    private String firstPicture;
    private String flag;
    private Integer 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;
    @ManyToMany(cascade = {CascadeType.PERSIST})
    private List<Tag> tags = new ArrayList<>();
    @ManyToOne
    private User user;
    @OneToMany(mappedBy = "news")
    private List<Comment> comments = 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 Integer getViews() {
        return views;
    }
    public void setViews(Integer 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 List<Tag> getTags() {
        return tags;
    }
    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public List<Comment> getComments() {
        return comments;
    }
    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }
    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());
    }
    //1,2,3
    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 +
                ", tags=" + tags +
                ", user=" + user +
                ", comments=" + comments +
                ", tagIds='" + tagIds + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
    public void initTags(Long id) {
        //3,4,5
        List<Tag> tags = this.getTags();
        StringBuffer ids=new StringBuffer();
        if(!tags.isEmpty()){
            Boolean flag=false;
            for(Tag t:tags){
                if(flag){
                    ids.append(t.getId());
                    flag=true;
                }else {
                    ids.append(",");
                    ids.append(t.getId());
                }

            }
            this.setTagIds(ids.toString());
        }

    }
}

实体类NewsQuery
通过新闻的标题、类型和是否被推荐三个条件来搜索新闻

public class NewsQuery {
    private String title;
    private String typeId;
    private Boolean recommend;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTypeId() {
        return typeId;
    }
    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }
    public Boolean getRecommend() {
        return recommend;
    }
    public void setRecommend(Boolean recommend) {
        this.recommend = recommend;
    }
    @Override
    public String toString() {
        return "NewsQuery{" +
                "title='" + title + '\'' +
                ", typeId='" + typeId + '\'' +
                ", recommend=" + recommend +
                '}';
    }
}

dao层:在dao中,创建继承JpaRepository、JpaSpecificationExecutor类的NewsDao接口
Specification是JPA中用于动态构造查询条件的接口,可以将传入的查询条件动态构建,用于查询

public interface NewsDao extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {
}

service层
(1)在service中,创建NewsService接口

public interface NewsServcie {
    Page<News> findByPageable(Pageable pageable);
    void input(News news);
    News findNewsById(Long id);
    Page<News> searchNews(Pageable pageable, NewsQuery newsQuery);
}

(2)在service下的impl中,实现NewsService接口
input:如果news的id为空,则将news的createTime设置为当前时间,调用dao层的save方法完成新增;
如果news的id不为空,则将updateTime设置为当前时间,再根据news的id调用dao层的getOne方法得到n,然后调用BeanUtils的copyProperties方法给n的属性赋值,最后调用dao层的save方法完成编辑
searchNews:根据title、typeId、recommend进行查询并以此new一个Specification
再以new出来的Specification以及pageable作为参数调用dao层的findAll方法完成搜索

@Service
public class NewsServiceImpl implements NewsServcie {
    @Autowired
    private NewsDao newsDao;
    @Override
    public Page<News> findByPageable(Pageable pageable) {
        return newsDao.findAll(pageable);
    }
    @Override
    public void input(News news) {
        if (news.getId()==null){
            news.setCreateTime(new Date());
            newsDao.save(news);
        }else {
            news.setUpdateTime(new Date());
            News n = newsDao.getOne(news.getId());
            BeanUtils.copyProperties(news,n, MyBeanUtils.getNullPropertyNames(news));
            newsDao.save(n);
        }

    }
    @Override
    public News findNewsById(Long id) {
        return newsDao.getOne(id);
    }
    @Override
    public Page<News> searchNews(Pageable pageable, NewsQuery newsQuery) {
        Page<News> news=newsDao.findAll(new Specification<News>() {
            @Override
            public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(!StringUtils.isEmpty(newsQuery.getTitle())){
                    predicates.add(criteriaBuilder.like(root.<String>get("title"),"%"+newsQuery.getTitle()+"%"));
                }
                if(!StringUtils.isEmpty(newsQuery.getTypeId())){
                    predicates.add(criteriaBuilder.equal(root.<Type>get("type").get("id"),newsQuery.getTypeId()));
                }
                if(!StringUtils.isEmpty(newsQuery.getRecommend())){
                    predicates.add(criteriaBuilder.equal(root.<Boolean>get("recommend"),newsQuery.getRecommend()));
                }
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        },pageable);
        return news;
    }
}

controller层
在Controller中创建NewsController类,在类中写list、toinput、input、search方法,控制页面的跳转
list:新闻列表
ssm项目中,需要自定义PageInfo类实现分页,现在可以使用内置的Pageable来实现分页,简化了编程工作
调用typeService的listType方法得到type列表,将model的types属性设置为得到的type列表
toInput:新增/编辑
如果输入的id为1,则进人新增界面;
如果输入的id不为1,则进入编辑界面:根据id查找出新闻news;再调用news的getTags方法,得到news的tag列表,然后调用tagservice的getTagIds方法,将tag列表转换为tagIds字符串;最后将tagIds字符串赋值给news;
分别调用typeService的listType方法、tagService的listTag方法,得到type、tag列表,将model的types属性设置为得到的type列表、tags属性设置为得到的tag列表
input:完成新增、编辑
从session中取出用户user,将news的User属性设置为user
调用news的getTagIds方法,得到news的tagIds字符串,然后调用tagservice的findTagByTagId方法,将tagIds字符串转换为tags列表;最后将tags列表赋值给news
调用service层的input方法,完成新增、编辑
search:搜索
page对象用于分页,newQuery对象用于查询,model对象用于将结果传递到前端

@Controller
@RequestMapping("/admin/news")
public class NewsController {
    @Autowired
    private NewsServcie newsServcie;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @RequestMapping
    public String list(@PageableDefault(size = 5,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,Model model){
        Page<News> page=newsServcie.findByPageable(pageable);
        model.addAttribute("page",page);
        model.addAttribute("types",typeService.listType());
        return "admin/news";
    }
    @RequestMapping("input/{id}")
    public String toInput(@PathVariable Long id,Model model){
        if(id==-1){
            model.addAttribute("news",new News());
        }else {
            News news=newsServcie.findNewsById(id);
            String tagIds=tagService.getTagIds(news.getTags());
            news.setTagIds(tagIds);
            model.addAttribute("news",news);
        }
        List<Type> types=typeService.listType();
        model.addAttribute("types",types);
        model.addAttribute("tags",tagService.listTag());
        return "admin/news-input";
    }
    @RequestMapping("input")
    public String input(News news, HttpSession session){
        User user= (User) session.getAttribute("user");
        news.setUser(user);
        List<Tag> tags=tagService.findTagByTagId(news.getTagIds());
        news.setTags(tags);
        newsServcie.input(news);
        return "redirect:/admin/news";
    }
    @RequestMapping("search")
    public String search(@PageableDefault(size = 5,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,NewsQuery newsQuery,Model model){
      Page<News> page=newsServcie.searchNews(pageable,newsQuery);
      model.addAttribute("page",page);
        return "admin/news :: newsList";
    }
}

界面
news.html
遍历page对象,显示页面信息:
Thymeleaf中th:each属性用于迭代循环,语法:th:each=“obj,iterStat:${objList}”
迭代对象可以是java.util.List,java.util.Map,数组等

<tr th:each="news,iterStat : ${page.content}">
            <td th:text="${iterStat.count}">1</td>
            <td th:text="${news.title}">刻意练习清单</td>
            <td th:text="${news.type.name}">认知升级</td>
            <td th:text="${news.recommend} ? '是':'否'"></td>
            <td th:text="${news.published} ? '发布':'草稿'">草稿</td>
            <td th:text="${news.updateTime}">2017-10-02 09:45</td>
          </tr>

上下页:
若总页数大于1,显示上一页、下一页
若当前页是首页,不显示上一页
若当前页是尾页,不显示下一页

              <div class="ui mini pagination menu" th:if="${page.totalPages}>1" >
                <a onclick="page(this)" th:attr="data-page=${page.number}-1" class="item" th:unless="${page.first}">上一页</a>
                <a onclick="page(this)" th:attr="data-page=${page.number}+1" class=" item" th:unless="${page.last}">下一页</a>
              </div>

进入新增界面:

              <a href="#" th:href="@{/admin/news/input/{id}(id=-1)}" class="ui mini right floated teal basic button">新增</a>

进入编辑界面:

              <a href="#" th:href="@{/admin/news/input/{id}(id=${news.id})}" class="ui mini teal basic button">编辑</a>

搜索:
在界面上通过输入的条件,生成NewsQuery对象传给后台

    $("#search-btn").click(function () {
      $("[name='page']").val(0);
      load();
    });
    function load() {
      $("#table-container").load("/admin/news/search",{
        title : $("[name='title']").val(),
        typeId : $("[name='typeId']").val(),
        recommend : $("[name='recommend']").prop('checked'),
        page : $("[name='page']").val()}
      )
    }

news-input.html
完成新增、编辑

      <form id="news-form" action="#" th:object="${news}" th:action="@{/admin/news/input}" method="post" class="ui form">
      ...
      </form>

运行结果
分页
在这里插入图片描述
在这里插入图片描述
新增
在这里插入图片描述
编辑
在这里插入图片描述
在这里插入图片描述
搜索
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43296415/article/details/107694666