SpringDataJPA如何实现某分类下的全部信息【分页】

本文介绍如何在SpringBoot项目中使用Spring Data JPA进行数据操作,并结合Thymeleaf实现分类及其博客的分页展示。通过TypeController实现了分类的获取与展示,以及根据分类ID查询相关博客的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

精选30+云产品,助力企业轻松上云!>>> hot3.png

1.在springboot中需要引入jpa的starter依赖,数据渲染采用themeleaf。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.TypeController.java

package com.wang.springboot.controller;


import com.wang.springboot.pojo.Type;
import com.wang.springboot.service.BlogService;
import com.wang.springboot.service.TagService;
import com.wang.springboot.service.TypeService;
import com.wang.springboot.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 王一宁 on 2017/10/23.
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    /**/
    @GetMapping("/types")
    public String types(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        HttpServletRequest request,
                        Model model) {
        // 1.点击分类,拿到的分类id
        String id = request.getParameter("id");
        long id2 = Long.parseLong(id);
        
		// 2.把分类id放入到session
        request.getSession().setAttribute("id", id);

        // 3.拿到所有的分类
        List<Type> types = typeService.listTypeTop(10000);
        /*if (id == -1) {
            id = types.get(0).getId();
        }*/
        // 4.封装查询对象
		// 或者自己实现根据id查询所有就好。
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id2);
		
		//5.放入前台页面的实体
        model.addAttribute("types", types);
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));

        return "list";
    }

    /*分页发送请求的地址*/
    @GetMapping("/pages")
    public String pages(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        HttpServletRequest request,
                        Model model) {

        // 1.拿到所有的分类
        List<Type> types = typeService.listTypeTop(10000);

		// 2.取出session存放的分类id
        String id2 = (String) request.getSession().getAttribute("id");
        Long id = Long.parseLong(id2);

        // 3.查询对象,BlogQuery封装分类的ID,根据id查询
		// 或者自己实现根据id查询所有就好。
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);

        model.addAttribute("types", types);
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));

        return "list";
    }

}

3.前台页面

<li class="menu"><a href="">分类</a>
                    <ul class="sub">
                        <li th:each="type : ${types}"><a th:href="@{/types/{id}(id=${type.id})}" th:text="${type.name}">分类名称</a>
                        </li>
                    </ul>
                    <span></span>
                </li>
/**
*  省略遍历内容的列表
*/

<div class="pagelist" th:if="${page.totalPages}>1">

                <a th:href="@{/pages?page=0}" th:classappend="${page.number==0} ? 'curPage'" >首页</a>
                <a th:href="@{/pages?page=1}" th:classappend="${page.number==1} ? 'curPage'">2</a>
                <a th:href="@{/pages?page=1}" th:classappend="${page.number==2} ? 'curPage'">3</a>
                <a th:href="@{/pages?page=2}" th:classappend="${page.number==3} ? 'curPage'">4</a>
                <a th:href="@{/pages?page=3}" th:classappend="${page.number==4} ? 'curPage'">5</a>
                <a>...</a>
                <a th:href="@{/pages(page=${page.totalPages}-1)}"  th:classappend="${page.number==page.totalPages-1} ? 'curPage'" >末页</a>
                <a th:href="@{/pages(page=${page.number}-1)}" th:unless="${page.first}">上一页</a>&nbsp;
                <a th:href="@{/pages(page=${page.number}+1)}" th:unless="${page.last}">下一页</a>

            </div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding路人王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值