import java.util.List;
/***
* VO: 封装当前页数据(记录信息和分页信息)
* @author 胡r
* @param <T>
*/
public class PageObject<T> {
/**每页显示的记录*/
private List<T> records;
/**总页数*/
private int pageCount;
/**总记录数*/
private int rowCount;
/**当前页面大小*/
private int pageSize=3;
/**当前页的页码*/
private int pageCurrent=1;
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
public int getPageCount() {
//或者用return (int)Math.ceil(rowCount*1.0/pagSize);
pageCount=rowCount/pageSize;
if(rowCount%pageSize!=0)pageCount++;
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageCurrent() {
return pageCurrent;
}
public void setPageCurrent(int pageCurrent) {
this.pageCurrent = pageCurrent;
}
}
//业务层方法
@Override
public PageObject<SysConfig> findPageObjects(
Integer pageCurrent,String name) {
//1.查询总记录数
int rowCount=sysConfigDao.getRowCount(name);
if(rowCount==0)
throw new ServiceException("没有记录");
//2.查询当前页记录
//2.1定义页面大小(每页最多现实多少条记录)
int pageSize=3;
//2.2计算当前页位置
if(pageCurrent==null||pageCurrent<=0)
throw new ServiceException("当前页码不合法");
int startIndex=(pageCurrent-1)*pageSize;
//2.3查询当前数据
List<SysConfig> list=
sysConfigDao.findPageObjects(startIndex,pageSize,name);
//3.封装数据
PageObject<SysConfig> pageObject=new PageObject<>();
pageObject.setRecords(list);
pageObject.setRowCount(rowCount);
pageObject.setPageSize(pageSize);
pageObject.setPageCurrent(pageCurrent);
return pageObject;
}
<sql id="pageWhere">
<where>
<if test="name!=null and name!=''">
name like concat("%",#{name},"%")
</if>
</where>
</sql>
<select id="findPageObjects"
resultType="sysConfig">
select *
from sys_configs
<include refid="pageWhere"/>
order by createdTime desc
limit #{startIndex},#{pageSize}
</select>
<select id="getRowCount"
resultType="int">
select count(*)
from sys_configs
<include refid="pageWhere"/>
</select>