徐州北大青鳥(niǎo)分享一個(gè)Java通用的DAO及接口代碼——以下為DAO類代碼
======================================================================
package com.oa.dao.support;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.oa.dao.IMultiDAO;
import com.oa.struts.PageInfo;
public class MultiDAO extends HibernateDaoSupport implements IMultiDAO {
private static final Log log = LogFactory.getLog(MultiDAO.class);
/**
* 自定義查詢方法(無(wú)參)
*
* @param hql
* @return
* @throws Exception
*/
public List findMsg(String hql) throws Exception {
log.debug("自定義查詢:" + hql);
try {
List list = getHibernateTemplate().find(hql);
return list;
} catch (Exception e) {
log.error("自定義查詢:" + e.getMessage());
throw e;
}
}
/**
* 自定義查詢方法(有參)
*
* @param hql
* @param params
* @return
* @throws Exception
*/
public List findMsg(String hql, Object[] params) throws Exception {
log.debug("自定義查詢:" + hql);
try {
List list = getHibernateTemplate().find(hql, params);
return list;
} catch (Exception e) {
log.error("自定義查詢:" + e.getMessage());
throw e;
}
}
/**
* 自定義查詢方法
*
* @param cls
* @param id
* @return
*/
public Object getObject(Class cls, Serializable id) throws Exception {
log.debug("自定義查詢:" + cls.getName());
try {
Object obj = this.getHibernateTemplate().get(cls, id);
return obj;
} catch (Exception e) {
log.error("自定義查詢:" + e.getMessage());
throw e;
}
}
/**
* 分頁(yè)查詢方法
*
* @param queryString
* @param parameters
* @param pageInfo,必填充pageSize,pageIndex
* @return
*/
public PageInfo findPageByQuery(final String hql,
final Object[] parameters, final PageInfo pageInfo) {
List list = getHibernateTemplate().executeFind(new HibernateCallback()// 這里使用了匿名內(nèi)部類
{
public Object doInHibernate(Session session)// Spring進(jìn)行事務(wù)維護(hù)
// 省去每次創(chuàng)建session和關(guān)閉session
throws HibernateException {
Query query = session.createQuery(hql);
if (parameters != null) {
for (int i = 0; i < parameters.length; i++) {
query.setParameter(i, parameters[i]);
}
}
ScrollableResults sr = query.scroll();
sr.last();// 滾至最后一行
int totalCount = sr.getRowNumber();// 最后記錄行數(shù),從零開(kāi)始???
int startIndex = (pageInfo.getPageIndex() - 1)
* pageInfo.getPageSize();// 去除頁(yè)數(shù)和頁(yè)記錄數(shù)計(jì)算開(kāi)始位置
query.setMaxResults(pageInfo.getPageSize());
query.setFirstResult(startIndex);
int totalRec = totalCount + 1;// 獲取總記錄數(shù)
pageInfo.setTotalRec(totalRec);// 設(shè)置總記錄數(shù)
int totalPage = (totalRec + pageInfo.getPageSize() - 1)
/ pageInfo.getPageSize();// 計(jì)算總頁(yè)數(shù)
pageInfo.setTotalPage(totalPage);// 設(shè)置總頁(yè)數(shù)
pageInfo.setPrePage(pageInfo.getPageIndex() - 1);// 設(shè)置上一頁(yè)
pageInfo.setNextPage(pageInfo.getPageIndex() + 1);// 設(shè)置下一頁(yè)
return query.list();
}
});
pageInfo.setPageList(list);
return pageInfo;
}
/**
* 自定義保存方法
*
* @param entity
* @throws Exception
*/
public void save(Object entity) throws Exception {
log.debug("保存數(shù)據(jù):" + entity.toString());
try {
this.getHibernateTemplate().save(entity);
} catch (Exception e) {
log.error("保存錯(cuò)誤:" + e.getMessage());
throw e;
}
}
/**
* 自定義刪除方法
*
* @param entity
* @throws Exception
*/
public void delete(Object entity) throws Exception {
log.debug("刪除操作:" + entity.toString());
try {
this.getHibernateTemplate().delete(entity);
} catch (Exception e) {
log.error("刪除錯(cuò)誤:" + e.getMessage());
throw e;
}
}
/**
* 自定義更新操作
*
* @param entity
* @throws Exception
*/
public void update(Object entity) throws Exception {
log.debug("更新操作:" + entity.toString());
try {
this.getHibernateTemplate().update(entity);
} catch (Exception e) {
log.error("更新操作:" + e.getMessage());
throw e;
}
}
/**
* 特殊情況多數(shù)更新刪除時(shí)
*
* @param hql
* @throws Exception
*/
public void userDefined(String hql, Object[] params) throws Exception {
log.debug("自定義操作:" + hql);
Session session = this.getSession();
Transaction trans = session.beginTransaction();// 開(kāi)始事務(wù)
try {
Query query = session.createQuery(hql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);// 填入?yún)?shù)
}
}
query.executeUpdate();
trans.commit();// 提交事務(wù)
} catch (Exception e) {
trans.rollback();// 回滾事務(wù)
log.error("自定義操作" + e.getMessage());
throw e;
} finally {
session.close();// 關(guān)閉Session
}
}
}
======================================================================
徐州北大青鳥(niǎo)分享一個(gè)Java通用的DAO及接口代碼——以下為接口代碼
======================================================================
package com.oa.dao;
import java.io.Serializable;
import java.util.List;
import com.oa.struts.PageInfo;
public interface IMultiDAO {
/**
* 自定義查詢方法(無(wú)參)
*
* @param hql
* @return
* @throws Exception
*/
public List findMsg(String hql) throws Exception ;
/**
* 自定義查詢方法(有參)
* @param hql
* @param params
* @return
* @throws Exception
*/
public List findMsg(String hql,Object [] params) throws Exception;
/**
* 自定義查詢方法
* @param cls
* @param id
* @return
*/
public Object getObject(Class cls,Serializable id) throws Exception;
/**
* 分頁(yè)查詢方法
* @param queryString
* @param parameters
* @param pageInfo,必填充pageSize,pageIndex
* @return
*/
public PageInfo findPageByQuery(final String hql,
final Object[] parameters, final PageInfo pageInfo) ;
/**
* 自定義保存方法
* @param entity
* @throws Exception
*/
public void save(Object entity) throws Exception;
/**
* 自定義刪除方法
* @param entity
* @throws Exception
*/
public void delete(Object entity)throws Exception;
/**
* 自定義更新操作
* @param entity
* @throws Exception
*/
public void update(Object entity) throws Exception;
/**
* 特殊情況多數(shù)更新刪除時(shí)
* @param hql
* @throws Exception
*/
public void userDefined(String hql,Object [] params) throws Exception;
}
===============================================================
徐州北大青鳥(niǎo)分享
(責(zé)任編輯:代碼如詩(shī)) |