hibernate 的basedao

package com.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.entity.NewsTypeInfo;
import com.entity.PageModel;
import com.util.HibernateSessionFactory;

/**
* dao层基类
* @author Think
*
*/
public class BaseDao {

/**
* 添加对象
* @param 对象实例
* @return 对象的插入数据后的id  --失败 返回null
*/
public Serializable addObject(Object obj){
    Session session = null;
        Transaction tx = null;
        Serializable idkey = null;
        try {
            session =HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            idkey=  session.save(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
            return idkey;
        }
}

/**
*
* @param 类
* @param 主键列
* @return
*/
public Object getObject(Class c ,Serializable id){
Session session =HibernateSessionFactory.getSession();
Object obj= session.get(c, id);
if(session!=null){session.close();}
  return obj;
}

/**
* 修改对象
* @param 对象
* @return true or false
*/
public boolean updateObject(Object obj){
    Session session = null;
        Transaction tx = null;
        boolean isok = true;
        try {
            session =HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            session.update(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            isok = false;
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
            return isok;
        }
       }
/**
* 删除对象
* @param 对象
* @return true or false
*/
public boolean deleteObject(Object obj){
    Session session = null;
        Transaction tx = null;
        boolean isok = true;
        try {
            session =HibernateSessionFactory.getSession();
            tx = session.beginTransaction(); 
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            isok = false;
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
            return isok;
        }
       }
/**
* hql查询 
* @param hql语句
* @param 参数
* @return
*/
public List getObjectListByHQL(String hql,Object ... params){

  Session  session =HibernateSessionFactory.getSession();
  Query query = session.createQuery(hql);
  if(params!=null && params.length>0){
for(int i=0;i<params.length;i++){
query.setString(i, params[i]+"");
}
  } 
  List list =  query.list();
  if(session!=null){session.close();}
  return list;
}

/**
* 限制返回行数的查询
* @param hql语句
* @param 返回大小
* @param 参数
* @return
*/
public List queryObjectListSizeByHQL(String hql,int size,Object ... params){

Session  session =HibernateSessionFactory.getSession();
  Query query = session.createQuery(hql);
  System.out.println( "................................"+params );
  if(params!=null && params.length>0){
for(int i=0;i<params.length;i++){
query.setString(i, params[i]+"");
}
  } 
  query.setMaxResults(size); 
  List list =  query.list();
  if(session!=null){session.close();}
  return list;
}
   
/**
* 分页的数据查询
* @param 当前页
* @param 每页显示的数据
* @param 查询语句
* @param 语句的参数
* @return
*/
public PageModel queryPageModel(int pageno,int rowsize,String hql,Object...params){
PageModel pm = new PageModel();
pm.setRowsize(rowsize);  
pm.setPagenow(pageno);
pm.setList_info(hql,params);  
return pm;
}



}

你可能感兴趣的:(DAO,Hibernate)