mybatis实现物理分页
2017-01-17 14:59
183 查看
物理分页:把数据在数据库中进行分页,得到需要的那页信息
逻辑分页:把数据从数据库中全部拿出来,在后台进行分页,得到需要的那页信息
背景:ssm框架+mysql
一:准备pager类public class Pager<T> {
private int pageSize;// 每页显示多少条记录
private int currentPage;// 当前第几页数据
private int totalRecord;// 一共多少条记录
private int totalPage;// 一共多少页记录
private List<T> dataList;// 要显示的数据
public Pager() {
super();
// TODO Auto-generated constructor stub
}
public Pager(int pageSize, int currentPage, int totalRecord, int totalPage, List<T> dataList) {
super();
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalRecord = totalRecord;
this.totalPage = totalPage;
this.dataList = dataList;
}
public Pager(int pageNum, int pageSize, List<T> sourceList) {
if (sourceList == null || sourceList.isEmpty()) {
return;
}
// 总记录条数
this.totalRecord = sourceList.size();
// 每页显示多少条记录
this.pageSize = pageSize;
// 获取总页数
this.totalPage = this.totalRecord / this.pageSize;
if (this.totalRecord % this.pageSize != 0) {
this.totalPage = this.totalPage + 1;
}
// 当前第几页数据
this.currentPage = this.totalPage < pageNum ? this.totalPage : pageNum;
// 起始索引
int fromIndex = this.pageSize * (this.currentPage - 1);
// 结束索引
int toIndex = this.pageSize * this.currentPage > this.totalRecord ? this.totalRecord
: this.pageSize * this.currentPage;
this.dataList = sourceList.subList(fromIndex, toIndex);
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
}
二:写好xxxMapper.xml中的配置
<select id="getCount" resultType="java.lang.Integer">
select
count(*)
from t_user
where 1=1
<if test="email!=null and email!=''">
and email like '%'+#{email,jdbcType=VARCHAR}+'%'
</if>
<if test="nickname!=null and nickname!=''">
and nickname like '%'+#{nickname,jdbcType=VARCHAR+'%'
</if>
</select>
<select id="findUsersByPager" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
where 1=1
<if test="email!=null and email!=''">
and email like '%'+#{email,jdbcType=VARCHAR}+'%'
</if>
<if test="nickname!=null and nickname!=''">
and nickname like '%'+#{nickname,jdbcType=VARCHAR+'%'
</if>
limit #{fromIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</select>
三:写好xxxService、xxxsServiceImpl
@Override
public List<User> findUsersByPage(String email,String nickname,int fromIndex, int pageSize) {
return userMapper.findUsersByPager(email,nickname,fromIndex, pageSize);
}
@Override
public int getCount(String email, String nickname) {
return userMapper.getCount(email, nickname);
}
@Override
public Pager<User> getUsersPage(String email, String nickname, int currentPage, int pageSize) {
int totalRecord=userMapper.getCount(email, nickname);
int totalPage=totalRecord/pageSize;
if(totalRecord%pageSize!=0){
totalPage++;
}
int fromIndex=(currentPage-1)*pageSize;
List<User> list=userMapper.findUsersByPager(email, nickname, fromIndex, pageSize);//找到那页所需的数据
Pager<User> pager=new Pager<User>(pageSize, currentPage, totalRecord, totalPage, list);
return pager;
}
四:junit测试
ps:将数据展示到页面可以使用插件也可自己原生写,有机会一定补上
逻辑分页:把数据从数据库中全部拿出来,在后台进行分页,得到需要的那页信息
背景:ssm框架+mysql
一:准备pager类public class Pager<T> {
private int pageSize;// 每页显示多少条记录
private int currentPage;// 当前第几页数据
private int totalRecord;// 一共多少条记录
private int totalPage;// 一共多少页记录
private List<T> dataList;// 要显示的数据
public Pager() {
super();
// TODO Auto-generated constructor stub
}
public Pager(int pageSize, int currentPage, int totalRecord, int totalPage, List<T> dataList) {
super();
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalRecord = totalRecord;
this.totalPage = totalPage;
this.dataList = dataList;
}
public Pager(int pageNum, int pageSize, List<T> sourceList) {
if (sourceList == null || sourceList.isEmpty()) {
return;
}
// 总记录条数
this.totalRecord = sourceList.size();
// 每页显示多少条记录
this.pageSize = pageSize;
// 获取总页数
this.totalPage = this.totalRecord / this.pageSize;
if (this.totalRecord % this.pageSize != 0) {
this.totalPage = this.totalPage + 1;
}
// 当前第几页数据
this.currentPage = this.totalPage < pageNum ? this.totalPage : pageNum;
// 起始索引
int fromIndex = this.pageSize * (this.currentPage - 1);
// 结束索引
int toIndex = this.pageSize * this.currentPage > this.totalRecord ? this.totalRecord
: this.pageSize * this.currentPage;
this.dataList = sourceList.subList(fromIndex, toIndex);
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
}
二:写好xxxMapper.xml中的配置
<select id="getCount" resultType="java.lang.Integer">
select
count(*)
from t_user
where 1=1
<if test="email!=null and email!=''">
and email like '%'+#{email,jdbcType=VARCHAR}+'%'
</if>
<if test="nickname!=null and nickname!=''">
and nickname like '%'+#{nickname,jdbcType=VARCHAR+'%'
</if>
</select>
<select id="findUsersByPager" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
where 1=1
<if test="email!=null and email!=''">
and email like '%'+#{email,jdbcType=VARCHAR}+'%'
</if>
<if test="nickname!=null and nickname!=''">
and nickname like '%'+#{nickname,jdbcType=VARCHAR+'%'
</if>
limit #{fromIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</select>
三:写好xxxService、xxxsServiceImpl
@Override
public List<User> findUsersByPage(String email,String nickname,int fromIndex, int pageSize) {
return userMapper.findUsersByPager(email,nickname,fromIndex, pageSize);
}
@Override
public int getCount(String email, String nickname) {
return userMapper.getCount(email, nickname);
}
@Override
public Pager<User> getUsersPage(String email, String nickname, int currentPage, int pageSize) {
int totalRecord=userMapper.getCount(email, nickname);
int totalPage=totalRecord/pageSize;
if(totalRecord%pageSize!=0){
totalPage++;
}
int fromIndex=(currentPage-1)*pageSize;
List<User> list=userMapper.findUsersByPager(email, nickname, fromIndex, pageSize);//找到那页所需的数据
Pager<User> pager=new Pager<User>(pageSize, currentPage, totalRecord, totalPage, list);
return pager;
}
四:junit测试
ps:将数据展示到页面可以使用插件也可自己原生写,有机会一定补上
相关文章推荐
- Mybatis实现物理分页
- mybatis实现物理分页准备之mybatis配置文件解析及实例
- mybatis物理分页的实现
- Mybatis技术: 实现物理分页
- mybatis实现物理分页
- JavaWeb从新手到入门(四)Spring Mybatis使用pageHelper实现物理分页
- Mybatis 实现物理分页的简单实现
- [Java][MyBatis]物理分页实现
- Mybatis 数据库物理分页插件 PageHelper 前端实现
- 利用Mybatis的动态SQL实现物理分页
- mybatis物理分页的实现(转)
- mybatis关联字表分页查询实现(物理分页)
- MyBatis实现物理分页的实例
- Mybatis实现物理分页
- MyBatis物理分页的代码实现
- Spring-mybatis3.4物理分页实现
- Mybatis技术(三) 实现物理分页
- MyBatis:简单物理分页实现(Plugin)
- mybatis框架下物理分页的实现(整个工程采用的是springmvc、spring、mybatis框架,数据库是mysql数据库)
- MyBatis:简单物理分页实现(Plugin)