springboot实现博客标签和分类标签的功能

这是一个合集!(7.10-7.13)


还是使用上个项目进行完善

然后就上代码


先写博客标签的功能

delete
在BlogController里面先写方法

@GetMapping("{id}/delete")
public String delete(@PathVariable Long id){
    blogService.deleteById(id);
    return "redirect:/admin/blogs";
}

然后在IBlogService上写出这个方法,并在BlogServiceImpl里面实现

IBlogService

void deleteById(Long id);

BlogServiceImpl

@Override
public void deleteById(Long id) {
    blogDao.deleteById(id);
}

现在写新增页面(写博客
啊这里要导入一个新的html(bolg-input.html

然后要实现填写的信息(分类,标签,首图)的东西点击提交按钮后显示在页面上

先在BlogController写新方法

@GetMapping("input")
public String input(Model model){
    model.addAttribute("blog",new Blog());				//标题
    model.addAttribute("types",typeService.listType());	//分类
    model.addAttribute("tags",tagService.listType());	//标签
    //后面两个是要实现查找后然后选一个出来的那种效果(啊很口语化了
    return "admin/blogs-input";
}

转到ITypeService

List<Type> listType();

然后实现类TypeServiceImpl

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

add
同样,从BlogController开始写

@RequestMapping("add")
public String add(Blog blog, HttpSession session){
    User user = (User) session.getAttribute("user");
    blog.setUser(user);
    String tagIds = blog.getTagIds();
    List<Tag> tagList=tagService.getTagByIds(tagIds);
    blog.setTags(tagList);
    if(blog.getId()==null){
        blogService.add(blog);
    }else {
        blogService.update(blog);
    }

IBlogService

 void add(Blog blog);

BlogServiceImpl

@Override
public void add(Blog blog) {
    blog.setCreateTime(new Date());
    blog.setUpdateTime(new Date());
    blogDao.save(blog);
}

update
BlogController

 @RequestMapping("{id}/toUpdate")
 public String toUpdate(@PathVariable Long id,Model model){
     Blog blog=blogService.getBlog(id);
     blog.initTags(id);
     model.addAttribute("blog",blog);
     model.addAttribute("types",typeService.listType());
     model.addAttribute("tags",tagService.listType());
     return  "admin/blogs-input";
 }

IBlogService

void update(Blog blog);

BlogServiceImpl

@Override
public void update(Blog blog) {
    Blog one = blogDao.getOne(blog.getId());
    BeanUtils.copyProperties(one,blog);
    one.setUpdateTime(new Date());
    blogDao.save(one);
}

转到ITagService

List<Tag> getTagByIds(String tagIds);

实现类TagServiceImpl

@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]));
            }
        }
    }

转到Blog

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());
    }
}


然后开始写分类标签

在BlogController里面完善add方法

@RequestMapping("add")
public String add(Blog blog, HttpSession session){
    User user = (User) session.getAttribute("user");
    blog.setUser(user);
    String tagIds = blog.getTagIds();
    List<Tag> tagList=tagService.getTagByIds(tagIds);
    blog.setTags(tagList);
    if(blog.getId()==null){
        blogService.add(blog);
    }else {
        blogService.update(blog);
    }
    return "redirect:/admin/blogs";
}

添加search方法
方法大致相同,就不赘述了

search
BlogController

@RequestMapping("search")
public String search(@PageableDefault(size = 5,sort = {"updataTime"},direction = Sort.Direction.DESC) Pageable pageable,
                     BlogQuery BlogQuery,Model model){
    Page<Blog> page=blogService.listBlog(Pageable,BlogQuery);
    model.addAttribute("page",page);
    return "admin/blog::blogList";
}

IBlogService

Blog getBlog(Long id);

BlogServiceImpl

@Override
    public Blog getBlog(Long id) {
        return blogDao.getOne(id);
}

还要写一个ListBlog在IBlogService里=

Page<Blog> listBlog(Pageable pageable, BlogQuery blogQuery);

当然还有实现类

 @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);
 }

新建IndexController

@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("page",page);
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        model.addAttribute("recommendBlogs",blogs);
        return "index";
    }
}

转到ITypeService

List<Tag> listTypeTop(int i);

TypeServiceImpl

@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;
}

更新TypeDao

 @Query("select t from Type t")
 List<Type> findTop(Pageable pageable);

更新BlogDao

@Query("select b from Blog b where b.recommend
List<Blog> findTop(Pageable pageable);

然就就完成啦!!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值