分页是前端常见的需求,分页的使用可以美观的展示数据,同时大数据的情况下,不需要全部加载数据库的数据,减少后台响应前台的时间。可以避免用户见长时间的等待。
封装分页的步骤
1、定义一个分页对象
public class PageResult<T> {
private Integer pageNo = 1; // 当前页码
private Integer pageSize = 20; // 每页显示的数量
private Integer total = 0; // 总数量
private Integer totalPages = 0; // 总页数
private List<T> result = Collections.emptyList(); // 返回的结果列表
private Integer start = 1;//起始页
private Integer end = 0;//结束页
/**
* 获取返回结果
* @param result
*/
public void setRecords(List<T> result) {
this.result = result;
if (result != null && result.size() > 0) {
setTotal(result.size());
}
}
/**
* 设置总条数
* @param total
*/
public void setTotal(Integer total) {
this.total = total;
if (this.pageSize > 0) {
this.totalPages = (total / this.pageSize) + (total % this.pageSize == 0 ? 0 : 1);
} else {
this.totalPages = 0;
}
this.start = (this.pageSize > 0 ? (this.pageNo - 1) * this.pageSize : 0) + 1;
this.end = (this.start - 1 + this.pageSize * (this.pageNo > 0 ? 1 : 0));
}
/**
* 设置分页大小
* @param pageSize
*/
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
/**
* 设置页码
* @param pageNo
*/
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
}
2、编写查询
public PageResult<SubjectInfoBo> getSubjectPage(SubjectInfoBo subjectInfoBo) {
PageResult<SubjectInfoBo> subjectInfoBoPageResult = new PageResult<>();
SubjectInfo subjectInfo = SubjectInfoBoConvert.INSTANCE.subjectInfoToBoConvert(subjectInfoBo);
subjectInfo.setIsDeleted(IsDeleteCodeEnum.NO_DELETE.getCode());
//获取总数
int total = subjectInfoService.countByCondition(subjectInfo,
subjectInfoBo.getLabelId(),
subjectInfoBo.getCategoryId());
if (total == 0) {
return subjectInfoBoPageResult;
}
List<SubjectInfo> subjectInfoList = subjectInfoService.queryPage(subjectInfo,
subjectInfoBo.getCategoryId(),
subjectInfoBo.getLabelId(),subjectInfoBo.getStater(),subjectInfoBo.getPageSize());
List<SubjectInfoBo> subjectInfoBos = SubjectInfoBoConvert.INSTANCE.subjectInfoListToBoConvert(subjectInfoList);
subjectInfoBoPageResult.setRecords(subjectInfoBos);
subjectInfoBoPageResult.setTotal(total);
return subjectInfoBoPageResult;
}
3、sql编写
这里sql是项目代码,所有这里两张表存在映射关系,大家可以使用单表
<select id="countByCondition" resultType="java.lang.Integer">
select count(1)
from subject_info a,subject_mapping b
<where>
<if test="subjectInfo.isDeleted != null">
and a.is_deleted = #{subjectInfo.isDeleted}
</if>
<if test="labelId != null">
and b.label_id = #{labelId}
</if>
<if test="categoryId != null">
and b.category_id = #{categoryId}
</if>
</where>
</select>
<!--分页查询题目信息-->
<select id="queryPage" resultType="com.jrlyt.subject.infra.base.entity.SubjectInfo">
select
a.id, subject_name, subject_difficult, settle_name, subject_type, subject_score, subject_parse, a.created_by,
a.created_time, a.update_by, a.update_time, a.is_deleted
from subject_info a,subject_mapping b
<where>
and b.label_id = #{labelId}
and b.category_id = #{categoryId}
<if test="subjectInfo.isDeleted != null">
and a.is_deleted = #{subjectInfo.isDeleted}
</if>
<if test="labelId != null">
and b.label_id = #{labelId}
</if>
<if test="categoryId != null">
and b.category_id = #{categoryId}
</if>
</where>
limit #{stater}, #{pageSize}