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&batis.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.配置l
# 与mybatis整合
mybatis:
config-location:l
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 ansaction.annotation.Transactional;
import org.t.ContextLoader;
import ity.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 = List();
return typeList;
}
}
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 ity.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 {
// 调⽤业务逻辑,返回数据
List(pageNum,pageSize);
}
}
5.测试
已知我数据库中有九条数据:
正常情况:
1.显⽰第⼀页或者第⼆页数据
请求url:
localhost:8080/getGoodsTypeList?pageNum=1&pageSize=4
返回数据:
"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:
localhost:8080/getGoodsTypeList?pageNum=3&pageSize=4返回数据:
[
{
"typeId": "d992195f6df111e7bab4e09467355fab",
"typeName": "测试2改变了",
"createTime": 1501145516000,
"updateTime": 1500716178000
}
]
不正常情况:
1.显⽰的页数⼩于第⼀页(显⽰第⼀页数据)
pageNumber <= 0
请求url:
localhost:8080/getGoodsTypeList?pageNum=0&pageSize=4返回数据:
"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:
localhost:8080/getGoodsTypeList?pageNum=4&pageSize=4返回数据:
[
{
"typeId": "d992195f6df111e7bab4e09467355fab",
"typeName": "测试2改变了",
"createTime": 1501145516000,
"updateTime": 1500716178000
}
]
结论:当请求页⾯⼤于最后⼀页时,显⽰最后⼀页数据
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论