您的位置:首页 > 其它

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:将数据展示到页面可以使用插件也可自己原生写,有机会一定补上       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: