713

BlogDao

public interface BlogDao extends JpaRepository<Blog,Long>, JpaSpecificationExecutor<Blog> {
    @Query("select b from Blog b where b.recommend=true")
    List<Blog> findTop(Pageable pageable);
}

TagDao

public interface TagDao extends JpaRepository<Tag,Long> {
    @Query("select t from Tag t")
    List<Tag> findTop(Pageable pageable);
}

BlogQurey

package com.zr.po;

public class BlogQuery {
    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 getRecommend() {
        return recommend;
    }

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

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

BlogServiceImpl

@Override
    public Page<Blog> listBlog(Pageable pageable, BlogQuery blogQuery) {
        Specification<Blog> specification=new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(!StringUtils.isEmpty(blogQuery.getTitle())){
                    predicates.add(criteriaBuilder.like(root.<String>get("title"),"%"+blogQuery.getTitle()+"%"));
                }
                if(blogQuery.getTypeId()!=null){
                    predicates.add(criteriaBuilder.equal(root.<Type>get("type").get("id"),blogQuery.getTypeId()));
                }
                if(blogQuery.getRecommend()){
                    predicates.add(criteriaBuilder.equal(root.<Boolean>get("recommend"),blogQuery.getRecommend()));
                }
                criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        };
        return blogDao.findAll(specification, pageable);
    }

    @Override
    public List<Blog> listRecommendBlogTop(int i) {
        Sort sort= Sort.by(Sort.Direction.DESC,"updateTime");
        Pageable pageable= PageRequest.of(0,i,sort);
        List<Blog> blogs=blogDao.findTop(pageable);
        return blogs;

    }

TypeServiceImpl


    @Override
    public List<Type> listType() {
        return typeDao.findAll();
    }

    @Override
    public List<Type> listTypeTop(int i) {
        Sort sort= Sort.by(Sort.Direction.DESC,"blogs.size");
        Pageable pageable=PageRequest.of(0,i,sort);
        List<Type> types=typeDao.findTop(pageable);
        return types;
    }

TagServiceImpl

 @Override
    public List<Tag> listType() {
        return tagDao.findAll();
    }

    @Override
    public List<Tag> getTagByIds(String tagIds) {
        //3,4,5
        List<Long> ids=new ArrayList<>();
        if(tagIds !=null && tagIds != ""){
            String[] s = tagIds.split(",");
            for(int i=0;i<s.length;i++){
                if(!StringUtils.isEmpty(s[i])){
                    ids.add(new Long(s[i]));
                }

            }
        }
        List<Tag> tags = tagDao.findAllById(ids);
        return tags;
    }

    @Override
    public List<Tag> listTypeTop(int i){
        Sort sort= Sort.by(Sort.Direction.DESC,"blogs.size");
        Pageable pageable= PageRequest.of(0,i,sort);
        List<Tag> tags=tagDao.findTop(pageable);
        return tags;
    }

Indexcontroller

package com.zr.web;

import com.zr.po.Blog;
import com.zr.po.Tag;
import com.zr.po.Type;
import com.zr.service.IBlogService;
import com.zr.service.ITagService;
import com.zr.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class  IndexController {

    @Autowired
    private IBlogService blogService;

    @Autowired
    private ITypeService typeService;

    @Autowired
    private ITagService tagService;

    @RequestMapping("/")
    public String index(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
                        Model model){
        Page<Blog> page = blogService.listBlog(pageable);
        List<Type> types=typeService.listTypeTop(6);
        List<Tag> tags=tagService.listTypeTop(6);
        List<Blog> blogs=blogService.listRecommendBlogTop(3);
        model.addAttribute("tags",tags);
        model.addAttribute("page",page);
        model.addAttribute("types",types);
        model.addAttribute("recommendBlogs",blogs);
        return "index";
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值