用hibernate实现动态查询+分页的模板,另附struts2的标签在分页中的使用

用hibernate实现动态查询+分页的模板,适合于所有的页面

封装页面的信息,进行了泛型。

PageInfo.java
复制代码
 1  package cn.jbit.auction.entity;
 2 
 3 import java.util.List;
 4 /**
 5  * 分页封装类
 6  */
 7 public class PageInfo {//此处进行了泛型,使用与所有的对象
 8     public static final int PAGESIZE = 3;
 9     private Integer count;// 总记录数
10     private List pageList;// 当前页的记录集合
11     private Integer pageIndex;// 当前页号
12     private Integer totalPages;// 总页数
13 
14     public Integer getCount() {
15         return count;
16     }
17 
18     public void setCount(Integer count) {
19         this.count = count;
20     }
21 
22     public List getPageList() {
23         return pageList;
24     }
25 
26     public void setPageList(List pageList) {
27         this.pageList = pageList;
28     }
29 
30     public Integer getPageIndex() {
31         return pageIndex;
32     }
33 
34     public void setPageIndex(Integer pageIndex) {
35         this.pageIndex = pageIndex;
36     }
37 
38     public Integer getTotalPages() {
39         this.totalPages = this.count / this.PAGESIZE;
40         if (this.count % this.PAGESIZE != 0)
41             this.totalPages++;
42         return this.totalPages;
43     }
44 
45 }
复制代码

jsp页面auctionList.jsp

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




无标题文档






class="wrap">
class="sale">

class="lf">在线拍卖系统

if test="${not empty sessionScope.user}">
class="logout right">注销
if> if test="${empty sessionScope.user}">
class="logout right">登录
if>
class="forms"> class="nwinput" id="name"/> class="nwinput"/> class="nwinput"/> class="nwinput" /> class="nwinput" /> class="spbg buttombg f14 sale-buttom"/> if test="${sessionScope.user.userisadmin==true}"> class="spbg buttombg f14 sale-buttom buttomb"/> if>
if test="${sessionScope.user.userisadmin==false}">     查看竞拍结果 if>
class="items">
    class="rows even strong">
  • 名称
  • class="list-wd">描述
  • 开始时间
  • 结束时间
  • 起拍价
  • class="borderno">操作
    class="rows">
  • ${auction.auctionname }
  • class="list-wd">${auction.auctiondesc }
  • ${auction.auctionstarttime }
  • ${auction.auctionendtime }
  • ${auction.auctionstartprice }
  • class="borderno red"> if test="${sessionScope.user.userisadmin==true }"> 修改| 删除 if> if test="${sessionScope.user.userisadmin==false }"> 竞拍 if>
class="page"> 首页 if test="${requestScope.auctionPageInfo.pageIndex!=1}"> 上一页 if> ${pageIndex } if test="${requestScope.auctionPageInfo.pageIndex!=requestScope.auctionPageInfo.totalPages}"> 下一页 if> 尾页
复制代码

Servlet页面

AuctionListServlet
复制代码
package cn.jbit.auction.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jbit.auction.biz.IAuctionBiz;
import cn.jbit.auction.biz.impl.AuctionBizImpl;
import cn.jbit.auction.entity.Auction;
import cn.jbit.auction.entity.PageInfo;
import cn.jbit.auction.util.Tool;

public class AuctionListServlet extends EncodingServlet {

    
    public AuctionListServlet() {
        super();
    }

    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            IAuctionBiz biz = new AuctionBizImpl();
            int pageIndex=1;
            if(request.getParameter("pageIndex")!=null){
                pageIndex=new Integer(request.getParameter("pageIndex")).intValue();
            }
            Auction condition=new Auction();
            if(request.getParameter("auctionName")!=null&&!"".equals(request.getParameter("auctionName"))){
                condition.setAuctionname(request.getParameter("auctionName"));
            }
            if(request.getParameter("auctionDesc")!=null&&!"".equals(request.getParameter("auctionDesc"))){
                condition.setAuctiondesc(request.getParameter("auctionDesc"));
            }
            if(request.getParameter("auctionStartTime")!=null&&!"".equals(request.getParameter("auctionStartTime"))){
                condition.setAuctionstarttime(new java.sql.Timestamp(Tool.strToDate(request.getParameter("auctionStartTime"), "yyyy-MM-dd HH:mm:dd").getTime()));
            }
            if(request.getParameter("auctionEndTime")!=null&&!"".equals(request.getParameter("auctionEndTime"))){
                condition.setAuctionendtime(new java.sql.Timestamp(Tool.strToDate(request.getParameter("auctionEndTime"), "yyyy-MM-dd HH:mm:dd").getTime()));
            }
            if(request.getParameter("auctionStartPrice")!=null&&!"".equals(request.getParameter("auctionStartPrice"))){
                condition.setAuctionstartprice(new Double(request.getParameter("auctionStartPrice")));
            }            
            PageInfo auctionPageinfo = biz.find(condition,pageIndex);
            request.setAttribute("auctionPageInfo", auctionPageinfo);
            request.getRequestDispatcher("auctionList.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            request.setAttribute("message", e.getMessage());
            request.getRequestDispatcher("error.jsp").forward(request,response);
        }
    }

    public void init() throws ServletException {
        // Put your code here
    }

}
复制代码

  dao层的实现方法

复制代码
public PageInfo select(Auction condition, int pageIndex)
            throws Exception {
        PageInfo pageInfo = new PageInfo();
        Session session = HibernateUtil.currentSession();
        DetachedCriteria dc = DetachedCriteria.forClass(Auction.class);
        Criteria c = session.createCriteria(Auction.class);// 用于列表
        Criteria c1 = session.createCriteria(Auction.class);// 用于查询总记录数
        if (condition.getAuctionname() != null
                && !condition.getAuctionname().equals("")) {
            c.add(Restrictions.ilike("auctionname", condition.getAuctionname(),
                    MatchMode.ANYWHERE));
            c1.add(Restrictions.ilike("auctionname",
                    condition.getAuctionname(), MatchMode.ANYWHERE));
        }
        if (condition.getAuctiondesc() != null
                && !"".equals(condition.getAuctiondesc())) {
            c.add(Restrictions.ilike("auctiondesc", condition.getAuctiondesc(),
                    MatchMode.ANYWHERE));
            c1.add(Restrictions.ilike("auctiondesc",
                    condition.getAuctiondesc(), MatchMode.ANYWHERE));
        }
        if (condition.getAuctionstarttime() != null) {
            c.add(Restrictions.ge("auctionstarttime",
                    condition.getAuctionstarttime()));
            c1.add(Restrictions.ge("auctionstarttime",
                    condition.getAuctionstarttime()));
        }
        if (condition.getAuctionendtime() != null) {
            c.add(Restrictions.le("auctionendtime",
                    condition.getAuctionendtime()));
            c1.add(Restrictions.le("auctionendtime",
                    condition.getAuctionendtime()));
        }
        if (condition.getAuctionstartprice() != null) {
            c.add(Restrictions.ge("auctionstartprice",
                    condition.getAuctionstartprice()));
            c1.add(Restrictions.ge("auctionstartprice",
                    condition.getAuctionstartprice()));
        }
        c.addOrder(Order.desc("auctionstarttime"));
        // 总记录数
        int count = (Integer) c1.setProjection(Projections.rowCount())
                .uniqueResult();
        pageInfo.setCount(count);
        // 当前页号
        pageInfo.setPageIndex(pageIndex);
        // 分页
        // 每页显示的记录数
        c.setMaxResults(PageInfo.PAGESIZE);
        c.setFirstResult((pageIndex - 1) * PageInfo.PAGESIZE);
        List list = c.list();
        pageInfo.setPageList(list);
        return pageInfo;
    }
复制代码




dao层接口

PageInfo select(Auction condition,int pageIndex) throws Exception;

biz层的实现方法

public PageInfo find(Auction condition,int pageIndex) throws Exception {
        return this.dao.select(condition, pageIndex);
    }

==========================================


                                              客户类别:
                                              
                                              	
                                              	
                                              联络方式:
                                              
                                              	
                                              	
                                              联络日期:
                                              
                                              
                                              	" class="input" style="width:80px;"/>
                                              	至
                                              
                                               	" class="input" style="width:80px;"/>
                                              	
                                             
                                              	
                                              联络状态:
                                              
                                              	
                                              	

 
         
   当前第页, 共页, 共条记录 首页 上一页 下一页 尾页 跳转到






 
    

你可能感兴趣的:(hibernate,hibernate)