SpringBoot 整合JPA | PageHelper 的分页最简实现

简介: JPA又自己的Pageable来帮助我们实现分页,Mybatis有PageHelper帮我们实现分页,下面直接贴代码。1. 用JPA实现分页1.1 pom添加依赖 org.springframework.boot spring-boot-starter-data-jpa 其实就是JPA的依赖。

JPA又自己的Pageable来帮助我们实现分页,Mybatis有PageHelper帮我们实现分页,下面直接贴代码。

1. 用JPA实现分页

1.1 pom添加依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

其实就是JPA的依赖。

1.2 核心实现
    /**
     * 查询全部
     */
    @Override
    public ServerResponse selectAll(Integer page,Integer size) {
        Pageable pageable = new PageRequest(page,size,Sort.Direction.DESC,"noticeId");
        Iterator<Notice> all = noticeRepostory.findAll(pageable).iterator();
        List<Notice> list = new ArrayList<Notice>();
        while (all.hasNext()){
           list.add(all.next());
        }
        if (all == null){
            return ServerResponse.createByErrorMessage("查询公告列表失败");
        }
        return ServerResponse.createBySuccess(list);
    }

更多详细的请看spring boot2 整合(二)JPA(特别完整!)中分页那部分。

2. 用Mybatis实现分页

2.1 pom依赖
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

2.2 核心代码
    /**
     * 查询消息列表
     *
     * @param userId
     */
    @Override
    public ServerResponse selectNewsList(Integer userId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<News> news = newsMapper.selectNewsList(userId);
        PageInfo<News> appsPageInfo = new PageInfo<>(news);

        if (StringUtils.isEmpty(news)){
            return ServerResponse.createByErrorMessage("查询消息列表失败");
        }
        log.info("查询到的消息数目{}",appsPageInfo.getList());
        appsPageInfo.getList().forEach(p-> System.out.println(p.toString()));
        return ServerResponse.createBySuccess(appsPageInfo.getList());
    }

我们只需要PageHelper.startPage,然后紧跟着查询并返回一个list对象,然后用list对象创建一个PageInfo对象。

pageInfo有很多属性,比如当前页是多少,有没有下一页,数据一共多少等等,我这里调用它的getList方法来获取到news集合。

更多详细的请看spring boot2 整合(一)Mybatis (特别完整!)
中分页那部分。

相关文章
|
27天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
41 0
|
3月前
|
Java Spring
Springboot+jpa如何设置启动项目表不存在就主动创建,字段没有就新增
Springboot+jpa如何设置启动项目表不存在就主动创建,字段没有就新增
71 0
|
2月前
|
Java
Springboot+JPA+Sqlite整合demo
Springboot+JPA+Sqlite整合demo
|
15天前
|
SQL Java 数据库连接
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
|
4月前
|
XML Java 数据库连接
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
47 0
|
6天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
8天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
【Spring系列】Sping VS Sping Boot区别与联系
|
4月前
|
Java Spring
Spring Boot利用Spring Data JPA实现排序与分页查询实战(附源码,超详细)
Spring Boot利用Spring Data JPA实现排序与分页查询实战(附源码,超详细)
77 0
|
2月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
QGS
|
3月前
|
前端开发 Java 关系型数据库
手拉手全栈springboot+vue2+Element实现分页
手拉手全栈springboot+vue2+Element实现分页
QGS
27 0