package com.common.base.dao;import java.io.Serializable;import java.util.Collection;import org.hibernate.Session;import org.hibernate.criterion.DetachedCriteria;public interface BaseDAOable {//再基本的 -增- -刪- -改- -查-//只有查是多變的難以控制public void create(Object po)throws Exception;public void remove(Object po)throws Exception;public void update(Object po)throws Exception;public void removeList(java.util.List list)throws Exception;public void saveOrUpdateList(java.util.List list)throws Exception;public void load(Object po,Serializable pk)throws Exception;/** 以下為查詢相關(guān)語句**/public Collection findByCriteriaByPage(DetachedCriteria dc,int startindex,int pagesize)throws Exception;public Collection findByCriteria(DetachedCriteria dc)throws Exception ;public Integer countByCriteria(DetachedCriteria dc) throws Exception ;/*** 調(diào)用hibernate的get方法得到對(duì)象*/public Object get(Class c,Serializable pk) throws Exception;/*** 得到 Hibernate的Session* @return*/public Session getLocalSession() throws Exception;}
package com.common.base.dao;import java.io.Serializable;import java.util.Collection;import java.util.List;import org.hibernate.Session;import org.hibernate.criterion.DetachedCriteria;import org.hibernate.criterion.Projections;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class BaseDAOimpl extends implements BaseDAOable {/*** 得Hibernate 得session (non-Javadoc)** @see com.common.base.dao.BaseDAOable#getLocalSession()*/public Session getLocalSession() {return super.getSession();}/*** 增加*/public void create(Object po) throws Exception {this.getHibernateTemplate().save(po);}/*** 刪除*/public void remove(Object po) throws Exception {this.getHibernateTemplate().delete(po);}/*** 修改*/public void update(Object po) throws Exception {this.getHibernateTemplate().update(po);}/*** 刪除多個(gè)對(duì)像*/public void removeList(java.util.List list) throws Exception {this.getHibernateTemplate().deleteAll(list);}/*** 保存或更新多個(gè)對(duì)像*/public void saveOrUpdateList(java.util.List list) throws Exception {this.getHibernateTemplate().saveOrUpdateAll(list);}/*** 通過記錄的主鍵載入持久數(shù)據(jù)到對(duì)象*/public void load(Object po, Serializable pk) throws Exception {this.getHibernateTemplate().load(po, pk);}/*** 查詢,*/// 對(duì)所有記錄分頁(yè)查詢public Collection findByCriteriaByPage(DetachedCriteria dc, int startindex,int pagesize) throws Exception {Collection result = this.getHibernateTemplate().findByCriteria(dc,startindex, pagesize);return result;}// 查詢所有記錄用DetachedCriteriapublic Collection findByCriteria(DetachedCriteria dc) throws Exception {Collection result = this.getHibernateTemplate().findByCriteria(dc);return result;}/*** 統(tǒng)計(jì),*/// 統(tǒng)計(jì)記錄數(shù)量public Integer countByCriteria(DetachedCriteria dc) throws Exception {Integer count = 0;dc.setProjection(Projections.rowCount());List list = this.getHibernateTemplate().findByCriteria(dc);if (list != null && list.size() > 0) {count = (Integer) list.get(0);}return count;}public Object get(Class c, Serializable pk) throws Exception {return this.getHibernateTemplate().get(c, pk);}}
package com.common.base.service;import java.io.Serializable;import java.util.Collection;import java.util.List;import org.hibernate.Session;import org.hibernate.criterion.DetachedCriteria;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;import com.common.base.dao.BaseDAOable;import com.common.exception.unchecked.ServiceException;import com.common.util.LogManage;public class BaseServiceimpl implements BaseServiceable {private BaseDAOable basedao;private ManualTransactionManager transaction;/*** 獲得事物狀態(tài)*/public TransactionStatus getTransactionStatus() {try {TransactionDefinition definition = new DefaultTransactionDefinition();TransactionStatus status = transaction.getTransactionManager().getTransaction(definition);return status;} catch (Exception e) {LogManage.errorLog("TransactionStatus getTransactionStatus() " + e);throw new ServiceException(e);}}/*** 回滾事物*/public void rollbackTransaction(TransactionStatus status) {try {transaction.getTransactionManager().rollback(status);} catch (Exception e) {LogManage.errorLog("rollbackTransaction(TransactionStatus status) "+ e);LogManage.warnLog("事物回滾失敗");throw new ServiceException(e);}}/*** 提交事物*/public void commitTransaction(TransactionStatus status) {try {transaction.getTransactionManager().commit(status);} catch (Exception e) {LogManage.errorLog("rollbackTransaction(TransactionStatus status) "+ e);LogManage.warnLog("提交事物失敗");this.rollbackTransaction(status);throw new ServiceException(e);}}/*** 增加*/public void create(Object po) throws ServiceException {try {basedao.create(po);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.create(Object po) " + e);throw new ServiceException(e);}}/*** 刪除*/public void delete(Object po) throws ServiceException {try {basedao.remove(po);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.delete(Object po) " + e);throw new ServiceException(e);}}/*** 修改*/public void update(Object po) throws ServiceException {try {basedao.update(po);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.update(Object po) " + e);throw new ServiceException(e);}}public Object get(Object po, Serializable pk) throws ServiceException {try {return null;} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.loadByPK(Object po,Serializable pk) "+ e);throw new ServiceException(e);}}/*** 在修改表數(shù)據(jù)時(shí)需先獲得該記錄對(duì)象*/public void loadByPK(Object po, Serializable pk) throws ServiceException {try {basedao.load(po, pk);if (po == null) {throw new ServiceException("Load Object \"" + po.getClass()+ "\" is NULL");}} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.loadByPK(Object po,Serializable pk) "+ e);throw new ServiceException(e);}}/*** 刪除一個(gè)集合中的內(nèi)容*/public void deleteList(List list) throws ServiceException {try {basedao.removeList(list);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.deleteList(List list) " + e);throw new ServiceException(e);}}/*** 保存或修改一個(gè)集合中的內(nèi)容*/public void saveOrUpdateList(List list) throws ServiceException {try {basedao.saveOrUpdateList(list);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.doSaveOrUpdateList(List list) "+ e);throw new ServiceException(e);}}public Collection findByCriteriaByPage(DetachedCriteria dc, int startindex,int pagesize) throws ServiceException {Collection result = null;try {result = basedao.findByCriteriaByPage(dc, startindex, pagesize);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.findByCriteriaByPage(Object po,int startindex,int pagesize) "+ e);throw new ServiceException(e);}return result;}/*** 將WEB層構(gòu)建的 DetachedCriteria 傳到服務(wù)層進(jìn)行查詢 這樣在WEB層就偶合了hibernate api** @param dc* @return* @throws ServiceException*/public Collection findByCriteria(DetachedCriteria dc)throws ServiceException {Collection result = null;try {result = basedao.findByCriteria(dc);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.findByCriteria(DetachedCriteria dc) "+ e);throw new ServiceException(e);}return result;}/*** 將WEB層構(gòu)建的 DetachedCriteria 傳到服務(wù)層進(jìn)行統(tǒng)計(jì) 這樣在WEB層就偶合了hibernate api** @param dc* @return* @throws ServiceException*/public int countByCriteria(DetachedCriteria dc) throws ServiceException {int count = 0;try {count = basedao.countByCriteria(dc);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.countByCriteria(DetachedCriteria dc) "+ e);throw new ServiceException(e);}return count;}/*** 得到Hibernate的Session*/public Session getSession() throws ServiceException{try {return basedao.getLocalSession();} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.getLocalSession() "+ e);throw new ServiceException(e);}}/*** 查詢一個(gè)對(duì)象*/public Object get(Class c, Serializable pk) throws ServiceException {try {return basedao.get(c, pk);} catch (Exception e) {LogManage.errorLog("BaseServiceimpl.get() "+ e);throw new ServiceException(e);}}public BaseDAOable getBasedao() {return basedao;}public void setBasedao(BaseDAOable basedao) {this.basedao = basedao;}public ManualTransactionManager getTransaction() {return transaction;}public void setTransaction(ManualTransactionManager transaction) {this.transaction = transaction;}}
聯(lián)系客服