一、普通分页:
1、分页参数dto:
(1)封装分页类
import java.io.Serializable;
import java.util.List;
/**
* 分页列表。
* <p>
* 用于传输后端进行分页的列表数据。
* @param <T> 列表中元素的类型。
*/
public class Pager<T> implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 总记录数。
*/
private int totalCount;
/**
* 当前页的记录列表。
*/
private List<T> list;
/**
* 构造函数。
* @param totalCount 总记录数。
* @param list 当前页的记录列表。
*/
public Pager(int totalCount,List<T> list){
this.totalCount=totalCount;
this.list=list;
}
/**
* @return the totalCount
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount the totalCount to set
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/**
* @return the list
*/
public List<T> getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(List<T> list) {
this.list = list;
}
}
(2)封装分页页码计算方法
/**
* 分页参数。
* <p>
* 用于传递用来分页的相关参数。
*
*/
public class PageParam implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 当前页码,从1开始。
*/
private int pageIndex;
/**
* 每页记录数。
*/
private int pageSize = 10;
/**
* 排序字段。
*/
private String orderBy;
/**
* 排序方向。
*/
private OrderDirection orderDirection;
/**
* 构造函数
*/
public PageParam() {
}
/**
* 构造函数。
*
* @param pageIndex
* 当前页码,从1开始。
* @param pageSize
* 每页记录数。
*/
public PageParam(int pageIndex, int pageSize) {
this.pageIndex = pageIndex;
this.pageSize = pageSize;
}
/**
* 构造函数。
*
* @param pageIndex
* 当前页码,从1开始。
* @param pageSize
* 每页记录数。
* @param orderBy
* 排序字段
* @param orderDirection
* 排序方向。
*/
public PageParam(int pageIndex, int pageSize, String orderBy, OrderDirection orderDirection) {
this.pageIndex = pageIndex;
this.pageSize = pageSize;
this.orderBy = orderBy;
this.orderDirection = orderDirection;
}
/**
* 得到开始记录index。
*
* @return 开始记录index。
*/
public int getStartIndex() {
return (pageIndex - 1) * pageSize;
}
/**
* @return the pageIndex
*/
public int getPageIndex() {
return pageIndex;
}
/**
* @param pageIndex
* the pageIndex to set
*/
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
/**
* @return the pageSize
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
* the pageSize to set
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return the orderBy
*/
public String getOrderBy() {
return orderBy;
}
/**
* @param orderBy
* the orderBy to set
*/
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
/**
* @return the orderDirection
*/
public OrderDirection getOrderDirection() {
return orderDirection;
}
/**
* @param orderDirection
* the orderDirection to set
*/
public void setOrderDirection(OrderDirection orderDirection) {
this.orderDirection = orderDirection;
}
}
(3)流式分页方向:后面讲 流式分页
/**
* 排序方向。
*/
public enum OrderDirection {
/**
* 正序
*/
ASC,
/**
* 倒序
*/
DESC
}
2、controller:
@RequestMapping("/getUserPager")
public HttpResult getPagerLog(int pageIndex, int pageSize, String userName){
try{
Pager<User> userPager = elTaskService.getUserPager(new PageParam(pageIndex,pageSize),userName);
return HttpResult.getSuccessInstance(userPager);
}catch (Exception e) {
}
return HttpResult.getFailedInstance("获取列表失败");
}
3、service:
@Override
public Pager<ElTask> getUserPager(PageParam pageParam, String userName) {
Assert.notNull(pageParam);
int totalCount = userMapper.getPagerCount(userName);
List<User> pagerList = new ArrayList<>();
if(totalCount > 0){
pagerList = userMapper.getPagerList(pageParam.getStartIndex(),pageParam.getPageSize(),
userName);
}
return new Pager<>(totalCount, pagerList);
}
4、dao:
<select id="getPagerCount" resultType="int">
select count(1) from t_user
where 1=1
<if test = "userName != null and userName !=''">
and user_name=#{userName}
</if>
</select>
<select id="getPagerList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
where 1=1
<if test = "userName != null and userName !=''">
and user_name=#{userName}
</if>
limit #{startIndex},#{pageSize}
</select>
二、普通分页第三方-PageHelper :拓展(一)mybatis+pagerHelper做分页查询_w_t_y_y的博客-优快云博客
三、流式分页:流式分页用于app中的分页,app调用web接口,接口不可能一次性返回所有数据,这样在数据量很大的情况下性能会很低,这时就可以采用流式分页,根据上拉下拉一次性返回部分数据。
例:现t_user表有字段userId(主键)、userName、userPwd
1、工具类:
/**
* 分页参数
*/
public class DirectionalPageParam implements Serializable{
private static final long serialVersionUID = -4370396647242992688L;
private static final int MIN_PAGE_SIZE = 1;
private static final int MAX_PAGE_SIZE = 1000;
private Direction direction;
private Long id;
private int pageSize;
public DirectionalPageParam(){
}
public DirectionalPageParam(Direction direction, Long id, int pageSize){
this.direction = direction;
this.id = id;
setPageSize(pageSize);
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if (pageSize >= MIN_PAGE_SIZE && pageSize <= MAX_PAGE_SIZE){
this.pageSize = pageSize;
}else {
throw new SnsException(String.format("page size should be ranged in [%s, %s]",MIN_PAGE_SIZE,MAX_PAGE_SIZE));
}
}
}
/**
* 方向
*/
public enum Direction {
/**
* 第一页
*/
FIRST,
/**
* 前面的一页
*/
PREVIOUS,
/**
* 后面的一页
*/
AFTER
}
2、controller需要传入(String pullType, String id) :
JSONArray resultJsonArr = new JSONArray();
DirectionalPageParam pageParam = new DirectionalPageParam();
if (StringUtils.isNotBlank(pullType)) {
if (Direction.FIRST.toString().equals(pullType)) {
pageParam.setDirection(Direction.FIRST);
pageParam.setId(null);
} else if (Direction.PREVIOUS.toString().equals(pullType)) {
pageParam.setDirection(Direction.PREVIOUS);
if (StringUtils.isNotBlank(id)) {
pageParam.setId(Long.parseLong(id));
} else {
return AppJSONResult.getSuccessInstance(resultJsonArr);
}
} else if (Direction.AFTER.toString().equals(pullType)) {
pageParam.setDirection(Direction.AFTER);
if (StringUtils.isNotBlank(id)) {
pageParam.setId(Long.parseLong(id));
} else {
return AppJSONResult.getSuccessInstance(resultJsonArr);
}
} else {
return AppJSONResult.getSuccessInstance(resultJsonArr);
}
} else {
return AppJSONResult.getSuccessInstance(resultJsonArr);
}
pageParam.setPageSize(3);
List<User> userList= userService.getUserList(pageParam);
3、service:
/**
* 分页获取<br>
* @param pageParam 分页参数
* @return
*/
List<User> getUserList(DirectionalPageParam pageParam);
4、dao:
pageSize固定,查询根据方向分为三种情况:
(1)方向为FIRST:第一页,不需要id; select * from t_user limit pageSize;
(2)方向为PREVIOUS:前一页,需要id;select * from t_user where userId<id limit pageSize;
(3)方向为AFTER:后一页,需要id;select * from t_user where userId>id limit pageSize。