当前位置:编程学习 > JS >>

HIbernate+struts2分页案例

[html]  
<pre name="code" class="html">package www.csdn.project.util;  
  
import org.hibernate.Session;  
  
  
/**  
 * Data access interface for domain model  
 * @author MyEclipse Persistence Tools  
 */  
public interface IBaseHibernateDAO {  
    public Session getSession();  
}</pre><br>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="myeclipse.connection.profile">localhost</property><property  
 name="connection.url">jdbc:mysql://localhost:3306/bookstore</property><property name="connection.username">root</property><property name="connection.password">root</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property  
 name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><mapping resource="www/csdn/project/domain/Admin.hbm.xml" /></session-factory></hibernate-configuration>  
<pre></pre>  
<p><br>  
</p>  
<p><br>  
</p>  
<p></p>  
<pre name="code" class="java">package www.csdn.project.dao;  
  
import www.csdn.project.domain.Admin;  
  
public interface AdminDAO extends BaseDAO<Admin, Integer> {  
  
}</pre><br>  
<pre name="code" class="java">package www.csdn.project.dao;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import org.hibernate.Query;  
  
import www.csdn.project.domain.Admin;  
import www.csdn.project.util.BaseHibernateDAO;  
import www.csdn.project.util.HiberSessionFactory;  
  
public class AdminDAOImpl extends BaseHibernateDAO implements AdminDAO {  
  
    public Integer getCountRecord() {  
        int i = 0;  
        try {  
            Query query = this.getSession().createQuery(  
                    "select count(admin) from Admin admin");  
            /*  
             * Long l = (Long) query.uniqueResult(); int i =  
             * Integer.parseInt(l.toString());  
             */  
  
            i = Integer.parseInt(query.uniqueResult().toString());  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            HiberSessionFactory.closeSession();  
        }  
        return i;  
    }  
      
      
  
  
  
  
    public List<Admin> getNowPageInfo(Integer nowpage) {  
  
        List<Admin> entities = new ArrayList<Admin>();  
        try {  
  
            entities = this.getSession().createQuery("from Admin")  
                    .setFirstResult((nowpage - 1) * PAGESIZE).setMaxResults(  
                            PAGESIZE).list();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            HiberSessionFactory.closeSession();  
        }  
  
        return entities;  
    }  
  
      
    /*public Integer getCountRecord(Class className){  
        int i=0;  
          
        try {  
             i = this.getSession().createCriteria(className).list().size();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            HiberSessionFactory.closeSession();  
        }  
          
        return 0;  
    }*/  
      
    public Integer getCountRecord(Class<Admin> className) {  
        int i = 0;  
        try {  
            Query query = this.getSession().createQuery(  
                    "select count(c) from "+className.getName()+" c");  
            /*  
             * Long l = (Long) query.uniqueResult(); int i =  
             * Integer.parseInt(l.toString());  
             */  
  
            i = Integer.parseInt(query.uniqueResult().toString());  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            HiberSessionFactory.closeSession();  
        }  
        return i;  
    }  
     &nb
补充:Web开发 , Jsp ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,