java分页实现

首先是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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-13 02:31:25

java分页实现的相关文章

浅谈Java分页技术

话不多言.我们要实现java分页技术,我们首先就需要定义四个变量,他们是: int  pageSize;//每页显示多少条记录 int pageNow;//希望现实第几页 int pageCount;//一共有多少页 int rowCount;//一共有多少条记录 说明: 1.pageSize是指定的 2.pageNow是用户选择的 3.rowCount是从表中查询得到的 4.pageCount是计算得到的,该计算为: if(rowCount%pageSize==0) { pageCount=r

数据量你造吗-JAVA分页

数据量你造吗-JAVA分页 原创地址:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),需要转载的,保留下! Thanks 学习的心态第一,解行要相应.其实<弟子规>在"余力学文"当中,一开头就强调了这一个重点."不力行,但学文,长浮华,成何人",这个没有侥幸的,只要学了不去做,无形当中就会增长傲慢,自己不知道.-<弟子规> Written In The Font JAVA-Web 基础那块,我自己也

Java 分页工具

[背景] 最近学习Spring MVC,涉及到分页显示的问题,准备自己整理一个分页工具.由于以前使用Strus框架使用 NewPandaKing 的一个PageBean和Page方法,耦合性比较高,于是优化一下. [优点] 耦合低,使用方便. [缺点] 由于耦合低,不查数据库,所以每次使用List的sublist方法,效率降低. 代码如下: 分页工具类:PageUtil.java /** * Java 分页工具类 */ package com.util; import java.util.Has

java分页组件简单设计(贼简单)

代码很简单,关键是思想. 每个业务模块基本都会涉及到分页的应用,不管是普通的分页浏览还是瀑布流形式的浏览.分页可以减轻服务器的压力,可以节俭页面布局.在我们的分页简单组件设计中,主要是需要返回给客户端四个数据:列表对象.当前页码.每页记录最大条数.记录总数. 列表对象:顾名思义,就是展示给用户看的当前页面记录: 当前页码:主要是需要js来高亮: 每页记录最大条数:一般是服务器设定 记录总数:js用来计算总页数使用 设计思想: 1.由于分页是一种可以公共使用的功能,所以可以开发成一个组件,这样可以

java 分页封装类

//后台Page.javapackage com.complaints.util;5import java.util.List;import java.util.Map;public class Page { private String pageBar; private int pageCount;//页总数 private int rowCount;//总行数 private int pagesize = 10;//每页显示的数据记录 private int curPage;//当前页 pr

java 分页代码

java======================== /** * 分页实体类 * @version [版本号, Nov 9, 2011] */public class Page { private int currentPage;//当前页 private int pageSize; //分页大小 private int totalNum; //总条数 private int totalPage; //总页数 private String url; //url地址 //private Obj

java分页的实现(后台工具类和前台jsp页面)

1.首先,新建一个类Page.java 1 public class Page implements Serializable { 2 private static final long serialVersionUID = -3198048449643774660L; 3 private int pageNow = 1; // 当前页数 4 private int pageSize = 10; // 每页显示记录的条数 5 private int totalCount; // 总记录条数 6

java分页功能代码

import java.util.ArrayList; import java.util.List; /** * * @author cheney * * @date Aug 31, 2012 */ public class PageModel<T> { private int page = 1; // 当前页 public int totalPages = 0; // 总页数 private int pageRecorders;// 每页5条数据 private int totalRows

Java分页类 Page

import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Iterator; import java.util.List; import net.sf.json.JSONObject; /** * 分页类 * @author rubekid * * @param <T> */ public class Page<T> extends QueryParameter

Java分页内容实例详解

首先定义一个fruit表,表里含有很多数据: 定义一个数据文件: public class Fruit { public String getIds() { return ids; } public void setIds(String ids) { this.ids = ids; } public String getName() { return name; } public void setName(String name) { this.name = name; } public dou