<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 4,  comments - 1,  trackbacks - 0
    /**********************************Page類*********************************************/
    package com.nyhr.struts.page;

    /**
     * 分頁(yè)實(shí)體類,保存當(dāng)前分頁(yè)狀態(tài)變量
     * @author Yeno.hhr
    */
    public class Page {

        /** imply if the page has previous page */
        private boolean hasPrePage;
      
        /** imply if the page has next page */
        private boolean hasNextPage;
          
        /** the number of every page */
        private int everyPage;
      
        /** the total page number */
        private int totalPage;
       
        /** the total record number */
        private int totalRecords;
          
        /** the number of current page */
        private int currentPage;
      
        /** the begin index of the records by the current query */
        private int beginIndex;
      
      
        /** The default constructor */
        public Page(){
          
        }
      
        /** construct the page by everyPage
         * @param everyPage
         * */
        public Page(int everyPage){
            this.everyPage = everyPage;
        }
      
        /** The whole constructor */
        public Page(boolean hasPrePage, boolean hasNextPage,
                        int everyPage, int totalPage, int totalRecords,
                        int currentPage, int beginIndex) {
            this.hasPrePage = hasPrePage;
            this.hasNextPage = hasNextPage;
            this.everyPage = everyPage;
            this.totalPage = totalPage;
            this.totalRecords = totalRecords;
            this.currentPage = currentPage;
            this.beginIndex = beginIndex;
        }

        /**
         * @return
         * Returns the beginIndex.
         */
        public int getBeginIndex() {
            return beginIndex;
        }
      
        /**
         * @param beginIndex
         * The beginIndex to set.
         */
        public void setBeginIndex(int beginIndex) {
            this.beginIndex = beginIndex;
        }
      
        /**
         * @return
         * Returns the currentPage.
         */
        public int getCurrentPage() {
            return currentPage;
        }
      
        /**
         * @param currentPage
         * The currentPage to set.
         */
        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }
      
        /**
         * @return
         * Returns the everyPage.
         */
        public int getEveryPage() {
            return everyPage;
        }
      
        /**
         * @param everyPage
         * The everyPage to set.
         */
        public void setEveryPage(int everyPage) {
            this.everyPage = everyPage;
        }
      
        /**
         * @return
         * Returns the hasNextPage.
         */
        public boolean getHasNextPage() {
            return hasNextPage;
        }
      
        /**
         * @param hasNextPage
         * The hasNextPage to set.
         */
        public void setHasNextPage(boolean hasNextPage) {
            this.hasNextPage = hasNextPage;
        }
      
        /**
         * @return
         * Returns the hasPrePage.
         */
        public boolean getHasPrePage() {
            return hasPrePage;
        }
      
        /**
         * @param hasPrePage
         * The hasPrePage to set.
         */
        public void setHasPrePage(boolean hasPrePage) {
            this.hasPrePage = hasPrePage;
        }
      
        /**
         * @return Returns the totalPage.
         *
         */
        public int getTotalPage() {
            return totalPage;
        }
      
        /**
         * @param totalPage
         * The totalPage to set.
         */
        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }
       
        /**
         * @param totalRecords
         * The totalRecords to set.
         */
        public void settotalRecords(int totalRecords)
        {
            this.totalRecords = totalRecords;
        }
        /**
         * @return Returns the totalRecords.
         *
         */
        public int getTotalRecords()
        {
            return this.totalRecords;
        }
    }

    /**********************************PageUtil類*********************************************/
    package com.nyhr.struts.page;

    /**
     * 分頁(yè)工具類,初始化Page對(duì)象
     * @author Yeno.hhr
    */
    public class PageUtil {
         
        /**
         * Use the origin page to create a new page
         * @param page
         * @param totalRecords
         * @return
         */
        public static Page createPage(Page page, int totalRecords){
            return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
        }
      
        /**
         * the basic page utils not including exception handler
         * @param everyPage
         * @param currentPage
         * @param totalRecords
         * @return page
         */
        public static Page createPage(int everyPage, int currentPage, int totalRecords){
            everyPage = getEveryPage(everyPage);
            currentPage = getCurrentPage(currentPage);
            int beginIndex = getBeginIndex(everyPage, currentPage);
            int totalPage = getTotalPage(everyPage, totalRecords);
            boolean hasNextPage = hasNextPage(currentPage, totalPage);
            boolean hasPrePage = hasPrePage(currentPage);
          
            return new Page(hasPrePage, hasNextPage,
                                    everyPage, totalPage, totalRecords,
                                    currentPage, beginIndex);
        }
      
        private static int getEveryPage(int everyPage){
            return everyPage == 0 ? 10 : everyPage;
        }
      
        private static int getCurrentPage(int currentPage){
            return currentPage == 0 ? 1 : currentPage;
        }
      
        private static int getBeginIndex(int everyPage, int currentPage){
            return (currentPage - 1) * everyPage;
        }
          
        private static int getTotalPage(int everyPage, int totalRecords){
            int totalPage = 0;
                  
            if(totalRecords % everyPage == 0)
                totalPage = totalRecords / everyPage;
            else
                totalPage = totalRecords / everyPage + 1 ;
                  
            return totalPage;
        }
      
        private static boolean hasPrePage(int currentPage){
            return currentPage == 1 ? false : true;
        }
      
        private static boolean hasNextPage(int currentPage, int totalPage){
            return currentPage == totalPage || totalPage == 0 ? false : true;
        }
      

    }

    /**********************************Result類*********************************************/
    package com.nyhr.struts.page;

    import java.util.List;
    /**
     * <p>Title: 檢索結(jié)果集實(shí)體類</p>
     * <p>Description: 保存分頁(yè)參數(shù)及數(shù)據(jù)庫(kù)查詢的結(jié)果,用于頁(yè)面顯示</p>
     * <p>Copyright: Copyright (c) 2006</p>
     * <p>Company: 四方人才網(wǎng)</p>
     * @author Yeno.hhr
     * @version 1.0
     */
    public class Result {
        /**分頁(yè)狀態(tài)變量實(shí)體*/
        private Page page;
        /**數(shù)據(jù)庫(kù)檢索到的當(dāng)前頁(yè)結(jié)果集*/
        private List content;

        /**
         * The default constructor
         */
        public Result() {
            super();
        }

        /**
         * The constructor using fields
         *
         * @param page
         * @param content
         */
        public Result(Page page, List content) {

            this.page = page;
            this.content = content;
        }

        /**
         * @return Returns the content.
         */
        public List getContent() {
            return content;
        }

        /**
         * @return Returns the page.
         */
        public Page getPage() {
            return page;
        }

        /**
         * The content to set.
         * @param content
         */
        public void setContent(List content) {
            this.content = content;
        }

        /**
         * The page to set.
         * @param page
         */
        public void setPage(Page page) {
            this.page = page;
        }
    }
     現(xiàn)在展示在大家面前的是查詢分頁(yè)兩個(gè)核心類 :AbstractSearch(預(yù)查詢初始化程序)、AbstractList(分頁(yè)及初始化分頁(yè)程序),代碼如下:
    /********************************AbstractSearch類************************************/
    package com.nyhr.struts.frame;

    import java.util.List;

    import com.nyhr.struts.beans.HibernateUtil;
    /**
     * <p>Title: 預(yù)查詢初始化程序</p>
     * <p>Description: 所有的初始化查詢必須繼承此類,本類只負(fù)責(zé)預(yù)查詢ID集和Page對(duì)象的初始化,不實(shí)現(xiàn)顯示邏輯</p>
     * <p>Copyright: Copyright (c) 2006</p>
     * <p>Company: 四方人才網(wǎng)</p>
     * @author Yeno.hhr
     * @version 1.0
     */
    public abstract class AbstractSearch {

        public AbstractSearch()
        {
            super();
        }
        /**
         * 根據(jù)HQL查詢出記錄的主鍵ID(主索引)集合
         * 注:此hql必須是只檢索主鍵及復(fù)合主鍵的查詢語(yǔ)句,具體見應(yīng)用實(shí)例
         * @param hql 不帶查詢的查詢語(yǔ)句
         * @return idArray 主索引集合(可以主鍵ID,也可以是復(fù)合ID)
         */
        public Object[] getIDList(String hql)
        {
            List list = HibernateUtil.query(hql);
            if (list==null || list.size()==0)
                return null;
            return list.toArray();
        }
        /**
         * 根據(jù)HQL查詢出記錄的主鍵ID(主索引)集合
         * 注:此hql必須是只檢索主鍵及復(fù)合主鍵的查詢語(yǔ)句,具體見應(yīng)用實(shí)例
         * @param hql 帶參數(shù)的查詢語(yǔ)句
         * @param bean 參數(shù)設(shè)置實(shí)體類
         * @return Object[] 主索引集合(可以主鍵ID,也可以是復(fù)合ID)
         */
        public Object[] getIDList(String hql, Object bean)
        {
            List list = HibernateUtil.query(hql,bean);
            if (list==null || list.size()==0)
                return null;
            return list.toArray();
        }
        /**子類方法:根據(jù)子類的需要選擇調(diào)用AbstractSearch的“帶參”和“不帶參”兩種查詢中的一種返回主鍵ID的數(shù)組集*/
        abstract public Object[] getList();
        /**子類方法:設(shè)定查詢條件*/
        abstract protected void condition();
    }


    /********************************AbstractList類************************************/
    package com.nyhr.struts.frame;

    import com.nyhr.struts.page.*;
    import com.nyhr.struts.constant.SysConstant;
    import com.nyhr.struts.hibernate.HibernateSessionFactory;

    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;

    import java.util.List;
    import java.util.ArrayList;
    /**
     * <p>Title: 分頁(yè)及初始化分頁(yè)程序</p>
     * <p>Description: 所有的初始化分頁(yè)必須繼承此類,如果是預(yù)查詢調(diào)用,同時(shí)會(huì)初始化Page實(shí)體,否則Page實(shí)體會(huì)由FormBean提交生成</p>
     * <p>Copyright: Copyright (c) 2006</p>
     * <p>Company: 四方人才網(wǎng)</p>
     * @author Yeno.hhr
     * @version 1.0
     */
    abstract public class AbstractList {
        private Page page;
        /**查詢結(jié)果的主鍵ID集*/
        private Object[] idList;
       
        public AbstractList(){}
        /**
         * 預(yù)查詢初始化分頁(yè)構(gòu)造(初次查詢)
         * @param hql 查詢語(yǔ)句
         * @param search 預(yù)查詢類
         */
        public AbstractList(AbstractSearch search){
            this.idList=search.getList();
        }
        /**
         * 查詢分頁(yè)構(gòu)造(分頁(yè)查詢)
         * @param page 分頁(yè)狀態(tài)實(shí)體
         * @param idList
         */
        public AbstractList(Page page, Object[] idList){
            this.idList=idList;
            this.page=page;
        }
        /**子類方法:設(shè)置分頁(yè)查詢的查詢語(yǔ)句*/
        abstract protected String getHql();
        /**
         * 返回查詢結(jié)果
         * @return Result
         */
        public Result getResult(){
            if(page==null){
                if(idList==null)
                    this.page = PageUtil.createPage(SysConstant.PAGE_SIZE,1,0);
                else
                    this.page = PageUtil.createPage(SysConstant.PAGE_SIZE,1,idList.length);
            }
            return new Result(page, getListByPage());
        }
        /**
         * 分頁(yè)查詢,返回當(dāng)前頁(yè)的查詢結(jié)果集
         * @param hql
         * @return list 結(jié)果集
         */
        public List getListByPage(){
            List list = null;
            if (page.getTotalPage() < 1)
                return list;
            try{
                String hql=getHql();
                if(hql==null || hql.equals(""))
                    return list;
                Object[] bean=getCurrentIds();
                if(bean!=null)
                    list=HibernateUtil.query(hql,bean);
                else
                    list=HibernateUtil.query(hql);
            }catch(Exception e){
                System.out.println(e.getMessage());
                throw new RuntimeException(e);
            }
            return list;
        }

        /**
         * 從查詢結(jié)果的ID集中找出當(dāng)前頁(yè)的ID集
         * @param arr 所有查詢結(jié)果的主鍵ID集
         * @return Object[]
         */
        private Object[] getCurrentIds(){
            if(idList==null)    return null;

            ArrayList<Object> list = new ArrayList<Object>();
            int begin = page.getBeginIndex();
            int ends = page.getTotalRecords();
            int end = begin+page.getEveryPage();
            if (end >= ends)
                end = ends;
            for (int l=begin;l<end;l++){
                list.add(idList[l]);
            }       
            return list.toArray();
        }
        /**
         * 返回查詢結(jié)果主鍵ID集的字符串組合形式
         * @return String
         */
        public String getIdList(){
            String ids="";
            if(idList == null)
                return ids;
            for(int x=0; x<idList.length; x++){
                ids+=idList[x].toString();
                if(x<idList.length-1)
                    ids+=",";
            }
            return ids;
        }
    }


    /********************************HibernateUtil類************************************/
    public class HibernateUtil {

        private HibernateUtil(){}  
        /**
         * 執(zhí)行數(shù)據(jù)庫(kù)查詢,返回結(jié)果集List
         * @param hsql HSQL查詢語(yǔ)句
         * @return list 結(jié)果集
         */
        public static List query(String hql) {
            List list = null;
            Query query = null;     
            Session sess = HibernateSessionFactory.currentSession();
            try{
                //創(chuàng)建一條HQL查詢語(yǔ)句
                if (hql.toLowerCase().contains("from "))
                    query = sess.createQuery(hql);
                else
                     query = sess.getNamedQuery(hql);
               
                list = query.list();
               
            }catch(HibernateException e)  {
                System.out.println("Hibernate Exception:@"+e.getMessage());
                throw new RuntimeException(e);
            }finally {
                HibernateSessionFactory.closeSession();
            }
            return list;
        }
        public static List query(String hql, Object bean){
            List list = null;
            Query query = null;
           
            Session sess = HibernateSessionFactory.currentSession();
            try {
                //創(chuàng)建一條HQL查詢語(yǔ)句
                if (hql.toLowerCase().contains("from "))
                    query = sess.createQuery(hql);
                else
                     query = sess.getNamedQuery(hql);
               
                query.setProperties(bean);
                list = query.list();
            }catch(HibernateException e) {
                System.out.println("Hibernate Exception:@"+e.getMessage());
                throw new RuntimeException(e);
            } finally {
                HibernateSessionFactory.closeSession();
            }
            return list;
        }
    }

    posted on 2007-09-18 08:56 chaochao 閱讀(917) 評(píng)論(1)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 在线a免费观看最新网站| 亚洲日本在线免费观看| 国产免费一区二区三区| 男人的天堂亚洲一区二区三区| 一本到卡二卡三卡免费高| 免费在线中文日本| 免费观看一级毛片| 好看的亚洲黄色经典| 亚洲欧洲免费无码| 国产精品视频白浆免费视频| 成在线人永久免费视频播放| 亚洲国产高清在线| 91嫩草私人成人亚洲影院| 国产成人精品日本亚洲语音| 人妻仑乱A级毛片免费看| 99视频全部免费精品全部四虎| 久久亚洲国产精品123区| 久久亚洲精品专区蓝色区| a毛片免费全部在线播放**| 精品97国产免费人成视频| 成人午夜性A级毛片免费| 亚洲va在线va天堂va不卡下载| 国产精品亚洲一区二区无码| 日本免费一区二区在线观看| 在线亚洲精品自拍| 欧洲亚洲综合一区二区三区| 国产又大又粗又长免费视频| 亚洲va久久久噜噜噜久久狠狠 | 精品视频免费在线| 国产妇乱子伦视频免费| 国产亚洲精品自在久久| 青青免费在线视频| 国产一区二区免费| 一本久到久久亚洲综合| 国产亚洲精aa在线看| 色www免费视频| 女人张开腿等男人桶免费视频| 久久亚洲中文字幕精品有坂深雪| 2022免费国产精品福利在线| 在线看片无码永久免费aⅴ| 亚洲五月综合缴情婷婷|