`
woaiwofengkuang
  • 浏览: 25736 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

电子商务项目的开发手记续(一)

阅读更多

时间过的真快呀,感觉不到的就又要上班了。今天是08年的第一天班在这里给大家拜个晚年。

今天来写写在项目中应用到的分页算法来和大家讨论讨论。

在这里主要实现的是模糊查询分页。

要分页当然要有一个记录页面信息的bean了下面是这写的一个bean

package com.huiyaa.businessweb.util;

public class Page {

    private int nowPageNo = 1;// 当前的页号

    private int totalPage;// 总页数

    private int firstResult = 0;// 查询的起始位置

    private int maxResults = 10;// 最大的结果集,每一页有多少记录

    private int count;// 总记录数

    private int[] pageNo;// 存放所有的页号

    public int getNowPageNo() {
        return nowPageNo;
    }

    public void setNowPageNo(int nowPageNo) {
        this.nowPageNo = nowPageNo;
    }

    public int getTotalPage() {
        return totalPage;
    }

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

    /**
     * 计算一共有多少页,即总的页数
     * @param conut
     * @param maxResults
     */
    public void setTotalPage(int conut, int maxResults) {
        int totalPage = 0;
        if (maxResults % 2 == 0) {
            if (count % 2 == 0) {
                totalPage = count / maxResults;
            } else {
                totalPage = count / maxResults + 1;
            }
            setTotalPage(totalPage);
        }else{
            if (count % 2 == 0) {
                totalPage = count / maxResults - 1;
            } else {
                totalPage = count / maxResults;
            }
            setTotalPage(totalPage);
        }
    }

    public int getFirstResult() {
        return firstResult;
    }

    public void setFirstResult(int firstResult) {
        this.firstResult = firstResult;
    }

    public int getMaxResults() {
        return maxResults;
    }

    public void setMaxResults(int maxResults) {
        this.maxResults = maxResults;
    }

    public int getCount() {
        return count;
    }

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

    public int[] getPageNo() {
        return pageNo;
    }

    public void setPageNo(int[] pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * 要据总的页数来计算出页号,并将所有的页码放在一个int[]里
     * 数组的大小为传入的总页数
     * @param totalPage
     */
    public void setPageNo(int totalPage) {
        int[] pageNoes = new int[totalPage];
        for (int i = 1; i <= totalPage; i++) {
            int j = i;
            pageNoes[i - 1] = j;
        }
        setPageNo(pageNoes);
    }

}
 

要分页当然是dao层的事了,不过在数据量小的情况下也可以直接在页面中分。下面就是我的dao层的分页的实现

public List<Hospital> getHospitalByAddressKey(String hospitalAddressKey,
            int maxResults) {
        log.debug("���根据医院地址关键字搜索相匹配的所有医院Ժ");
        List<Hospital> hospitals = null;

        hospitals = getSession().createCriteria(Hospital.class).add(
                Restrictions.like("hospitalAddress", "%" + hospitalAddressKey
                        + "%")).setFirstResult(
                (page.getNowPageNo() - 1) * maxResults).setMaxResults(
                page.getMaxResults()).list();

        return hospitals;
    }

在这里用的是hibernate中的条件查询。

在此之前当然要先得到所要分页的记录的总数,我将这个些方法放封装到一个类里,当然也是在dao了。也提供相的接口。下面就是这个方法。

package com.huiyaa.businessweb.dao.imp;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.huiyaa.businessweb.dao.PagintionInterface;

public class PagintionInterfaceImpl extends HibernateDaoSupport implements PagintionInterface {

    /**
     * 根据搜索关键字和搜索选项来得到要搜索的内容的总的记录数
     * @param searchKey
     * @param searchOption
     * @return int
     */
    public int getCount(String searchKey, String searchOption) {
        int count = 0;

        if (searchOption.equalsIgnoreCase("byHospitalAddress")) {
            String hql = "from Hospital hospital where hospital.hospitalAddress like :address";
            count = getHibernateTemplate().findByNamedParam(hql, "address",
                    "%" + searchKey + "%").size();
            return count;
        }

        if (searchOption.equalsIgnoreCase("byHospitalName")) {
            String hql = "from Hospital hospital where hospital.hospitalName like :name";
            count = getHibernateTemplate().findByNamedParam(hql, "name",
                    "%" + searchKey + "%").size();
            return count;
        }
        return count;
    }

}

 

这些工作做好之后就是怎样用struts去调用了。

首先在最初调用的action中来对页面bean进行数值初始化。具体如下

page.setCount(yLService.getCount(searchKey, searchOption));
        int count = page.getCount();
        page.setTotalPage(count, page.getMaxResults());
        page.setPageNo(page.getTotalPage());
        int[] pageNoes = page.getPageNo();
        request.getSession().setAttribute("pageNoes", pageNoes);

这一步做好之后我们就可以在结果页面上来显示面号和相应的内容了,如下

<logic:notEmpty name="hospitals" scope="request">
     <p align="center"  class="css">
        <logic:iterate id="pageNo" name="pageNoes" indexId="index"
            scope="session">
            <html:link action="pagintion" paramId="nowPageNo" paramName="pageNo">
                <bean:write name="pageNo"/>
            </html:link>
        </logic:iterate>
    </p>
    </logic:notEmpty>

当我们点击一个页码时,要将当前面码作为一个参数传到一个辅助的action中去。再将当前页码保存到页面bean中,以便于有这来计算FirstResult。这个action如下

public class PagintionAction extends Action {

    private Page page;

    private YLServiceInterface yLService;

    public void setPage(Page page) {
        this.page = page;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        String searchOption = (String) request.getSession().getAttribute(
                "searchOption");
        String searchKey = (String) request.getSession().getAttribute(
                "searchKey");

        int nowPageNo = Integer.valueOf(request.getParameter("nowPageNo"));
        page.setNowPageNo(nowPageNo);
        System.out.println("NOWPAGENO = :" + page.getNowPageNo());

        List<Hospital> hospitals = null;
       
        if (searchOption.equalsIgnoreCase("byHospitalAddress")) {
            hospitals = yLService.getHospitalByAddressKey(searchKey, page
                    .getMaxResults());
            request.setAttribute("hospitals", hospitals);
            return mapping.findForward("searchList");
        }

        if (searchOption.equalsIgnoreCase("byHospitalName")) {
            hospitals = yLService.getHospitalByNameKey(searchKey, page
                    .getMaxResults());
            request.setAttribute("hospitals", hospitals);
            System.out.println("HOSPITAL OK");
            return mapping.findForward("searchList");
        }

        if (searchOption.equalsIgnoreCase("byIllName")) {
            hospitals = yLService.getHospitalByIll(searchKey, page
                    .getMaxResults());
            request.setAttribute("hospitals", hospitals);
            return mapping.findForward("searchList");
        }
        return null;
    }

    public void setYLService(YLServiceInterface service) {
        yLService = service;
    }

}

 

OK,这样一个简单的分页就算完成,如果您还有什么更好,更调高效的算法的话请不吝赐教。

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics