最近在做一个公司的管理系统,涉及到很多表格数据的展示,就需要做分页,这时通常有两种方式,一种是前端一次请求获取全部数据,前端再对数据做分页,这种方式如果数据规模比较小的情况下可以使用,如果数据量较大,对内存、网络传输的消耗都是非常大的,所以实际开发中一般很少使用。另外一种方式是前端在请求时将分页信息传给后端,后端查询时进行分页,并将相应的分页数据返回给前端,而后端分页的实现又可以分为逻辑分页和物理分页,逻辑分页就是在进行数据库查询时一次性将数据查出来,然后将相应页的数据筛选出来返回,很明显逻辑分页跟第一种前端分页的方式有着相同的弊端。物理分页就是在查数据库时就查询相应页的数据(比如直接在mysql查询语句中添加limit)。
mybatis原生也支持分页,但为了与数据库语法解耦,实现的是逻辑分页,首先将所有结果查询出来,然后通过计算offset和limit,来返回部分结果,操作在内存中进行,所以也叫内存分页,Mybatis逻辑分页是通过RowBounds实现的。而物理分页一般是通过为sql添加limit实现的,本文主要讲解物理分页,通过改造mapper.xml文件添加limit的方式实现。
sql添加limit实现物理分页
1.用户持久化类
package com.test.po;
import java.util.Date;
/**
* 用户持久化类
*
* @author xxx
* @version 1.0
*/
public class User {
/*
* 用户ID
*/
private String userId;
/*
* 用户名
*/
private String userName;
/*
* IP地址
*/
private String ip;
/*
* 用户角色
*/
private int roleNum;
/*
* 创建时间
*/
private Date createtime;
/**
* @return the userId
*/
public String getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the ip
*/
public String getIp() {
return ip;
}
/**
* @param ip the ip to set
*/
public void setIp(String ip) {
this.ip = ip;
}
/**
* @return the roleNum
*/
public int getRoleNum() {
return roleNum;
}
/**
* @param roleNum the roleNum to set
*/
public void setRoleNum(int roleNum) {
this.roleNum = roleNum;
}
/**
* @return the createtime
*/
public Date getCreatetime() {
return createtime;
}
/**
* @param createtime the createtime to set
*/
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
}
2.mapper.xml
<!-- 分页获取用户列表 -->
<resultMap id="userEntity" type="com.test.po.User"></resultMap>
<resultMap id="userTotalCount" type="int"></resultMap>
<select id="getUserList" parameterType="hashmap" resultMap="userEntity,userTotalCount">
SELECT sql_calc_found_rows
user_id as userId,
user_name as userName,
ip,
role_num as roleNum,
createtime,
FROM tb_test_user
ORDER BY user_id ASC limit #{curCount,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER};
<!-- 查询数量 -->
select found_rows() as userTotalCount;
</select>
3.Service层 用户管理类
package com.test.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONObject;
import com.test.po.User;
/**
*
* 用户管理Service
*
* @author xxx
* @version 1.0
*/
@Service
public class UserService {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
/**
* 按分页列出用户
*
* @param param
* @return
*/
public JSONObject listUsers(int pageNum, int pageSize) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("curCount", (pageNum - 1) * pageSize);
paramMap.put("pageSize", pageSize);
List<List<?>> list = this.nlpwebSqlSessionTemplate.selectList("getUserList", paramMap);
JSONObject resultObject = new JSONObject();
resultObject.put("totalCount",list.get(1).get(0));
resultObject.put("dataList", list.get(0));
return resultObject;
}
}