通用Hibernate DAO类(包括分页)

package com.jronline.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

//import com.jkjshoponline.entity.OrderOfMembercenterDTO;

public abstract class CommonDAOImpl extends HibernateDaoSupport{

    //执行SQL原生查询
public List findHqlForSql(final String hql) {
        
        List  list = getHibernateTemplate().executeFind(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Query query = session.createSQLQuery(hql);
                         return  query.list();
                    }
                });
        return list;
    }
 
 
    
     /**
     * 分页信息
     * 
     * @param hql
     * @param first
     * @param max
     * @return List
     */
    public List findListForPage(final String hql, final int pageId, final int pageSize) {
        List list = getHibernateTemplate().executeFind(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Query query = session.createQuery(hql);
                        int comp=(pageId-1)*pageSize;
                        query.setFirstResult(comp);// 第一行
                        query.setMaxResults(pageSize);// 最大行
                        List list = query.list();
                        return list;
                    }
                });
        return list;  
    }
    
    /**
     * sql by qbzuo 2012-10-15
     * 分页信息
     * 
     * @param sql
     * @param first
     * @param max
     * @return List
     */
    public List findSQLListForPage(final String sql, final int pageId, final int pageSize) {
        List list = getHibernateTemplate().executeFind(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        SQLQuery query = session.createSQLQuery(sql);
                        int comp=(pageId-1)*pageSize;
                        query.setFirstResult(comp);// 第一行
                        query.setMaxResults(pageSize);// 最大行
                        List list = query.list();
                        return list;
                    }
                });
        return list;  
    }
     
    /**
     * 查询所有的记录数
     * 
     * @author qbzuo
     * @param totalHql
     * @return int
     */
    public int findTotalRow(String totalHql) {
        int totalRow = 0;
        List list = getHibernateTemplate().find(totalHql);
        if (list.size() > 0&&list.get(0)!=null) {
        //    System.out.println("list.get(0).toString()="+list.get(0).toString());
            totalRow = Integer.parseInt(list.get(0).toString());
        }
        return totalRow;
    }
    
    public Double findTotal(String totalHql){
        double total=0;
        List list = getHibernateTemplate().find(totalHql);
        if(list.size()>0 && list.get(0)!=null){
            total = Double.parseDouble(list.get(0).toString());
        }
        
        return total;
    }
        
    /**
     * 执行HQL
     * 
     * @author qbzuo
     * @param totalHql
     * @return int
     */
    public List findHql(String hql) {
    
        return getHibernateTemplate().find(hql);
    }
    
    /**
     * 执行SQL
     * 
     * @author qbzuo
     * @param totalHql
     * @return int
     */
    public List findSql(final String sql) {
    
        List list = getHibernateTemplate().executeFind(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        SQLQuery query = session.createSQLQuery(sql);
                        List list = query.list();
                        return list;
                    }
                });
        return list;  
    }
    //
    public List findSqlLimit(final String sql,final int limit){
        
        List list = getHibernateTemplate().executeFind(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        SQLQuery query = session.createSQLQuery(sql);
                        query.setFirstResult(0);// 第一行
                        query.setMaxResults(limit);// 最大行
                        List list = query.list();
                        
                        return list;
                    }
                });
        return list;  
    }

    /**
     * 查找一个表的前几条记录,如前3条
     * @param hql
     * @param limit 
     * @return
     */
    public List findHqlLimit(final String hql,final int limit) {
        
        List  list = getHibernateTemplate().executeFind(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Query query = session.createQuery(hql);
                     
                        query.setFirstResult(0);// 第一行
                        query.setMaxResults(limit);// 最大行
                        return     query.list();
                         
                    }
                });
        return list;
    }
 
    /**内部
     * 执行多条更新
     * @param hql
     * by qbzuo
     */
    public int updateSqlList(final String sql) {
        
        Session session=getHibernateTemplate().getSessionFactory().openSession();
        java.sql.Connection con=session.connection();
        Statement statement;
    try {
        Transaction transaction=    session.beginTransaction();
        transaction.begin();
        statement = con.createStatement();
        int row=statement.executeUpdate(sql);
        transaction.commit();
        con.close();
        return row;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -1;
    }    
    
   } 
    
    /**内部 qbzuo  2013-1-25
     * 执行多条更新
     * @param hql
     * by qbzuo
     */
    public int updateSql(final String sql) {
        
        Object result = getHibernateTemplate().execute(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        SQLQuery query = session.createSQLQuery(sql);
                     
                        int result = query.executeUpdate();
                        return     new Integer(result);
                         
                    }
                });
        return    result!=null?(Integer)result:0;
    
   } 
    
    /**内部 qbzuo  2013-1-25
     * 执行多条更新
     * @param hql
     * by qbzuo
     */
    public int updateHql(final String hql) {
        
        Object result = getHibernateTemplate().execute(new HibernateCallback()
        // 用来回调的匿名内部类
                {
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        Query query = session.createQuery(hql);
                     
                        int result = query.executeUpdate();
                        return     new Integer(result);
                         
                    }
                });
        return    result!=null?(Integer)result:0;
    
   }     
    
    //抽象方法得到dao
    //public abstract HibernateDaoSupport getDao();
    
    
}

 

通用Hibernate DAO类(包括分页),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。