Java paging implementation, java Paging

Source: Internet
Author: User

Java paging implementation, java Paging

The first is dataobject. The project uses 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;}


Next, we use the pagination Page class.

Public class PageDO {/*** Total number of items */private int totalNumber;/***** current page */private int currentPage; /***** total page number */private int totalPage;/***** number of entries per page */private int pageNumber = 10; /*** calculate and set the relevant attribute value based on the attribute value of the current object */public void count () {// calculate the total number of pages 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; // set the current page number to be smaller than the current page number. Set the current page number to the total page number if (this. totalPage <this. currentPage) {this. currentPage = this. totalPage;} // set the current page number to 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 ();}}

Two paging methods for the DAO Layer

 /**     *  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;    }


The following is the mapping file of mybatis:
</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>

Below is the controller Layer

  @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";    }

Front-end html and 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; "> total <B >$ {page. totalNumber} </B> <c: if test = "$ {page. currentPage! = 1} "> <a href =" javascript: changeCurrentPage ('1') "class = 'first'> homepage </a> <a href =" javascript: changeCurrentPage ('$ {page. currentPage-1} ') "class = 'pre'> previous page </a> </c: if> current <span >$ {page. currentPage}/$ {page. totalPage} </span> page <c: if test = "$ {page. currentPage! = Page. totalPage} "> <a href =" javascript: changeCurrentPage ('$ {page. currentPage + 1} ') "class = 'Next'> next page </a> <a href =" javascript: changeCurrentPage (' $ {page. totalPage} ') "class = 'last'> last page </a> </c: if> jump to <input id = "currentPageText" name = "currentPage" type = 'text' value = '$ {page. currentPage} 'class = 'allinput w28'/> page <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>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.