SpringBoot学习-(七)SpringBoot分页插件PageHelper

访问数据库采用mybatis框架

1.添加pom文件依赖

<!-- spring mvc支持 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- springboot整合mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

<!-- springboot分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <!-- 特别注意版本问题, 看到评论以后得以纠正 -->
    <version>1.2.3</version>
</dependency>

<!-- 阿里巴巴druid数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.3</version>
</dependency>

<!-- mysql驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2.配置application.yml

# 与mybatis整合
mybatis:
  config-location: classpath:mybatis.xml
  mapper-locations:
  - classpath:mapper/*.xml

# 分页配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3.service层中使用插件

package com.ahut.serviceImpl;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.ContextLoader;

import com.ahut.entity.GoodsType;
import com.ahut.mapper.GoodsTypeMapper;
import com.ahut.service.GoodsTypeService;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @ClassName: GoodsTypeServiceImpl
 * @Description: 商品类型业务逻辑处理
 * @author cheng
 * @date 2017年7月17日 上午10:04:31
 */
@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class GoodsTypeServiceImpl implements GoodsTypeService {
    // 数据访问
    @Autowired
    private GoodsTypeMapper typeDao;

    /**
     * 
     * @Title: getList
     * @Description: 从数据库中获取所有商品类型列表
     * @param pageNum 当前页
     * @param pageSize 当前页面展示数目
     * @return
     * @throws Exception
     */
    public List<GoodsType> getList(int pageNum, int pageSize) throws Exception {
        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageNum, pageSize);
        // 获取
        List<GoodsType> typeList = typeDao.getList();
        return typeList;
    }

}

4.controller层代码

package com.ahut.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ahut.entity.GoodsType;
import com.ahut.service.GoodsTypeService;

/**
 * 
 * @ClassName: GoodsTypeAction
 * @Description: 商品类型控制层
 * @author cheng
 * @date 2017年7月17日 上午11:09:47
 */
@RestController // 等价于@Controller+@ResponseBody
public class GoodsTypeAction {
    // 业务逻辑
    @Autowired
    private GoodsTypeService typeService;

    /**
     * 
     * @Title: getGoodsTypeList
     * @Description: 获取商品类型列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getGoodsTypeList")
    public List<GoodsType> getGoodsTypeList(int pageNum, int pageSize) throws Exception {
        // 调用业务逻辑,返回数据
        return typeService.getList(pageNum,pageSize);
    }

}

5.测试

已知我数据库中有九条数据:

这里写图片描述

正常情况:

1.显示第一页或者第二页数据
请求url:

http://localhost:8080/getGoodsTypeList?pageNum=1&pageSize=4

返回数据:

[
    {
        "typeId": "708cc61c6a9811e796dee09467355fab",
        "typeName": "全部",
        "createTime": 1500258859000,
        "updateTime": 1500621762000
    },
    {
        "typeId": "98f8a04e6a9811e796dee09467355fab",
        "typeName": "考研资料",
        "createTime": 1500258927000,
        "updateTime": null
    },
    {
        "typeId": "b720c87f6a9811e796dee09467355fab",
        "typeName": "交通工具",
        "createTime": 1500258978000,
        "updateTime": null
    },
    {
        "typeId": "cbe3c2326a9811e796dee09467355fab",
        "typeName": "生活用品",
        "createTime": 1500259013000,
        "updateTime": 1500626046000
    }
]

2.显示最后一页
请求url:

http://localhost:8080/getGoodsTypeList?pageNum=3&pageSize=4

返回数据:

[
    {
        "typeId": "d992195f6df111e7bab4e09467355fab",
        "typeName": "测试2改变了",
        "createTime": 1501145516000,
        "updateTime": 1500716178000
    }
]

不正常情况:
1.显示的页数小于第一页(显示第一页数据)

pageNumber <= 0

请求url:

http://localhost:8080/getGoodsTypeList?pageNum=0&pageSize=4

返回数据:

[
    {
        "typeId": "708cc61c6a9811e796dee09467355fab",
        "typeName": "全部",
        "createTime": 1500258859000,
        "updateTime": 1500621762000
    },
    {
        "typeId": "98f8a04e6a9811e796dee09467355fab",
        "typeName": "考研资料",
        "createTime": 1500258927000,
        "updateTime": null
    },
    {
        "typeId": "b720c87f6a9811e796dee09467355fab",
        "typeName": "交通工具",
        "createTime": 1500258978000,
        "updateTime": null
    },
    {
        "typeId": "cbe3c2326a9811e796dee09467355fab",
        "typeName": "生活用品",
        "createTime": 1500259013000,
        "updateTime": 1500626046000
    }
]

结论:当请求页数小于第一页时,显示第一页数据

2.显示的页数大于最后一页(显示最后一页数据)
pageNum > 最后一页

请求url:

http://localhost:8080/getGoodsTypeList?pageNum=4&pageSize=4

返回数据:

[
    {
        "typeId": "d992195f6df111e7bab4e09467355fab",
        "typeName": "测试2改变了",
        "createTime": 1501145516000,
        "updateTime": 1500716178000
    }
]

结论:当请求页面大于最后一页时,显示最后一页数据

  • 18
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
Spring Boot是一个用于创建独立的、基于Spring的应程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一套强大的开发工具和约定,使开发人员能够更快地构建高效的应用程序。 MyBatis是一个开源的持久层框架,它可以将SQL语句和数据库操作映射到Java对象中,使得开发人员可以通过简单的配置来实现数据访问层的操作。在Spring Boot中集成MyBatis可以通过添加相应的依赖和配置来实现。 PageHelper是一个用于实现分页功能的MyBatis插件。它可以通过拦截SQL语句并自动添加分页查询的相关信息,从而简化了分页查询的操作。在Spring Boot中集成PageHelper可以通过添加相应的依赖和配置来实现。 Swagger是一个用于生成、描述、调用和可视化RESTful风格的Web服务的工具集。它可以根据代码注解自动生成接口文档,并提供了一个用户友好的界面来测试和调试接口。在Spring Boot中集成Swagger可以通过添加相应的依赖和配置来实现。 下面是集成Spring Boot、MyBatis、PageHelper和Swagger的步骤: 1. 在pom.xml文件中添加Spring Boot、MyBatis、PageHelper和Swagger的依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> </dependencies> ``` 2. 在application.properties或application.yml文件中配置数据库连接和MyBatis的相关配置: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # MyBatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.model # PageHelper配置 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true # Swagger配置 swagger.enabled=true ``` 3. 创建MyBatis的Mapper接口和对应的XML文件,定义数据库操作的SQL语句和映射关系。 4. 创建Spring Boot的Controller类,定义接口的请求路径和处理方法。 5. 在启动类上添加@EnableSwagger2注解,启用Swagger。 ```java @SpringBootApplication @EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 6. 启动应用程序,访问http://localhost:8080/swagger-ui.html可以查看生成的接口文档,并进行接口的测试和调试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值