hibernate 分页

package net.dao;
/**
 * 封装显示房屋信息的参数
 * @author hp
 *
 */
public class houseParm {
String title;
Double price1;
Double price2;
Double floorage1;
Double floorage2;
Integer typeid;
Integer streetid;
@Override
public String toString() {
 return "houseParm [title=" + title + ", price=" + price1 + ", price2="
   + price2 + ", floorage=" + floorage1 + ", floorage2=" + floorage2
   + ", typeid=" + typeid + ", streetid=" + streetid + "]";
}
public String getTitle() {
 return title;
}
public void setTitle(String title) {
 this.title = title;
}
public Double getPrice1() {
 return price1;
}
public void setPrice1(Double price1) {
 this.price1 = price1;
}
public Double getPrice2() {
 return price2;
}
public void setPrice2(Double price2) {
 this.price2 = price2;
}
public Double getFloorage1() {
 return floorage1;
}
public void setFloorage1(Double floorage1) {
 this.floorage1 = floorage1;
}
public Double getFloorage2() {
 return floorage2;
}
public void setFloorage2(Double floorage2) {
 this.floorage2 = floorage2;
}
public Integer getTypeid() {
 return typeid;
}
public void setTypeid(Integer typeid) {
 this.typeid = typeid;
}
public Integer getStreetid() {
 return streetid;
}
public void setStreetid(Integer streetid) {
 this.streetid = streetid;
}

 

}

//实现类:

package net.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import net.dao.BaseDao;
import net.dao.houseDao;
import net.dao.houseParm;
import net.entity.House;
import net.entity.Users;

public class houseImpl extends BaseDao implements houseDao {

 public List<House> findHouse(houseParm params, int page, int pagesize) {
  // TODO Auto-generated method stub
  // String sql="from House h where"+
  // " title like :title"+
  // "and h.price between :price and :price2"+
  // "and h.floorage between :floorage and floorage2"+
  // "and h.types.id=:typeid"+
  // "and h.street.id=:streetid";
  String sql = "from House h left join fetch h.users "
                                 + "left join fetch h.street s "
                                 + "left join fetch s.district where"
                                 + "( h.title like :title or :title is null)"
                                + " and (h.price >= :price1 or :price1 is null)"
                                  + " and (h.price <= :price2 or :price2 is null)"
                                 + " and (h.floorage >=:floorage1 or :floorage1 is null)"
    + " and (h.floorage <=:floorage2 or :floorage2 is null)"
    + " and (h.types.id=:typeid or :typeid is null)"
    + " and (h.street.id=:streetid or :streetid is null)";
  Session s = super.getsession();
  try {

   Query query = s.createQuery(sql);
   query.setProperties(params);
   if (page > 0 && pagesize > 0) {
    query.setFirstResult((page - 1) * pagesize);
    query.setMaxResults((pagesize));
   }
   return query.list();
  } catch (HibernateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   s.close();
  }

  return null;
 }

 public long getcount(houseParm params) {
  // TODO Auto-generated method stub
  Session s = super.getsession();
  String sql = "select count(*) from House h where"
    + " (h.title like :title or :title is null)"
    + " and (h.price >= :price1 or :price1 is null)"
    + " and (h.price <= :price2 or :price2 is null)"
    + " and (h.floorage >=:floorage1 or :floorage1 is null)"
    + " and (h.floorage <=:floorage2 or :floorage2 is null)"
    + " and (h.types.id=:typeid or :typeid is null)"
    + " and (h.street.id=:streetid or :streetid is null)";

  try {
   Query query = s.createQuery(sql);
   query.setProperties(params);
   return (Long) query.uniqueResult();
  } catch (HibernateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   s.close();
  }
  return 0;
 }

 /**
  * Criteria
  *
  * @param params
  * @param page
  * @param pagesize
  * @return
  */
 public List<House> findHouses(houseParm params, int page, int pagesize) {
  Session s = super.getsession();
  try {
   Criteria cri = s.createCriteria(House.class);
   cri.createAlias("street", "s", Criteria.LEFT_JOIN);
   cri.createAlias("types", "t", Criteria.LEFT_JOIN);
   cri.createAlias("users", "u", Criteria.LEFT_JOIN);
   cri.createAlias("s.district", "d", Criteria.LEFT_JOIN);//关联街区
   if (params.getTitle() != null) {
    cri.add(Restrictions.like("title", params.getTitle(),
      MatchMode.ANYWHERE));
   }
   if (params.getPrice1() != null) {
    cri.add(Restrictions.ge("price", params.getPrice1()));
   }
   if (params.getPrice2() != null) {
    cri.add(Restrictions.le("price", params.getPrice2()));
   }
   if (params.getFloorage1() != null) {
    cri.add(Restrictions.ge("price", params.getFloorage1()));
   }
   if (params.getFloorage2() != null) {
    cri.add(Restrictions.le("price", params.getFloorage2()));
   }
   // 执行关联查询类 类属性 别名 连接方式
   if (params.getTypeid() != null) {
    cri.add(Restrictions.eq("t.id", params.getTypeid()));
   }
   if (params.getStreetid() != null) {

    cri.add(Restrictions.eq("s.id", params.getStreetid()));
   }
   if (page > 0 && pagesize > 0) {
    cri.setFirstResult((page - 1) * pagesize);
    cri.setMaxResults((pagesize));
   }
   return cri.list();
  } catch (HibernateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();

  } finally {
   s.close();
  }
  return null;
 }

 public void saveHouseDao(House house) {
  // TODO Auto-generated method stub
  Transaction tx = null;
  Session s = super.getsession();
  try {
   tx = s.beginTransaction();
   s.save(house);
   tx.commit();
  } catch (HibernateException e) {
   // TODO Auto-generated catch block
   if (tx != null) {
    tx.rollback();
   }
   e.printStackTrace();

  } finally {
   s.close();
  }
 }
 // String sql="from House   h left join fetch h.users"+
 // "left join fetch h.street s"+
 // "left join fetch s.district where"+
 // "( h.title like :title or :title is null)"+
 // " and (h.price >= :price1 or :price1 is null)"+
 // " and (h.price <= :price2 or :price2 is null)"+
 // " and (h.floorage >=:floorage1 or :floorage1 is null)"+
 // " and (h.floorage >=:floorage2 or :floorage2 is null)"+
 // " and (h.types.id=:typeid or :typeid is null)"+
 // " and (h.street.id=:streetid or :streetid is null)";

 
 
 public House getHouse(int id) {
  // TODO Auto-generated method stub
   Transaction tx=null;
      House house=null;
      Session s=super.getsession();
    
       try {
     tx=s.beginTransaction();
      house = (House)s.get(House.class, id);
      tx.commit();
    } catch (HibernateException e) {
     // TODO Auto-generated catch block
      if(s!=null){
     tx.rollback();
      }
     e.printStackTrace();
    }finally{
     s.close();
  
      }
     return house ;
 }

}


action 类:

package Action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.dao.houseParm;
import net.dao.typeDao;
import net.entity.District;
import net.entity.House;
import net.entity.Street;
import net.entity.Types;
import net.impl.typeImpl;
import net.service.districtSeverDao;
import net.service.houseSever;
import net.service.streetSever;
import net.serviceImpl.districtSeverImpl;
import net.serviceImpl.houseSeverImpl;
import net.serviceImpl.streetSeverImpl;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class houseListAction extends ActionSupport {
 //获得session
private String chose;
 public String getChose() {
 return chose;
}

public void setChose(String chose) {
 this.chose = chose;
}
   HttpServletRequest request=ServletActionContext.getRequest();
 HttpSession session=request.getSession();

 public String execute() throws Exception {
  houseParm hp = (houseParm) request.getSession().getAttribute("parm");
  if (hp == null) {
   hp = new houseParm();
  }
  //获取数据库中下拉框的值  
  houseSever hs = new houseSeverImpl();
  //获取数据库中下拉框的值  
  typeDao t = new typeImpl();
  List<Types> tlist = t.findTyList();
  districtSeverDao d = new districtSeverImpl();
  List<District> dlist =d.getDistrict();
  streetSever s = new streetSeverImpl();
  List<Street>  slist=s.findAll();
request.setAttribute("tlist", tlist);
request.setAttribute("dlist", dlist);
request.setAttribute("slist", slist);
  int currpage = 1;
  int pagesize = 5;
  if (chose != null) {
   currpage =Integer.parseInt(chose);
  }
  List<House> house = hs.findHouse(hp, currpage, pagesize);
  session.setAttribute("houseList", house);
  long total = hs.getcount(hp);
  long pages = (total % pagesize == 0) ? (total / pagesize)
    : (total / pagesize+1);
  
  request.setAttribute("currpage",currpage);
  request.setAttribute("total", total);
  request.setAttribute("pages", pages);

  return "success";
  
 }

}

JSP页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sx" %>
<%@ page
 import="net.serviceImpl.*,net.dao.*,net.service.*,net.entity.*,net.impl.*"%>
 <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'hellow.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   

<sx:head/>
  </head>
 
<%

%>
<body>


<form action="dohouse.jsp" method="post">
 <p>
  区:
  <select name="district">
   <option value="">
    请选择
   </option>
   <c:forEach var="d1" items="${dlist }" >
   <option value=${d1.id }>${d1.name }</option>
   </c:forEach>
  </select>
 </p>
 <p>
  街道:
  <select name="street">
   <option value="">
    请选择
   </option>
   <c:forEach var="s1" items="${slist }" >
   <option value=${s1.id }>${s1.name }</option>
   </c:forEach>
  </select>
 </p>
 <p>
  类型:
  <select name="type">
  <option value="">
    请选择
   </option>
   <c:forEach var="t1" items="${tlist }" >
   <option value=${t1.id }>${t1.name }</option>
   </c:forEach>
  </select>
 </p>
 <p>
  面积:
  <input type="text" name="floorage1">
  -
  <input type="text" name="floorage2">
 </p>
 <p>
  价格:
  <input type="text" name="price1">
  -
  <input type="text" name="price2">
 </p>
 <p>
  标题:
  <input type="text" name="title" />
 </p>
 <input type="submit" value="提交">
</form>
<table>
 <tr>

  <td>
   id
  </td>
  <td>
   title
  </td>
  <td>
   ds
  </td>
  <td>
   price
  </td>
  <td>
   name
  </td>
  <td>
   street
  </td>
  <td>
   type
  </td>
  <td>
   dictrict
  </td>
        <td>Items</td>
 </tr>
 
<c:forEach var="h" items="${sessionScope.houseList }" >
  <tr>
  <td>${h.id }</td>
  <td>${h.title }</td>
  <td>${h.description }</td>
  <td>${h.price }</td>
  <td>${h.users.name }</td>
  <td>${h.street.name }</td>
  <td>${h.types.name }</td>
  <td>${h.street.district.name }</td>
  <td><A href="Items.jsp?id=${h.id }">查看</a></td>
 </tr>
 </c:forEach>
 
 
</table>
<p>总条数:${total }总页数:${pages } 当前第${currpage }页</p>


<c:if test="${currpage >1}">
<a href="page?chose=${currpage - 1}">上一页</a>
</c:if>
<c:if test="${currpage <=1}">
上一页
</c:if>


<c:if test="${currpage < pages}">
<a href="page?chose=${currpage+1}">下一页</a>
</c:if>
<c:if test="${currpage >=pages}">
下一页
</c:if>
<a href="addHouse.jsp">发布</a>
<a href="login.jsp">登录</a>
</body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值