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

如果需要转载,请注明出处,谢谢!!
原文地址:https://www.cnblogs.com/wlx520/p/4693016.html