java分页实现

3 篇文章 0 订阅
2 篇文章 0 订阅
首先是dataobject,项目是用的是SpringMVC
public class MbChannelExtensionDO  {
    private static final long serialVersionUID = 741231858441822688L;

    //========== properties ==========

	/**
	 * This property corresponds to db column <tt>ID</tt>.
	 */
	private String id;

	/**
	 * This property corresponds to db column <tt>BANK_NAME</tt>.
	 */
	private String bankName;

	/**
	 * This property corresponds to db column <tt>PRODUCT_TYPE</tt>.
	 */
	private String productType;

	/**
	 * This property corresponds to db column <tt>EXCHANGE_TYPE</tt>.
	 */
	private String exchangeType;

	/**
	 * This property corresponds to db column <tt>SIGN_VERIFY_ELE</tt>.
	 */
	private String signVerifyEle;

	/**
	 * This property corresponds to db column <tt>CHANNEL_ID</tt>.
	 */
	private String channelId;

	/**
	 * This property corresponds to db column <tt>GMT_CREATE</tt>.
	 */
	private Date gmtCreate;

	/**
	 * This property corresponds to db column <tt>CREATOR</tt>.
	 */
	private String creator;

	/**
	 * This property corresponds to db column <tt>GMT_MODIFIED</tt>.
	 */
	private Date gmtModified;

	/**
	 * This property corresponds to db column <tt>UPDATOR</tt>.
	 */
	private String updator;

	/**
	 * This property corresponds to db column <tt>STATUS</tt>.
	 */
	private String status;

    //========== getters and setters ==========

    /**
     * Getter method for property <tt>id</tt>.
     *
     * @return property value of id
     */
	public String getId() {
		return id;
	}
	
	/**
	 * Setter method for property <tt>id</tt>.
	 * 
	 * @param id value to be assigned to property id
     */
	public void setId(String id) {
		this.id = id;
	}

    /**
     * Getter method for property <tt>BANK_NAME</tt>.
     *
     * @return property value of bankName
     */
	public String getBankName() {
		return bankName;
	}
	
	/**
	 * Setter method for property <tt>BANK_NAME</tt>.
	 * 
	 * @param bankName value to be assigned to property bankName
     */
	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

    /**
     * Getter method for property <tt>PRODUCT_TYPE</tt>.
     *
     * @return property value of productTYPE
     */
	public String getProductType() {
		return productType;
	}
	
	/**
	 * Setter method for property <tt>PRODUCT_TYPE</tt>.
	 * 
	 * @param productType value to be assigned to property productType
     */
	public void setProductType(String productType) {
		this.productType = productType;
	}

	/**
	 * Getter method for property <tt>SIGN_VERIFY_ELE</tt>.
	 *
	 * @return property value of signVerifyEle
	 */
	public String getSignVerifyEle() {
		return signVerifyEle;
	}

	/**
	 * Setter method for property <tt>SIGN_VERIFY_ELE</tt>.
	 *
	 * @param signVerifyEle value to be assigned to property signVerifyEle
	 */
	public void setSignVerifyEle(String signVerifyEle) {
		this.signVerifyEle = signVerifyEle;
	}

    /**
     * Getter method for property <tt>EXCHANGE_TYPE</tt>.
     *
     * @return property value of signVerifyEle
     */
    public String getExchangeType() {
        return exchangeType;
    }

    /**
     * Setter method for property <tt>EXCHANGE_TYPE</tt>.
     *
     * @param exchangeType value to be assigned to property exchangeType
     */
    public void setExchangeType(String exchangeType ) { this.exchangeType = exchangeType;}
    /**
     * Getter method for property <tt>channelId</tt>.
     *
     * @return property value of channelId
     */
	public String getChannelId() {
		return channelId;
	}
	
	/**
	 * Setter method for property <tt>channelId</tt>.
	 * 
	 * @param channelId value to be assigned to property channelId
     */
	public void setChannelId(String channelId) {
		this.channelId = channelId;
	}

    /**
     * Getter method for property <tt>gmtCreate</tt>.
     *
     * @return property value of gmtCreate
     */
	public Date getGmtCreate() {
		return gmtCreate;
	}
	
	/**
	 * Setter method for property <tt>gmtCreate</tt>.
	 * 
	 * @param gmtCreate value to be assigned to property gmtCreate
     */
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}

    /**
     * Getter method for property <tt>creator</tt>.
     *
     * @return property value of creator
     */
	public String getCreator() {
		return creator;
	}
	
	/**
	 * Setter method for property <tt>creator</tt>.
	 * 
	 * @param creator value to be assigned to property creator
     */
	public void setCreator(String creator) {
		this.creator = creator;
	}

    /**
     * Getter method for property <tt>gmtModified</tt>.
     *
     * @return property value of gmtModified
     */
	public Date getGmtModified() {
		return gmtModified;
	}
	
	/**
	 * Setter method for property <tt>gmtModified</tt>.
	 * 
	 * @param gmtModified value to be assigned to property gmtModified
     */
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}

    /**
     * Getter method for property <tt>updator</tt>.
     *
     * @return property value of updator
     */
	public String getUpdator() {
		return updator;
	}
	
	/**
	 * Setter method for property <tt>updator</tt>.
	 * 
	 * @param updator value to be assigned to property updator
     */
	public void setUpdator(String updator) {
		this.updator = updator;
	}

    /**
     * Getter method for property <tt>status</tt>.
     *
     * @return property value of status
     */
	public String getStatus() {
		return status;
	}
	
	/**
	 * Setter method for property <tt>status</tt>.
	 * 
	 * @param status value to be assigned to property status
     */
	public void setStatus(String status) {
		this.status = status;
	}


接着是使用分页的Page类

public class PageDO {
    /**
     * 总条数
     */
    private int totalNumber;
    /**
     * 当前第几页
     */
    private int currentPage;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 每页显示条数
     */
    private int pageNumber = 10;

    /**
     * 根据当前对象中属性值计算并设置相关属性值
     */
    public void count() {
        // 计算总页数
        int totalPageTemp = this.totalNumber / this.pageNumber;
        int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1;
        totalPageTemp = totalPageTemp + plus;
        if(totalPageTemp <= 0) {
            totalPageTemp = 1;
        }
        this.totalPage = totalPageTemp;

        // 设置当前页数
        // 总页数小于当前页数,应将当前页数设置为总页数
        if(this.totalPage < this.currentPage) {
            this.currentPage = this.totalPage;
        }
        // 当前页数小于1设置为1
        if(this.currentPage < 1) {
            this.currentPage = 1;
        }

    }

    public int getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(int totalNumber) {
        this.totalNumber = totalNumber;
        this.count();
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
        this.count();
    }
}

DAO层实现分页的两个方法

 /**
     *  select  <tt>MbChannelExtensionDO</tt> object by page to DB table <tt>MB_CHANNEL_EXTENSION</tt>, return primary key
     *
     *  <p>
     *  The sql statement for this operation is <br>
     *  <tt>select * from MB_CHANNEL_EXTENSION where id not in(select id from MB_CHANNEL_EXTENSION
     *  where rownum<=(PAGESIZE*(CURRENTPAGE-1))) and rownum<=PAGESIZE order by id; </tt>
     *
     *	@param page,mbChannelExtension
     *	@return List<MbChannelExtensionDO>
     *	@throws DataAccessException
     */
    @Override
    public List<MbChannelExtensionDO> getMbChannelExtensionByPage(MbChannelExtensionDO mbChannelExtensionDO,PageDO page) throws DataAccessException {
        if (page == null) {
            throw new IllegalArgumentException("Can't select by a null page.");
        }
        Map map=new HashMap();
        map.put("page",page);
        map.put("mbChannelExtension",mbChannelExtensionDO);
        return getSqlMapClientTemplate().queryForList("MS-MB-CHANNEL-EXTENSION-SELECT-BY-PAGE", map);
    }

    @Override
    public int countOfMbChannelExtension(MbChannelExtensionDO mbChannelExtensionDO ) throws DataAccessException {

        int count=(Integer)getSqlMapClientTemplate().queryForObject("MS-MB-CHANNEL-EXTENSION-COUNT",mbChannelExtensionDO);
        return count;
    }


以下是mybatis的mapping文件 
</pre><pre name="code" class="html" style="font-size: 11.8181819915771px;"><select id="MS-MB-CHANNEL-EXTENSION-SELECT-BY-PAGE" parameterClass="java.util.Map" resultMap="RM-MB-CHANNEL-EXTENSION">
        select
            *
        from
            MB_CHANNEL_EXTENSION
        where
            id
        not in
        (
            select
                id
            from
                MB_CHANNEL_EXTENSION
            where 1=1

                AND
                rownum
                  <![CDATA[ <=]]>
                (
                    #page.pageNumber#*(#page.currentPage#-1)
                )
        )
        <dynamic>
            <isNotEmpty prepend="AND" property="mbChannelExtension.bankName">
                BANK_NAME=#mbChannelExtension.bankName#
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="mbChannelExtension.productType">
                PRODUCT_TYPE=#mbChannelExtension.productType#
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="mbChannelExtension.exchangeType">
                EXCHANGE_TYPE=#mbChannelExtension.exchangeType#
            </isNotEmpty>
        </dynamic>
        and rownum
        <![CDATA[ <=]]> #page.pageNumber#
        order by
            id
    </select>


    <select id="MS-MB-CHANNEL-EXTENSION-COUNT" resultClass="java.lang.Integer">
        select count(ID)
        from
        MB_CHANNEL_EXTENSION
        where 1=1
        <dynamic>
            <isNotEmpty prepend="AND" property="bankName">
                BANK_NAME=#bankName#
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="productType">
                PRODUCT_TYPE=#productType#
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="exchangeType">
                EXCHANGE_TYPE=#exchangeType#
            </isNotEmpty>
        </dynamic>
<p>    </select></p><div>
</div>

以下是controller层

  @RequestMapping("/channlExtension")
    public String selectCustomer(MbChannelExtensionDO customer,PageDO page,
                                 HttpServletRequest request) {
        if(page==null){
            page=new PageDO();
            page.setPageNumber(1);
            page.count();
        }
        MbChannelExtensionDO channelExtension = new MbChannelExtensionDO();
        channelExtension = customer;
        page.setTotalNumber(channelExtensionService.countOfMbChannelExtension(customer));
        this.tcustomerList  = this.channelExtensionService.getMbChannelExtensionByPage(customer,page);
        request.setAttribute("tcustomerList", tcustomerList);
        request.setAttribute("page",page);
        request.setAttribute("channelExtension",channelExtension);
        return "/channlExtension/channlExtension";
    }

前端html和js
<form name ="frm" id="mainForm" method="post" action="<%=request.getContextPath()%>/channlExtension.html">
            <div class='page fix' style="text-align: end;margin:5px 30px 20px 30px;">
                共 <b>${page.totalNumber}</b> 条
                <c:if test="${page.currentPage != 1}">
                    <a href="javascript:changeCurrentPage('1')" class='first'>首页</a>
                    <a href="javascript:changeCurrentPage('${page.currentPage-1}')" class='pre'>上一页</a>
                </c:if>
                当前第<span>${page.currentPage}/${page.totalPage}</span>页
                <c:if test="${page.currentPage != page.totalPage}">
                    <a href="javascript:changeCurrentPage('${page.currentPage+1}')" class='next'>下一页</a>
                    <a href="javascript:changeCurrentPage('${page.totalPage}')" class='last'>末页</a>
                </c:if>
                跳至 <input id="currentPageText" name="currentPage" type='text' value='${page.currentPage}' class='allInput w28' /> 页 
                <a href="javascript:changeCurrentPage($('#currentPageText').val())" class='go'>GO</a>
            </div>
</form>

<script type="text/javascript">
    function changeCurrentPage(currentPage) {
        $("#currentPageText").val(currentPage);
        $("#mainForm").submit();
    }
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值