java分页封装

分页是前端常见的需求,分页的使用可以美观的展示数据,同时大数据的情况下,不需要全部加载数据库的数据,减少后台响应前台的时间。可以避免用户见长时间的等待。

封装分页的步骤
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}
4、结果显示

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值