Hibernate + jsp + HQL paging query and java_webhql
Paging queries are everywhere. Use Hibernate + jsp + HQL for paging query.
Step 1: Compile the ing between the House entity class and House. hbm. xml.
/** House entity class */public class House {private int id; // House id private HouseType type; // House type private Users2 user; // user private Street street; // Street private String title; // title private String description; // description private String fdate; // Date private String price; // price private String contact; // area // omit the get and set Methods}
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Step 2: Compile hibernate. cfg. xml ing
<! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Step 3: HibernateUtil + fenye. java paging statement
package com.msit.hibernate.HibernateUtil;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil { private HibernateUtil(){ }; public static SessionFactory SessionFactory = null; static{ //hibernate Configuration cf = new Configuration(); cf.configure(); SessionFactory = cf.buildSessionFactory();//DriverManager.getconnection() //Session session = SessionFactory.openSession(); } public static Session getSession(){ return SessionFactory.openSession(); } public static void closeSession(Session session){ if(session!=null){ session.clear(); } }}
Package Dao; import java. util. list; import org. hibernate. query; import org. hibernate. session; import org. hibernate. transaction; import com. msit. hibernate. hibernateUtil. hibernateUtil; import entity. house;/** paging */public class fenye {// query all houses public List <House> selecthouse () {// TODO Auto-generated method stub Session session = HibernateUtil. getSession (); // enable Transaction tran = session. beginTransaction (); String hql = "from House"; Query q = session. createQuery (hql); List <House> list = q. list (); return list;} // total number of houses except for the number of items to be divided public int getTotalPages (int count, int pageSize) {int totalpages = 0; totalpages = (count % pageSize = 0 )? (Count/pageSize) :( count/pageSize + 1); return totalpages;} // get the total number of houses public int getConut () {Session session = HibernateUtil. getSession (); Transaction tran = session. beginTransaction (); String hql = "select count (*) from House"; Query q = session. createQuery (hql); List list = q. list (); String li = list. get (0 ). toString (); Integer count = Integer. parseInt (li); return count;} public List <House> selechouse (int pageIndex, int pageSize) {// TODO Auto-generated method stub Session session = HibernateUtil. getSession (); // enable Transaction tran = session. beginTransaction (); String hql = "from House"; Query query = session. createQuery (hql); query. setFirstResult (pageIndex-1) * pageSize); query. setMaxResults (pageSize); List <House> result = query. list (); return result ;}}
Jsp page:
<% // ======================= Paging ============================/// set the number of news display entries int pageSize = 4; // instantiate fenye newxw = new fenye (); // obtain the number of data records in the database int count = newxw. getConut (); // obtain the page number String page1 = request. getParameter ("pageIndex"); // obtain the page to be split. int pag = newxw. getTotalPages (newxw. getConut (), pageSize); // get the int pageIndex = 0 on the current page; // determine whether the obtained value has a value if (page1 = null) {pageIndex = 1; // query} else {// assign the current page to pageIndex = Integer. parseInt (page1); // determines whether the current page is the largest if (pageIndex> pag) {pageIndex = pag ;}list <House> List = newxw. selechouse (pageIndex, pageSize); request. setAttribute ("list", list); %> <% HouseBiz hou = new HouseBizImpl (); List <House> hoi = hou. selecthouse (); request. setAttribute ("list", list); %>
<C: forEach var = "mind" items = "$ {requestScope. list}">
/
Omitted
Show data/
</C: forEach>
<%
// Determine whether the current page is the last page
If (pageIndex> 1 ){
%>
<LI> <a href = "list. jsp? PageIndex = <%= 1%> "> homepage </a> </LI>
<LI> <a href = "list. jsp? PageIndex = pageIndex-1 %> "> previous </a> </LI>
<%
}
// Determine whether the current page is the last page
If (pageIndex <pag ){
%>
<LI> <a href = "list. jsp? PageIndex = <% = pageIndex + 1%> "> next page </a> </LI>
<LI> <a href = "list. jsp? PageIndex = <% = pag %> "> last page </a> </LI>
<%
}
%>
</UL>
<SPAN
Class = total> [<% = pageIndex %>/<% = pag %>] page </SPAN> </DIV>