SpringBoot学习-(七)SpringBoot分页插件PageHelper
来源:互联网 发布:淘宝开店后怎么卖东西 编辑:程序博客网 时间:2024/06/04 22:33
访问数据库采用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.2</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+@ResponseBodypublic 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 }]
结论:当请求页面大于最后一页时,显示最后一页数据
阅读全文
0 0
- SpringBoot学习-(七)SpringBoot分页插件PageHelper
- springboot使用pageHelper分页插件
- SpringBoot集成分页插件PageHelper
- springboot +mybatis + pagehelper分页插件
- SpringBoot集成MyBatis的分页插件PageHelper
- SpringBoot整合Mybaties分页插件PageHelper
- springboot+mybatis+pagehelper分页
- (4)学习SpringBoot之整合 Mybatis+PageHelper分页
- Gradle构建SpringBoot+Mybatis集成分页插件PageHelper
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- SpringBoot Mybatis PageHelper分页插件的两种用法(一)
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)
- springboot+mybatis分页插件pageHelper的配置与使用
- SpringBoot使用PageHelper进行分页
- springboot学习10之jersey+mybatis+通用mapper+分页PageHelper
- Springboot使用PageHelper插件实现翻页
- SpringBoot自我整理学习3(springboot集成Mybatis+PageHelp分页插件)
- SpringBoot学习-(五)SpringBoot测试
- 求1+2+···+n
- [BZOJ2440] 完全平方数 莫比乌斯函数+容斥+二分
- 如何实现两个文本框同时输入
- 《PHP MySQL和WEB开发》第八章笔记
- Elasticsearch.5.4.1+kibana5.4.1+IK分词集群搭建
- SpringBoot学习-(七)SpringBoot分页插件PageHelper
- 动手打造史上最简单的 Recycleview 侧滑菜单
- Java之旅--跨域(CORS)
- 【数据库】count(*),count(1)和count(列)
- leetcode 221. Maximal Square 最大正方形面积 + DP
- css3 transition 动画
- 超好用的 Linux 文件管理器推荐
- 《代码整洁之道》读书笔记
- Yii2文件上传