使用ssm实现分页

本文中间省略了service层的实现,因为service层比较简单,调用mapper中的方法即可。
本文还实现了模糊查询。如果只实现分页,可以忽略PageBean中的custom属性。

1.PageBean实现

public class PageBean<T> {

    private int pc; //当前页码
//  private int tp; //总页数
    private int tr; //总记录数
    private int ps; //每页记录数
    private List<T> beanList; //当前页记录
    private int s;  //每页开始的记录数
    private T custom;//扩展类



    public T getCustom() {
        return custom;
    }

    public void setCustom(T custom) {
        this.custom = custom;
    }

    private String url;

    public int getS() {
        return (pc-1)*ps;
    }

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public int getPc() {
        return pc;
    }
    public void setPc(int pc) {
        this.pc = pc;
    }
    public int getTp() {
        int tp = tr/ps;
        return tr%ps==0 ? tp : tp+1;
    }
//  public void setTp(int tp) {
//      this.tp = tp;
//  }
    public int getTr() {
        return tr;
    }
    public void setTr(int tr) {
        this.tr = tr;
    }
    public int getPs() {
        return ps;
    }
    public void setPs(int ps) {
        this.ps = ps;
    }
    public List<T> getBeanList() {
        return beanList;
    }
    public void setBeanList(List<T> beanList) {
        this.beanList = beanList;
    }


}

2.实现mapper

<sql id="query_user_where">
        <if test="custom!=null">
            <if test="custom.uname!=null and custom.uname!=''">
                user.uname LIKE '%${custom.uname}%'
            </if>
        </if>
   </sql>
<select id="findUserListByUnamePage" parameterType="pageBean"
        resultType="userCustom">
        SELECT user.* FROM user 
        <where>
            <include refid="query_user_where"></include>
        </where>
        LIMIT ${s},${ps}
    </select>

s是每页的开始的记录,ps是每页的记录数。

3.controller实现

@RequestMapping("/queryUserByUnamePage")
    public String queryUserByUnamePage(HttpServletRequest request ,Model model,UserQueryVo userQueryVo) throws Exception {
        Integer count = service.findUserCountByUname(userQueryVo);

        String uname = null;
        StringBuffer url = new StringBuffer();
        if(userQueryVo.getCustom() != null) {
            uname = userQueryVo.getCustom().getUname();
        }
        if(userQueryVo.getPc() == null) {
            userQueryVo.setPc(1);
        }
        PageBean<UserCustom> pageBean = new PageBean<UserCustom>();
        pageBean.setPs(pageSice);
        pageBean.setTr(count);
        pageBean.setPc(userQueryVo.getPc());
        url.append(request.getRequestURI()+"?0=0");
        if(uname != null) {
            url.append("&custom.uname=" + uname);
        }
        pageBean.setUrl(url.toString());
        pageBean.setCustom(userQueryVo.getCustom());
        List<UserCustom> list = service.findUserListByUnamePage(pageBean);
        model.addAttribute("userList", list);
        model.addAttribute("pb", pageBean);
        return "/user_tables";

    }

4.jsp页面实现

${pb.pc } 页/共${pb.tp }页
        <a href="${pb.url }&pc=1">首页</a>
        <c:if test="${pb.pc > 1 }">
            <a href="${pb.url }&pc=${pb.pc-1}">上一页</a>
        </c:if>
        <c:choose>
            <c:when test="${pb.tp <= 10 }">
                <c:set var="begin" value="1"/>
                <c:set var="end" value="${pb.tp }"/>
            </c:when>
            <c:otherwise>
                <c:set var="begin" value="${pb.pc - 5 }"/>
                <c:set var="end" value="${pb.pc + 4 }"/>

                <c:if test="${begin < 1 }">
                    <c:set var="begin" value="1"/>
                    <c:set var="end" value="10"/>
                </c:if>
                <c:if test="${end > pb.tp }">
                    <c:set var="begin" value="${pb.tp-9 }"/>
                    <c:set var="end" value="${pb.tp }"/>
                </c:if>
            </c:otherwise>
        </c:choose>

        <c:forEach var="i" begin="${begin }" end="${end }">
            <c:choose>
                <c:when test="${pb.pc == i }">
                    [${i }]
                </c:when>
                <c:otherwise>
                    <a href="${pb.url }&pc=${i }">[${i }]</a>
                </c:otherwise>
            </c:choose>

        </c:forEach>


        <c:if test="${pb.pc < pb.tp }">
            <a href="${pb.url }&pc=${pb.pc+1}">下一页</a>
        </c:if>
        <a href="${pb.url }&pc=${pb.tp}">尾页</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值