Spring + Hibernate 数据访问

Spring + Hibernate 数据访问

 

一,1个接口:StatDAO.java

package  com.statistic.DAO;

import  java.util.List;
import  org.hibernate.Session;

public   interface  StatDAO {

  
public   void  save(Object obj);
  
public   void  update(Object obj);
  
public   void  delete(Object obj);
  
public  Object get(Class c, Long id);
  
public  List findByPage(String hql,  int  pageSize,  int  pageNO);
  
public  List findList(String hql);
  
public   int  getCount(String hql);
  
public   void  executeDel(String hql);
  
public  Session getSess();
}


二,1个接口实现类:StatDAOImpl.java 
        继承HibernateDaoSupport
        实现StatDAO.java

package  com.statistic.DAO.impl;

import  com.statistic.DAO.StatDAO;
import  java.util. * ;
import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import  org.springframework.orm.hibernate3.HibernateCallback;
import  org.hibernate.Session;
import  org.hibernate.Query;
import  java.sql.Statement;
import  java.sql.SQLException;


public   class  StatDAOImpl  extends  HibernateDaoSupport  implements  StatDAO{
  
public  StatDAOImpl() {
  }
  
public   void  save(Object obj){
    
this .getHibernateTemplate().save(obj);
  }

  
public   void  update(Object obj){
    
this .getHibernateTemplate().update(obj);
  }

  
public   void  delete(Object obj){
    
this .getHibernateTemplate().delete(obj);
  }
  
public  Object get(Class c, Long id){
    
return   this .getHibernateTemplate().get(c,id);
  }

  
public  List findByPage(String hql,  int  pageSize,  int  pageNO){
    
final  String sql  =  hql;
    
final   int  firstRow  =  (pageNO  -   1 *  pageSize;
    
final   int  maxRow  =  pageSize;

    List list 
=   this .getHibernateTemplate().executeFind( new  HibernateCallback() {
      
public  Object doInHibernate(Session session) {
        Query q 
=  session.createQuery(sql);
        q.setFirstResult(firstRow);
        q.setMaxResults(maxRow);
        
return  q.list();
      }
    }
    );
    
return  list;
  }

  
public  List findList(String hql){
    
return   this .getHibernateTemplate().find(hql);
  }

  
public   int  getCount(String hql){
    
int  count  =   0 ;
        List list 
=   this .getHibernateTemplate().find(hql);
        
if ((list  !=   null &&  (list.size()  !=   0 )) {
            count 
=  ((Long) list.get( 0 )).intValue();
        }
        
return  count;

  }

  
/**
   * 执行非查询的SQL语句,这里是删除
   * 
@param  hql String
   
*/
  
public   void  executeDel(String hql) {
    
final  String sql  =  hql;
    
this .getHibernateTemplate().execute( new  HibernateCallback()  {
     
public  Object doInHibernate(Session session) {
       Statement stat 
= null ;
       
try {
         stat 
=  session.connection().createStatement();
         
return   new  Integer(stat.executeUpdate(sql));

       }
catch (Exception e){
         e.printStackTrace();
         
return   null ;
       }
finally {
         
try {
           stat.close();
         }
catch (Exception e){
           e.printStackTrace();
         }
       }

     }
   }
   );
  }

  
public  Session getSess(){
    
return   this .getHibernateTemplate().getSessionFactory().openSession();
   }



}


三,商业逻辑

 

package  com.statistic.business;

import  com.statistic.DAO.StatDAO;
import  java.util.List;
import  java.util.ArrayList;
import  com.statistic.bean.ItemDay;
import  com.bean.Item;

public   class  ItemDayBO {
  
private  StatDAO statDAO;

  
private  com.business.ItemBO miscItemBO;

  
public   void  setMiscItemBO(com.business.ItemBO miscItemBO) {
        
this .miscItemBO  =  miscItemBO;
  }
  
public   void  setStatDAO(StatDAO statDAO){
    
this .statDAO =  statDAO;
  }

  
public  List getItemList(String type,String date1){
    List list 
=   new  ArrayList();
    String hql 
=   " select * from ItemDay as i where i.typeid= " + type +
        
"  and i.downloadDate=' " + date1 + " ' " ;
    list 
=  statDAO.findList(hql);
    
return  list;
  }

  
public  List getTypeCount(String date1,String date2){
    List list 
=   new  ArrayList();
    String hql 
=   " select i.typeid,sum(i.downloadTotal) " ;
    
return  list;
  }

  
public  List[] getAvatatListBySubject(String date1,String str,String orderCol , String orderMode){
    List[] list 
=   new  ArrayList[ 3 ];
    String hql 
= " from ItemDay i  " +
                    
"  where i.typeid=1 and ( " + str +
                    
" ) and i.downloadDate=' " + date1 + " " +
                    
"  order by  " + orderCol  + "   " + orderMode;
    List tempList 
=  statDAO.findList(hql);
    list[
0 ] = new  ArrayList();
    list[
1 ] = new  ArrayList();
    list[
2 ] = new  ArrayList();
    
for ( int  i  = 0 ;i < tempList.size();i ++ ){
      ItemDay itemDay 
=  (ItemDay)tempList.get(i);
      Item item 
=  avatarItemBO.get(itemDay.getProductid());
      
if (item != null ){
        itemDay.setName(item.getName());

        
if (itemDay.getSex() == 1 ){
          list[
0 ].add(itemDay);
        }
else   if (itemDay.getSex() == 2 ){
          list[
1 ].add(itemDay);
        }
else {
          list[
2 ].add(itemDay);
        }
      }

    }

    
return  list;
  }

  
public  List getMiscListBySubject(String date1,String str,String orderCol , String orderMode){
    List list 
=   new  ArrayList();
    String hql 
=   " from ItemDay i where i.downloadDate=' " + date1 + " ' and i.code like ' " + str +   " ' order by  " + orderCol  + "   " + orderMode;
    list 
=  statDAO.findList(hql);
    
for ( int  i  = 0 ;i < list.size();i ++ ){
      ((ItemDay)list.get(i)).setName((miscItemBO.get(((ItemDay)list.get(i)).getProductid()).getName()));
    }
    
return  list;
  }

  
public  String[] getTotalCount(String date1,String type){
    String[] counts 
=   new  String[ 2 ];
    String hql 
=   " select sum(i.downloadTotal),sum(i.clickTotal) from ItemDay i  " +
                 
" where i.downloadDate=' " + date1 + " ' and i.code like ' " + type + " ' " ;
    List list 
=  statDAO.findList(hql);
    Object[] objs 
=  (Object[])list.get( 0 );
    counts[
0 =  objs[ 0 ] + "" ;
    counts[
1 =  objs[ 1 ] + "" ;
    
return  counts;
  }

}

你可能感兴趣的:(Spring + Hibernate 数据访问)