Spring boot开源项目之个人博客(13)—博客管理

本文介绍了如何在Spring Boot项目中实现博客管理功能,包括分页展示、博客新增、编辑和删除。分页展示涉及动态查询和Ajax局部更新,博客新增和编辑通过前端表单与后端实体类映射实现,博客删除则由简单的服务层和控制器层方法处理。
摘要由CSDN通过智能技术生成

Spring boot开源项目之个人博客(13)—博客管理

1. 分页展示

这部分一共两个内容,一个带条件查询动态分页展示,一个是ajax页面局部动态更新。

  • 带条件查询动态分页展示

博客管理比分类要复杂一些,需要根据高级查询条件筛选文章进行分页展示,在流程上是差不多的。

首先是把查询条件封装成一个对象BlogQuery

@Data
public class BlogQuery {
   

    private String title;
    private Long typeId;
    private boolean recommend;
}

查询标题、分类、是否推荐3项。

然后,写一下dao层接口、service层接口和实现,这部分和分类是差不多的。主要的区别就在于查找博客列表的方法。先使dao层接口继承JpaSpecificationExecutor

public interface BlogRepository extends JpaRepository<Blog, Long>, JpaSpecificationExecutor<Blog> {
   
}

然后定义好service层接口

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

接口的实现方法

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

Root<Blog> root表示使用高级查询的对象,criteriaQuery、criteriaBuilder可以帮助我们很方便的完成条件的拼接。

最后在controller层定义回调方法

@GetMapping("/blogs")
public String blogs(@PageableDefault(size = 3, sort = {
   "updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                    Model model, BlogQuery blog){
   
    model.addAttribute("types", typeService.listType());
    model.addAttribute("page", blogService.listBlog(pageable, blog));
    return "admin/blogs";
}

这里 model.addAttribute("types", typeService.listType());是因为前段页面分类的条件筛选是通过一个dropdown实现的,所以需要把数据库中的Type查出来送到前端

<div class="ui selection dropdown">
    <input type="hidden" name="typeId">
    <i class="dropdown icon"></i>
    <div class="default text">分类</div>
    <div class="menu">
        <div class="item" th:each="type : ${types}" th:data-value="${type.id}" th:text="${type.name}" data-value="1">错误日志</div>
    </div>
</div>

通过 th:each循环把所有的分类都加入到dropdown里,设置th:data-value为分类的id,显示文字为分类的名称。

查到的数据都送到前端之后,在前端把数据都展示出来

<div id="table-container">
    <table th:fragment="blogList" class="ui celled teal table">
        <thead>
            <tr>
                <th></th>
                <th>标题</th>
                <th>类型</th>
                <th>推荐</th>
                <th>更新时间</th>
                <th>操作</th>
            </tr>
        </thead
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值