Java_Web三大框架之Hibernate+jsp+HQL分頁查詢


分頁查詢無處不在。使用Hibernate+jsp+HQL進行分頁查詢。

第一步:編寫房屋實體類和House.hbm.xml映射。

/*
 * 房屋實體類
 */
public class House {
    private int id;//房屋id
    
    private HouseType type;//房屋類型
    private Users2 user;//用戶
    private Street street;//街道
    
    private String title;//標題
    private String description;//描述
    private String fdate;//日期
    private String price;//價格
    private String contact;//面積
//省略get和set方法
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
    package="entity">

    <class name="House" table="House">
        <id name="id">
            <generator class="increment"/>
        </id>
        <!--外鍵-->
  <many-to-one name="type" column="type_id" cascade="save-update" />
    <many-to-one name="user" column="user_id" cascade="save-update" />
      <many-to-one name="street" column="street_id" cascade="save-update" />
        <property name="title" />
        <property name="description" />
        <property name="fdate" />
        <property name="price" />
        <property name="contact" />

        
    </class>

</hibernate-mapping>

第二步:編寫hibernate.cfg.xml映射

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory name="foo">
        <!-- 數據庫方言 -->
        <property name="dialect">
            org.hibernate.dialect.OracleDialect
        </property>
        <!-- 連接數據庫Url -->
        <property name="hibernate.connection.url">
            jdbc:oracle:thin:@localhost:1521:orcl
        </property>
        <!-- 連接驅動 -->
        <property name="hibernate.connection.driver_class">
            oracle.jdbc.driver.OracleDriver
        </property>
        <!-- 用戶名 -->
        <property name="hibernate.connection.username">epet</property>
        <!-- 密碼 -->
        <property name="hibernate.connection.password">123456</property>

                <!-- 在控制台打印sql信息 -->
        <property name="show_sql">true</property>
        <!-- 創建表結構 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
    
        <!-- 配置映射信息 -->
    
        <mapping resource="entity/House.hbm.xml" />
        
        
    </session-factory>
</hibernate-configuration>

第三步:HibernateUtil+fenye.java分頁語句

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;

/*
 * 分頁
 */
public class fenye {
    //查詢所有房屋
    public List<House> selecthouse() {
        // TODO Auto-generated method stub
         Session session = HibernateUtil.getSession();
        //開啟事物
         Transaction tran=session.beginTransaction();
         
         String hql="from House";
         Query q=session.createQuery(hql);
         
          List<House> list = q.list();

          return list;
      
    }
    //房屋總數除於要分的條數
    public int getTotalPages(int count,int pageSize){
        int totalpages=0;

       totalpages=(count%pageSize==0)?(count/pageSize):(count/pageSize+1);
        return totalpages;


    }
    //獲取房屋總條數
    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();
         //開啟事物
         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頁面:

 

<%
//==============分頁=============== //設置新聞顯示條數
    int pageSize=4; //實例化
fenye newxw=new fenye(); //獲取數據庫有多少條數據
int count=newxw.getConut(); //獲取頁碼
String page1=request.getParameter("pageIndex"); //得到具體要分的頁
int pag=newxw.getTotalPages(newxw.getConut(),pageSize); //得到當前頁
int pageIndex=0; //判斷得到的值是否有值
if(page1==null){ pageIndex=1; //查詢
 }else{ //把當前頁賦值給pageIndex
    pageIndex=Integer.parseInt(page1); //判斷當前頁是否為最大頁
    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}">

/
省略
顯示數據/

</c:forEach >

<%
//判斷當前頁是否為末頁
if(pageIndex>1){
%>
<LI><a href="list.jsp?pageIndex=<%=1 %>"> 首頁</a></LI>
<LI> <a href="list.jsp?pageIndex=<%=pageIndex-1%>"> 上一頁 </a></LI>

<%
}
//判斷當前頁是否為末頁
if(pageIndex<pag){
%>
<LI> <a href="list.jsp?pageIndex=<%=pageIndex+1 %>"> 下一頁</a></LI>
<LI> <a href="list.jsp?pageIndex=<%=pag%>"> 末頁 </a></LI>

<%
}

%>


</UL>


<SPAN
class=total>[<%=pageIndex %>/<%=pag%>]頁</SPAN> </DIV></DIV>

 

您可以通過點擊 右下角 的按鈕 來對文章內容作出評價, 也可以通過左下方的 關注按鈕 來關注我的博客的最新動態。 

如果文章內容對您有幫助, 不要忘記點擊右下角的 推薦按鈕 來支持一下哦   

如果您對文章內容有任何疑問, 可以通過評論或發郵件的方式聯系我: 2276292708@qq.com

如果需要轉載,請注明出處,謝謝!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM