/**
*
*/
package zhenjw.base.hibernate.db.dao;
import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
/**
* @author zhenjw
*
*/
public interface IBaseDao {
/**
* 获得数据库连接的Session
* @return
*/
public Session getSession();
/**
* 得到记录总数
* @param queryString
* @return
*/
public int getTotalCount(String queryString) ;
/**
* 得到记录总数
* @param queryString
* @return
*/
public int getTotalCountBySql(String queryString) ;
/**
* 得到记录总数
* @param queryString
* @param obj
* @return
*/
public int getTotalCount(String queryString,Object obj) ;
/**
* 得到记录总数
* @param queryString
* @param paramname
* @param paramvalue
* @return
*/
public int getTotalCount(String queryString,String[]paramname ,Object []paramvalue) ;
/**
* 得到Query
* @param queryString
* @return
*/
public Query getQuery(String queryString);
/**
* 得到Criteria
* @param clazz
* @return
*/
public Criteria getCriteria(Class clazz) ;
/**
* 增加记录
* @param entity
*/
public void create(Object entity);
/**
* 修改记录
* @param entity
*/
public void update(Object entity);
/**
* 删除记录
* @param entity
*/
public void delete(Object entity);
/**
* 删除数据
* @param clazz
* @param id
*/
public void delete(Class clazz,long id);
/**
* 批量删除数据
* @param clazz
* @param id
*/
public void batchDelete(Class clazz,long []id);
/**
* 删除表中的所有的记录
* @param clazz
*/
public void deleteAll(Class clazz) ;
/**
* 删除记录集中的所有的记录
* @param entities
*/
public void deleteAll(Collection entities) ;;
/**
* 通过主键查询记录
* @param clazz
* @param id
* @return
*/
public Object getByPk(Class clazz, Integer id);
/**
* 通过主键查询记录
* @param clazz
* @param id
* @return
*/
public Object getByPk(Class clazz, Long id);
/**
* 通过主键查询记录
* @param clazz
* @param id
* @return
*/
public Object getByPk(Class clazz, String id);
/**
* 通过关键字和值来进行查询
* @param clazz
* @param keyName
* @param keyValue
* @return 得到的Object是List
* @
*/
public Object loadByPk(Class clazz, String keyName, Object keyValue);
/**
* 查询记录
* @param queryString
* @return
*/
public List find(String queryString) ;
/**
* 查询记录
* @param queryString
* @param param
* @return
*/
public List find(String queryString, Object param) ;
/**
* 查询记录
* @param queryString
* @param name
* @param param
* @return
*/
public List findByNamedParam(String queryString ,String name,Object param);
/**
* 查询记录
* @param queryString
* @param params
* @return
*/
public List find(String queryString, Object[] params) ;
/**
* 得到表中的所有记录
* @param clazz
* @return
*/
public List loadAll(Class clazz) ;
/**
* 分页查询注意Object[]中不能包含有hibernate不能唯一确定的数据类型例如Date类型
* @param pageNo
* @param pageSize
* @param hql
* @param paramname
* @param paramvalue
* @return
*/
public List query(int pageNo,int pageSize,String queryString,String []paramname,Object []paramvalue);
/**
* 分页查询
* @param pageNo
* @param pageSize
* @param queryString
* @return
*/
public List query(int pageNo,int pageSize,String queryString );
/**
* 分页查询
* @param pageNo
* @param pageSize
* @param queryString
* @param obj
* @return
*/
public List query(int pageNo,int pageSize,String queryString ,Object obj);
}
下面是实现类
package zhenjw.base.hibernate.db.dao;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import zhenjw.hibernate.HibernateSessionFactory;
/**
* @author zhenjw
*
*
*/
public class BaseDao implements IBaseDao {
private Logger logger = Logger.getLogger(this.getClass());
private String databeaseError=Messages.getString("BaseDao.databeaseError");
private SessionFactory sessionFactory;
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#GetTotal(java.lang.String)
*/
public int getTotalCount(String queryString) {
// TODO Auto-generated method stub
logger.debug("getTotalCount() queryString=" + queryString);
int result = 0;
Session session = null;
try {
String strsql =this.getQueryTotalCountSqlString(queryString);
session = this.getSession();
Query query = session.createQuery(strsql);
List list = query.list();
if (list != null || list.size() > 1) {
result = ((Long) list.get(0)).intValue();
} else
result = 0;
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}
finally
{
this.closeSession(session);
}
return result;
}
/* (non-Javadoc)
* @see zhenjw.base.hibernate.db.dao.IBaseDao#getTotalCountBySql(java.lang.String)
*/
public int getTotalCountBySql(String queryString) {
// TODO Auto-generated method stub
logger.debug("getTotalCountBySql() queryString=" + queryString);
int result = 0;
Session session =null;
try {
String strsql =this.getQueryTotalCountSqlString(queryString);
session = this.getSession();
Query query = session.createSQLQuery(strsql);
List list = query.list();
if (list != null || list.size() > 1) {
result = ((Long) list.get(0)).intValue();
} else
result = 0;
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}
finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernate.db.dao.IBaseDao#getTotalCount(java.lang.String,
* java.lang.String[], java.lang.Object[])
*/
public int getTotalCount(String queryString, Object obj) {
// TODO Auto-generated method stub
logger.debug("getTotalCount(String,Object) queryString=" + queryString + ""
+ obj.getClass().getName());
int result = 0;
Session session =null;
try {
String strsql =this.getQueryTotalCountSqlString(queryString);
session = this.getSession();
Query query = session.createQuery(strsql);
List list = query.setProperties(obj).list();
if (list != null || list.size() > 1) {
result = ((Integer) list.get(0)).intValue();
} else
result = 0;
} catch (Exception e) {
String error=Messages.getString("BaseDao.getTotalCount.Error");
logger.debug(error, e);
throw new DaoException(error);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernate.db.dao.IBaseDao#getTotalCount(java.lang.String,
* java.lang.String[], java.lang.Object[])
*/
public int getTotalCount(String queryString, String[] paramname, Object[] paramvalue) {
// TODO Auto-generated method stub
logger.debug("getTotalCount(String, String[], Object[]) queryString=" + queryString);
int result = 0;
String paramnameArray = "";
for (int i = 0; i < paramname.length; i++) {
if (i > 0)
paramnameArray += " , " + paramname[i];
else
paramnameArray = paramname[i];
}
logger.debug("String[] paramname=" + "{" + paramnameArray + "}");
String paramvalueArray = "";
for (int i = 0; i < paramname.length; i++) {
if (i > 0)
paramvalueArray += " , " + paramname[i];
else
paramvalueArray = paramname[i];
}
logger.debug("Object[] paramvalue=" + "{" + paramvalueArray + "}");
Session session =null;
try {
if (paramname.length != paramname.length) {
String arrayparamError=Messages.getString("BaseDao.getTotalCount.paramarrayError");
logger.debug(arrayparamError);
throw new DaoException(arrayparamError);
}
String strsql =this.getQueryTotalCountSqlString(queryString);
session = this.getSession();
Query query = session.createQuery(strsql);
for (int i = 0; i < paramname.length; i++) {
query.setParameter(paramname[i], paramvalue[i]);
}
List list = query.list();
if (list != null || list.size() > 1) {
result = ((Long) list.get(0)).intValue();
} else
result = 0;
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernate.db.dao.IBaseDao#queryHql(int, int,
* java.lang.String, java.lang.String[], java.lang.Object[])
*/
public List query(int pageNo, int pageSize, String queryString, String[] paramname,
Object[] paramvalue) {
// TODO Auto-generated method stub
logger.debug("queryByHql(int, int, String, String[], Object[]) pageNo="
+ pageNo + ",pageSize=" + pageSize + " ,queryString=" + queryString);
List result = null;
String paramnameArray = "";
for (int i = 0; i < paramname.length; i++) {
if (i > 0)
paramnameArray += " , " + paramname[i];
else
paramnameArray = paramname[i];
}
logger.debug("String[] paramname=" + "{" + paramnameArray + "}");
String paramvalueArray = "";
for (int i = 0; i < paramname.length; i++) {
if (i > 0)
paramvalueArray += " , " + paramname[i];
else
paramvalueArray = paramname[i];
}
logger.debug("Object[] paramvalue=" + "{" + paramvalueArray + "}");
Session session = null;
try {
if (paramname.length != paramname.length) {
String paramarrayError=Messages.getString("BaseDao.query.paramarrayError");
logger.debug(paramarrayError);
throw new DaoException(paramarrayError);
}
session = this.getSession();
Query query = session.createQuery(queryString);
for (int i = 0; i < paramname.length; i++) {
query.setParameter(paramname[i], paramvalue[i]);
}
if (pageNo > 0 && pageSize > 0) {
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
}
List list = query.list();
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}
finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernate.db.dao.IBaseDao#query(int, int,
* java.lang.String, java.lang.Object)
*/
public List query(int pageNo, int pageSize, String queryString, Object obj) {
// TODO Auto-generated method stub
logger.debug("query(int, int, String, Object) pageNo=" + pageNo
+ ",pageSize=" + pageSize + "queryString ,obj"
+ obj.getClass().getName());
List result = null;
Session session =null;
try {
session = this.getSession();
Query query = session.createQuery(queryString);
query.setProperties(obj);
if (pageNo > 0 && pageSize > 0) {
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
}
result = query.list();
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernate.db.dao.IBaseDao#query(int, int,
* java.lang.String)
*/
public List query(int pageNo, int pageSize, String queryString) {
// TODO Auto-generated method stub
logger.debug("query(int, int, String) pageNo="+pageNo+",pageSize="+pageSize+" ,queryString="+queryString);
List result = null;
Session session=null;
try {
session = this.getSession();
Query query = session.createQuery(queryString);
if (pageNo > 0 && pageSize > 0) {
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
}
List list = query.list();
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#create(java.lang.Object)
*/
public void create(Object entity) {
logger.debug("create(Object) "+entity.getClass().getName());
// TODO Auto-generated method stub
/**
try {
super.getHibernateTemplate().save(entity);
logger.debug("保存" + entity.getClass().getName() + " 的实例到数据库成功!");
} catch (Exception e) {
String error=Messages.getString("BaseDao.create.saveError");
logger.debug("保存" + entity.getClass().getName() + " 的实例到数据库 ,"+error, e);
throw new DaoException(error);
}
**/
Session session=null;
Transaction tr=null;
try {
session=this.getSession();
tr=session.beginTransaction();
session.save(entity);
tr.commit();
//super.getHibernateTemplate().save(entity);
logger.debug("保存" + entity.getClass().getName() + " 的实例到数据库成功!");
} catch (Exception e) {
if(tr!=null)
tr.rollback();
String error=Messages.getString("BaseDao.create.saveError");
logger.debug("保存" + entity.getClass().getName() + " 的实例到数据库 ,"+error, e);
throw new DaoException(error);
}
finally
{
this.closeSession(session);
}
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#delete(java.lang.Object)
*/
public void delete(Object entity) {
logger.debug("delete(Object) entity.class="+entity.getClass().getName());
// TODO Auto-generated method stub
/**
try {
super.getHibernateTemplate().delete(entity);
} catch (Exception e) {
String Error=Messages.getString("BaseDao.delete.Error");
logger.debug(Error+" Class="+entity.getClass().getName(),
e);
throw new DaoException(Error);
}
**/
Session session=null;
Transaction tr=null;
try {
session=this.getSession();
tr=session.getTransaction();
tr.begin();
session.delete(entity);
tr.commit();
} catch (Exception e) {
if(tr!=null)
tr.rollback();
String Error=Messages.getString("BaseDao.delete.Error");
logger.debug(Error+" Class="+entity.getClass().getName(),
e);
throw new DaoException(Error);
}
finally
{
this.closeSession(session);
}
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#deleteAll(java.lang.Class)
*/
public void deleteAll(Class clazz) {
logger.debug("deleteAll(Class) ClassName="+clazz.getName());
// TODO Auto-generated method stub
Session session=null;
Transaction tr=null;
try {
//List result = super.getHibernateTemplate().loadAll(clazz);
//super.getHibernateTemplate().deleteAll(result);
session=this.getSession();
tr=session.beginTransaction();
Query query=session.createQuery(" delete from "+clazz.getName());
query.executeUpdate();
tr.commit();
} catch (Exception e) {
if(tr!=null)
tr.rollback();
String Error=Messages.getString("BaseDao.delete.Error");
logger.debug("从数据库中删除" + clazz.getName() + "的所有实例失败!", e);
throw new DaoException(Error);
}finally
{
this.closeSession(session);
}
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#deleteAll(java.util.Collection)
*/
public void deleteAll(Collection entities) {
// TODO Auto-generated method stub
Session session=null;
Transaction tr=null;
try {
session=this.getSession();
tr=session.beginTransaction();
Iterator ite=entities.iterator();
while(ite.hasNext())
session.delete(ite.next());
//super.getHibernateTemplate().deleteAll(entities);
tr.commit();
} catch (Exception e) {
if(tr!=null)
tr.rollback();
String error=Messages.getString("BaseDao.deleteAll.Error");
logger.debug(error, e);
throw new DaoException(error);
}finally
{
this.closeSession(session);
}
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#find(java.lang.String)
*/
public List find(String queryString) {
// TODO Auto-generated method stub
List result = null;
logger.debug("find(String) queryString="+queryString);
Session session=null;
try {
//result = super.getHibernateTemplate().find(queryString);
session=this.getSession();
Query query=session.createQuery(queryString);
result=query.list();
} catch (Exception e) {
logger.debug("执行数据库中查询时失败,语句为:" + queryString, e);
throw new DaoException(databeaseError);
}finally{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#find(java.lang.String,
* java.lang.Object)
*/
public List find(String queryString, Object param) {
// TODO Auto-generated method stub
logger.debug("find(String, Object) queryString=" + queryString
+ " ,param=" + param);
List result = null;
Session session=null;
try {
session=this.getSession();
Query query=session.createQuery(queryString);
query.setParameter(0, param);
result=query.list();
//result = super.getHibernateTemplate().find(queryString, param);
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}
finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernate.db.dao.IBaseDao#findByNamedParam(java.lang.String,
* java.lang.String, java.lang.Object)
*/
public List findByNamedParam(String queryString, String name, Object param) {
// TODO Auto-generated method stub
logger.debug("findByNamedParam(String, String, Object) queryString="
+ queryString + "name=" + name + " ,param=" + param);
List result = null;
Session session=null;
try {
session=this.getSession();
Query query=session.createQuery(queryString);
query.setParameter(name, param);
result=query.list();
//result = super.getHibernateTemplate().findByNamedParam(queryString,
// name, param);
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#find(java.lang.String,
* java.lang.Object[])
*/
public List find(String queryString, Object[] params) {
// TODO Auto-generated method stub
StringBuffer paramString = new StringBuffer("");
for (int i = 0; i < params.length; i++) {
paramString.append(params[i]);
paramString.append(" [] ");
}
logger.debug("find(String, Object[]) queryString=" + queryString
+ ",params : " + paramString);
List result = null;
Session session=null;
try {
session=this.getSession();
Query query=session.createQuery(queryString);
for(int i=0;i<params.length;i++)
{
query.setParameter(i, params[i]);
}
result=query.list();
//result = super.getHibernateTemplate().find(queryString, params);
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}
finally{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#getByPk(java.lang.Class,
* java.lang.Integer)
*/
public Object getByPk(Class clazz, Integer id) {
// TODO Auto-generated method stub
logger.debug("getByPk(Class, Integer) class="+clazz.getName()+" ID="+id);
Object result = null;
Session session=null;
try {
session=this.getSession();
result=session.get(clazz, id);
//result = (Object) super.getHibernateTemplate().get(clazz, id);
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#getByPk(java.lang.Class,
* java.lang.Long)
*/
public Object getByPk(Class clazz, Long id) {
// TODO Auto-generated method stub
logger.debug("getByPk(Class, Long) Class="+clazz.getName()+",id="+id);
Object result = null;
Session session=null;
try {
session=this.getSession();
result =session.get(clazz,id);
//(Object) super.getHibernateTemplate().get(clazz, id);
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#getByPk(java.lang.Class,
* java.lang.String)
*/
public Object getByPk(Class clazz, String id) {
// TODO Auto-generated method stub
logger.debug("getByPk(Class, String) Class="+clazz.getName()+",id="+id);
Object result = null;
Session session=null;
try {
session=this.getSession();
result =session.get(clazz,id);
//result = (Object) super.getHibernateTemplate().get(clazz, id);
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
public List loadAll(Class clazz) {
// TODO Auto-generated method stub
logger.debug(" loadAll(Class) Class="+clazz.getName());
List result = null;
Session session=null;
try {
session=this.getSession();
result=session.createCriteria(clazz).list();
//result = super.getHibernateTemplate().loadAll(clazz);
} catch (Exception e) {
//logger.debug(" 加载 " + clazz.getName() + " 的所有实例时失败", e);
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}
finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#loadByPk(java.lang.Class,
* java.lang.String, java.lang.Object)
*/
public Object loadByPk(Class clazz, String keyName, Object keyValue) {
// TODO Auto-generated method stub
Object result = null;
String query = "from " + clazz.getName() + " where " + keyName + "=? ";
logger.debug("loadByPk(Class, String, Object) queryString="+query+" ,keyValue="+keyValue);
Session session=null;
try {
session=this.getSession();
result=session.createCriteria(clazz).add(Restrictions.eq(keyName, keyValue )).list();
} catch (Exception e) {
logger.debug(databeaseError, e);
throw new DaoException(databeaseError);
}finally
{
this.closeSession(session);
}
return result;
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#getSession()
*/
public Session getSession() {
// TODO Auto-generated method stub
return HibernateSessionFactory.getSession();
//return getHibernateTemplate().get
//return SessionFactoryUtils.getSession(getSessionFactory(), true);
//return this.getSession();
}
/*
* (non-Javadoc)
*
* @see zhenjw.hibernage.db.dao.IBaseDao#update(java.lang.Object)
*/
public void update(Object entity) {
// TODO Auto-generated method stub
logger.debug("update(Object) entity.class="+entity.getClass().getName());
Session session=null;
Transaction tr=null;
try {
session=this.getSession();
tr=session.beginTransaction();
session.update(entity);
tr.commit();
//super.getHibernateTemplate().update(entity);
} catch (Exception e) {
if(tr!=null)
tr.rollback();
String Error=Messages.getString("BaseDao.update.Error");
logger.debug(Error,e);
throw new DaoException(Error);
}
finally
{
this.closeSession(session);
}
}
/* (non-Javadoc)
* @see zhenjw.hibernate.db.dao.IBaseDao#getCriteria(java.lang.Class)
*/
public Criteria getCriteria(Class clazz) {
// TODO Auto-generated method stub
logger.debug("getCriteria(Class) Class.name="+clazz.getName());
Criteria result=null;
Session session=null;
try
{
session=this.getSession();
result=session.createCriteria(clazz);
}catch(Exception e)
{
logger.debug(" 根据 class : "+clazz.getName()+" 得到 Criteria 时失败");
logger.debug(databeaseError,e);
throw new DaoException(databeaseError);
}finally{
this.closeSession(session);
}
return result;
}
/* (non-Javadoc)
* @see zhenjw.hibernate.db.dao.IBaseDao#getQuery(java.lang.String)
*/
public Query getQuery(String queryString) {
// TODO Auto-generated method stub
logger.debug("getQuery(String)queryString="+queryString);
Query result=null;
Session session=null;
try
{
session=this.getSession();
result=session.createQuery(queryString);
}catch(Exception e)
{
logger.debug(" 根据 "+queryString+" 得到 Query 时失败");
logger.debug("getQuery(String) "+databeaseError,e);
throw new DaoException(databeaseError);
}
finally
{
this.closeSession(session);
}
return result;
}
private String getQueryTotalCountSqlString(String queryString)
{
int form_index = queryString.indexOf("from ");
int orderby_index = queryString.indexOf(" order by ");
if (form_index < 0) {
throw new DaoException(Messages.getString("BaseDao.getTotalCount.notFromkeyword"));
}
String strsql = " select count(*) ";
if (orderby_index > -1) {
strsql = strsql + queryString.substring(form_index, orderby_index);
} else {
strsql = strsql + queryString;
}
return strsql;
}
private void closeSession(Session session)
{
if(session!=null&&session.isOpen())
session.close();
session=null;
}
/* (non-Javadoc)
* @see zhenjw.base.hibernate.db.dao.IBaseDao#delete(java.lang.Class, long)
*/
public void delete(Class clazz, long id) {
// TODO Auto-generated method stub
logger.debug("ClassName="+clazz.getName()+" ,id="+id);
try{
Object entity=this.getByPk(clazz, id);
if(entity!=null)
this.delete(entity);
else
logger.debug(clazz.getSimpleName()+" 的关键字为 "+id+" 的对象不存在 ");
}catch(Exception e)
{
logger.debug(" delete(Class, long) excute is error . Error="+e.toString());
throw new DaoException(Messages.getString("BaseDao.delete.Error"));
}
}
/* (non-Javadoc)
* @see zhenjw.base.hibernate.db.dao.IBaseDao#batchDelete(java.lang.Class, long[])
*/
public void batchDelete(Class clazz, long[] id) {
// TODO Auto-generated method stub
String strId="";
for(int i=0;i<id.length;i++)
{
if(i>0)
strId+=", "+id[i];
else
strId=""+id[i];
}
logger.debug("batchDelete(Class, long[]) id[]={"+strId+"}");
for(int i=0;i<id.length;i++)
{
this.delete(clazz,id[i]);
}
}
}
这两个可以满足所有的基本需求的实现