hibernate baseDaoImpl 类

importjava.io.Serializable;

importjava.sql.SQLException;

importjava.util.Collection;

importjava.util.Iterator;

importjava.util.List;

importorg.apache.log4j.Logger;

importorg.hibernate.HibernateException;

importorg.hibernate.Query;

importorg.hibernate.Session;

importorg.springframework.dao.DataAccessException;

importorg.springframework.orm.hibernate3.HibernateCallback;

importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**

*功能描述:BaseDaoHibImpl

*/

publicclassBaseDaoHibImpl<POJO>extendsHibernateDaoSupportimplementsBaseDao<POJO>{

Loggerlog=Logger.getLogger(BaseDaoHibImpl.class);

/**

*将记录持久化到数据库

*@parampojo需要持久化的pojo,类型为泛型所指定类型

*@returnvoid

**/

publicvoidsave(POJOpojo){

if(pojo==null){

thrownewSystemException("POJOisnullinBaseDAOHibernateImpl'ssava(),itmustn'tbenull.");

}

try{

getHibernateTemplate().save(pojo);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'ssava()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'ssava()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'ssave()runsuccessful.");

}

/**

*将记录从数据库删除

*@parampojo需要删除的pojo,类型为泛型所指定类型

*@returnvoid

**/

publicvoiddelete(POJOpojo){

if(pojo==null){

thrownewSystemException("POJOisnullinBaseDAOHibernateImpl'sdelete(),itmustn'tbenull.");

}

try{

getHibernateTemplate().delete(pojo);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sdelete()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sdelete()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sdelete()runsuccessful.");

}

/**

*将记录从数据库中批量删除

*@paramentities需要删除实体集合

*@returnvoid

**/

publicvoiddeleteAll(Collection<POJO>entities){

if(entities==null){

thrownewSystemException("POJOisnullinBaseDAOHibernateImpl'sdeleteAll(),itmustn'tbenull.");

}

try{

getHibernateTemplate().deleteAll(entities);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sdeleteAll()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sdeleteAll()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sdeleteAll()runsuccessful.");

}

/**

*更新数据库记录

*@parampojo需要更新的pojo,类型为泛型所指定类型

*@returnvoid

**/

publicvoidupdate(POJOpojo){

if(pojo==null){

thrownewSystemException("POJOisnullinBaseDAOHibernateImpl'supdate(),itmustn'tbenull.");

}

try{

getHibernateTemplate().update(pojo);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'supdate()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'supdate()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'supdate()runsuccessful.");

}

/**

*获得数据库所有记录get

*@parampojoNamepojo的类名字符串

*@paramorderBy字段排序

*@returnList<POJO>从数据库获得的相应记录的结果集,list的元素为POJO泛型

**/

@SuppressWarnings("unchecked")

publicList<POJO>getAll(StringpojoName,StringorderBy){

if((pojoName==null)||"".equals(pojoName)){

thrownewSystemException("pojoNameisnullor\"\"inBaseDAOHibernateImpl'sfindAll(),itmustn'tbethat.");

}

StringBuffersb=newStringBuffer();

sb.append("from"+pojoName);

sb.append("");

sb.append(orderBy);

StringqueryString=sb.toString();

log.debug(queryString);

List<POJO>list=null;

try{

list=(List<POJO>)getHibernateTemplate().find(queryString);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sfindAll()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sfindAll()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sfindAll()runsuccessful.");

returnlist;

}

/**

*获得数据库所有记录load

*@parampojoNamepojo的类名字符串

*@returnList<POJO>从数据库获得的相应记录的结果集,list的元素为POJO泛型

**/

@SuppressWarnings("unchecked")

publicList<POJO>loadAll(POJOpojo){

if((pojo==null)||"".equals(pojo)){

thrownewSystemException("pojoisnullor\"\"inBaseDAOHibernateImpl'sloadAll(),itmustn'tbethat.");

}

List<POJO>list=null;

try{

list=(List<POJO>)getHibernateTemplate().loadAll(pojo.getClass());

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sloadAll()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sloadAll()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sloadAll()runsuccessful.");

returnlist;

}

/**

*根据主键,获得数据库一条对应的记录,如果没有相应的实体,返回null

*@paramlongPojoNamepojo的类包名字符串

*@paramID主键类型可以是(Integer,Float,Double,Short,Byte,String)

*@returnPOJO从数据库获得的相应记录,POJO的实例,类型为POJO泛型

**/

@SuppressWarnings("unchecked")

publicPOJOgetPojoById(StringlongPojoName,SerializableID){

if((longPojoName==null)||"".equals(longPojoName)){

thrownewSystemException("longPojoNameisnullor\"\"inBaseDAOHibernateImpl'sgetPojoById(),itmustn'tbethat.");

}

POJOpojo=null;

try{

pojo=(POJO)getHibernateTemplate().get(longPojoName,ID);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sgetPojoById()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sgetPojoById()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sgetPojoById()runsuccessful.");

returnpojo;

}

/**

*根据主键,获得数据库一条对应的记录,如果没有相应的实体,抛出异常

*@paramlongPojoNamepojo的类包名字符串

*@paramID主键类型可以是(Integer,Float,Double,Short,Byte,String)

*@returnPOJO从数据库获得的相应记录,POJO的实例,类型为POJO泛型

**/

@SuppressWarnings("unchecked")

publicPOJOloadPojoById(StringlongPojoName,SerializableID){

if((longPojoName==null)||"".equals(longPojoName)){

thrownewSystemException("longPojoNameisnullor\"\"inBaseDAOHibernateImpl'sloadPojoById(),itmustn'tbethat.");

}

POJOpojo=null;

try{

pojo=(POJO)getHibernateTemplate().load(longPojoName,ID);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sloadPojoById()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sloadPojoById()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sloadPojoById()runsuccessful.");

returnpojo;

}

/**

*根据POJO的属性获得数据库相应记录(相当于根据查询条件的字符串获得相应记录)

*@paramqueryString查询条件的字符串

*@returnList<POJO>从数据库获得的相应记录的结果集,list的元素为POJO泛型

**/

@SuppressWarnings("unchecked")

publicList<POJO>findByProperty(StringqueryString){

if((queryString==null)||"".equals(queryString)){

thrownewSystemException("queryStringisnullor\"\"inBaseDAOHibernateImpl'sfindByProperty(),itmustn'tbethat.");

}

List<POJO>list=null;

try{

list=(List<POJO>)getHibernateTemplate().find(queryString);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sfindByProperty()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sfindByProperty()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sfindByProperty()runsuccessful.");

returnlist;

}

/**

*使用带参数的HSQL语句检索数据

*@paramqueryString查询条件

*@paramparas查询参数

*/

publicListfindByPropertyWithParas(StringqueryString,Object[]paras){

if((queryString==null)||"".equals(queryString)){

thrownewSystemException("queryStringisnullor\"\"inBaseDAOHibernateImpl'sfindByPropertyWithParas(),itmustn'tbethat.");

}

List<POJO>list=null;

try{

list=(List<POJO>)getHibernateTemplate().find(queryString,paras);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sfindByPropertyWithParas()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sfindByPropertyWithParas()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sfindByPropertyWithParas()runsuccessful.");

returnlist;

}

/**

*批量更新或保存记录

*@paramentities实体类集成

*@returnvoid

**/

publicvoidsaveOrUpdateAll(Collection<POJO>entities){

try{

this.getHibernateTemplate().saveOrUpdateAll(entities);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sfindByProperty()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sfindByProperty()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'ssaveOrUpdateAll()runsuccessful.");

}

/**

*获得单行记录

*@parampojoNamepojo的类名字符串

*@returnint数据库表的总记录数

**/

publicObjectgetSingleRowRecord(StringqueryString){

if((queryString==null)||"".equals(queryString)){

thrownewSystemException("queryStringisnullor\"\"inBaseDAOHibernateImpl'sgetSingleRowRecord(),itmustn'tbethat.");

}

Objectrecord=null;

try{

Sessionsession=openSession();

record=session.createQuery(queryString).uniqueResult();

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sgetSingleRowRecord()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sgetSingleRowRecord()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sgetSingleRowRecord()runsuccessful.");

returnrecord;

}

/**

*获得数据库表的总记录数

*@parampojoNamepojo的类名字符串

*@returnint数据库表的总记录数

**/

publicintgetRowCount(StringqueryString){

if((queryString==null)||"".equals(queryString)){

thrownewSystemException("queryStringisnullor\"\"inBaseDAOHibernateImpl'sgetRowCount(),itmustn'tbethat.");

}

intcount=0;

try{

Sessionsession=openSession();

LongcountLong=(Long)session.createQuery(queryString).uniqueResult();

count=countLong.intValue();

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sgetRowCount()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sgetRowCount()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sgetRowCount()runsuccessful.");

returncount;

}

/**

*获得数据库表的总记录数

*@parampojoNamepojo的类名字符串

*@paramparams查询参数

*@returnint数据库表的总记录数

**/

publicintgetRowCount(StringqueryString,Listparams){

if((queryString==null)||"".equals(queryString)){

thrownewSystemException("queryStringisnullor\"\"inBaseDAOHibernateImpl'sgetRowCount(),itmustn'tbethat.");

}

intcount=0;

try{

Sessionsession=openSession();

Queryquery=session.createQuery(queryString);

if(params!=null){

for(inti=0;i<params.size();i++){

query.setParameter(i,params.get(i));

}

}

LongcountLong=(Long)query.uniqueResult();

count=countLong.intValue();

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'sgetRowCount()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'sgetRowCount()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'sgetRowCount()runsuccessful.");

returncount;

}

/**

*保存或更新数据库的一条记录

*@paramPOJO保存或更新数据库的一条记录POJO

*@returnvoid

**/

publicvoidmerge(POJOpojo){

if(pojo==null){

thrownewSystemException("POJOisnullinBaseDAOHibernateImpl'smerge(),itmustn'tbenull.");

}

try{

getHibernateTemplate().merge(pojo);

}catch(DataAccessExceptione){

log.error("BaseDAOHibernateImpl'smerge()occurerror:",e);

thrownewSystemException("BaseDAOHibernateImpl'smerge()occurerror:",e);

}

log.debug("BaseDAOHibernateImpl'smerge()runsuccessful.");

}

/**

*功能描述:获得hibernatesession

*@para

*@returnSession

*/

publicSessionopenSession(){

returngetHibernateTemplate().getSessionFactory().getCurrentSession();

}

/**

*功能说明:分页查询

*@paramstart第几页,页码

*@paramlimit页面大小

*@paramqueryStringhql

*@return

*@throwsException

*@throws

*/

@SuppressWarnings("unchecked")

publicList<POJO>findByHqlWithPagination(finalintstart,finalintlimit,finalStringqueryString,finalListparams){

return(List<POJO>)getHibernateTemplate().executeWithNativeSession(newHibernateCallback(){

publicObjectdoInHibernate(finalSessionsession)throwsHibernateException,SQLException{

List<Object>list=null;

finalQueryquery=session.createQuery(queryString);

query.setFirstResult(start);

query.setMaxResults(limit);

if(params!=null){

for(inti=0;i<params.size();i++){

query.setParameter(i,params.get(i));

}

}

list=query.list();

returnlist;

}

});

}

/**

*功能说明:执行HQL语句

*@paramhqlString

*@throwsException

*/

publicvoidexcuteHql(finalStringhqlString){

getHibernateTemplate().executeWithNativeSession(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)throwsHibernateException,SQLException{

intresult=session.createQuery(hqlString).executeUpdate();

returnresult;

}

});

}

/**

*功能说明:执行sql语句

*@paramqueryString

*@paramstr

*@return

*/

publicListexcuteSql(StringqueryString,Stringstr){

Queryquery=null;

Sessionsession=openSession();

if(str.equals("newstype")){

System.out.println("newstype====================================================");

query=session.createSQLQuery(queryString).addEntity(WebNewstype.class);

}

if(str.equals("news")){

System.out.println("news====================================================");

query=session.createSQLQuery(queryString).addEntity(WebNews.class);

}

if(str.equals("warehouse")){

System.out.println("warehouse====================================================");

query=session.createSQLQuery(queryString).addEntity(WebWarehouse.class);

}

if(str.equals("msgBack")){

System.out.println("msgBack====================================================");

query=session.createSQLQuery(queryString).addEntity(MessageBack.class);

}

if(str.equals("visitors")){

System.out.println("visitors====================================================");

query=session.createSQLQuery(queryString).addEntity(WebNewsVisitors.class);

}

if(str.equals("newstypeName")){

System.out.println("newstypeName====================================================");

query=session.createSQLQuery(queryString);

}

if(str.equals("voteSingle")){

System.out.println("voteSingle====================================================");

query=session.createSQLQuery(queryString).addEntity(VoteSingle.class);

}

if(str.equals("tempSingle")){

System.out.println("tempSingle====================================================");

query=session.createSQLQuery(queryString).addEntity(TempSingle.class);

}

if(str.equals("navigation")){

System.out.println("navigation====================================================");

query=session.createSQLQuery(queryString).addEntity(Navigation.class);

}

returnquery.list();

}

/**

*功能说明:执行sql语句

*@paramqueryString

*@paramstr

*@return

*/

publicObjectexcuteSqlResult(StringqueryString){

Queryquery=null;

Sessionsession=openSession();

query=session.createSQLQuery(queryString);

returnquery.uniqueResult();

}

/**

*功能说明:批量保存

*@paramentities

*@throwsException

*/

publicvoidbatchSave(finalCollectionentities){

getHibernateTemplate().execute(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession){

intcount=0;

Iteratorit=entities.iterator();

while(it.hasNext()){

Objectobj=it.next();

if(obj!=null){

session.save(obj);

}

count++;

if(count%50==0){

//flushabatchofinsertsandreleasememoryper50

session.flush();

session.clear();

}

}

returnnull;

}

});

}

/**

*sql查询

*@paramhql

*@paramobj

*@return

*/

publicListgetObjectList(Stringhql,Classobj){

Sessionsession=openSession();

if(obj!=null){

returnsession.createSQLQuery(hql).addEntity(obj).list();

}else{

returnsession.createSQLQuery(hql).list();

}

}

}

相关推荐