Hibernate通用Dao&Biz接口及实现类模板
IGenericDao.java
package com.amg.dao.base;import java.io.Serializable;import java.util.List;import java.util.Map;import com.amg.vo.PageBean;public interface IGenericDao<T extends Serializable, ID extends Serializable> {public abstract int findByCount(String strHQL);public abstract ID create(T entity);public abstract T findById(ID id);public abstract void delete(ID id);public abstract void update(T entity);public abstract List<T> findAll();public abstract List<Map> findByHQL(final String strHQL, final Object[] params);public abstract List<T> findBySQL(final String strSQL, final Object[] params);public abstract List<T> findByHQL_Old(final String strHQL, final Object[] params);public abstract PageBean findByPage(final int currentPage, final int pageSize, final String strHQL, final Object[] params);public abstract void bulkUpdate(final String strHQL, final Object[] params);public abstract boolean executeUpdateBySession(String strSQL);}
?AGenericHibernateDao.java
package com.amg.dao.base;import java.io.Serializable;import java.lang.reflect.ParameterizedType;import java.sql.SQLException;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.springframework.orm.hibernate3.HibernateCallback;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.amg.vo.PageBean;public class AGenericHibernateDao<T extends Serializable, ID extends Serializable>extends HibernateDaoSupport implements IGenericDao<T, ID> {private Class<T> persistentClass;@SuppressWarnings("unchecked")public AGenericHibernateDao() {this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];}@SuppressWarnings("unchecked")public ID create(T entity) {return (ID) this.getHibernateTemplate().save(entity);}public T findById(ID id) {return this.getHibernateTemplate().get(persistentClass, id);}public void delete(ID id) {this.getHibernateTemplate().delete(findById(id));}public void update(T entity) {this.getHibernateTemplate().update(entity);}public List<T> findAll() {return this.getHibernateTemplate().loadAll(persistentClass);}public void bulkUpdate(String strHQL, Object[] params) {if (params == null)this.getHibernateTemplate().bulkUpdate(strHQL);this.getHibernateTemplate().bulkUpdate(strHQL, params);}public PageBean findByPage(final int currentPage, final int pageSize,final String strHQL, final Object[] params) {return this.getHibernateTemplate().execute(new HibernateCallback<PageBean>() {public PageBean doInHibernate(Session arg0)throws HibernateException, SQLException {PageBean bean = new PageBean();Query qu = arg0.createQuery(strHQL);for (int i = 0; i < params.length; i++) {qu.setParameter(i, params[i]);}qu.setFirstResult((currentPage - 1) * pageSize);qu.setMaxResults(pageSize);bean.setData(qu.list());qu = arg0.createQuery("select count(*) "+ strHQL.substring(strHQL.toLowerCase().indexOf("from")));for (int j = 0; j < params.length; j++) {qu.setParameter(j, params[j]);}bean.setTotalRows(Integer.parseInt(qu.uniqueResult().toString()));bean.setCurrentPage(currentPage);bean.setPageSize(pageSize);return bean;}});}@SuppressWarnings({ "rawtypes", "unchecked" })public List<java.util.Map> findByHQL(final String strHQL, final Object[] params) {return super.getHibernateTemplate().execute(new HibernateCallback<List<java.util.Map>>() {public List<java.util.Map> doInHibernate(Session arg0)throws HibernateException, SQLException {Query query = arg0.createQuery(strHQL);for (int i = 0; i < params.length; i++) {query.setParameter(i, params[i]);}return query.list();}});}public boolean executeUpdateBySession(String strSQL) {Query query = this.getSession().createSQLQuery(strSQL);return query.executeUpdate() > 0;}public int findByCount(final String strHQL) {return super.getHibernateTemplate().execute(new HibernateCallback<Integer>() {public Integer doInHibernate(Session arg0)throws HibernateException, SQLException {Query query = arg0.createQuery(strHQL);int count = (((Number) query.uniqueResult()).intValue());return count;}});}public List<T> findByHQL_Old(final String strHQL, final Object[] params) {return super.getHibernateTemplate().execute(new HibernateCallback<List<T>>() {@SuppressWarnings("unchecked")public List<T> doInHibernate(Session arg0)throws HibernateException, SQLException {Query query = arg0.createQuery(strHQL);for (int i = 0; i < params.length; i++) {query.setParameter(i, params[i]);}return query.list();}});}@SuppressWarnings("unchecked")public List<T> findBySQL(final String strSQL, final Object[] params) {return super.getHibernateTemplate().execute(new HibernateCallback<List<T>>() {public List<T> doInHibernate(Session arg0)throws HibernateException, SQLException {Query query = arg0.createQuery(strSQL);for (int i = 0; i < params.length; i++) {query.setParameter(i, params[i]);}return query.list();}});}}
?IRecordDao.java
package com.amg.dao;import java.util.List;import com.amg.dao.base.IGenericDao;import com.amg.po.Record;public interface IRecordDao extends IGenericDao<Record, Integer> {public List<?> findBySql_totalTypeCount(String sql,Class clas);public List<?> findBySql_totalStrutedCount(String sql,Class clas);//public List<?> findBySql_totalCountDetail(String sql,Class clas);public List<?> find2VoBySql(String sql,Class cls);public List<?> findBySql_totalTCountDetail(String sql, Class clas);public List<?> findBySql_totalSCountDetail(String sql, Class clas);public List<?> findBySql_totalTypeCountByDay(String sql, Class clas);}
?RecordDaoImpl.java
package com.amg.dao.impl;import java.sql.SQLException;import java.util.List;import org.hibernate.Hibernate;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.transform.Transformers;import org.springframework.orm.hibernate3.HibernateCallback;import com.amg.dao.IRecordDao;import com.amg.dao.base.AGenericHibernateDao;import com.amg.po.Record;public class RecordDaoImpl extends AGenericHibernateDao<Record, Integer>implements IRecordDao {public List<?> findBySql_totalTypeCount(final String sql, final Class clas) {return super.getHibernateTemplate().execute(new HibernateCallback<List<?>>() {public List<?> doInHibernate(Session session)throws HibernateException, SQLException {return session.createSQLQuery(sql)//.addScalar("createTime", Hibernate.STRING).addScalar("typeCount", Hibernate.INTEGER)//.addScalar("structedCount", Hibernate.INTEGER).setResultTransformer(Transformers.aliasToBean(clas)).list();}});}public List<?> find2VoBySql(String sql, Class cls) {// TODO Auto-generated method stubreturn null;}public List<?> findBySql_totalStrutedCount(final String sql, final Class clas) {return super.getHibernateTemplate().execute(new HibernateCallback<List<?>>() {public List<?> doInHibernate(Session session)throws HibernateException, SQLException {return session.createSQLQuery(sql)//.addScalar("createTime", Hibernate.STRING)//.addScalar("typeCount", Hibernate.INTEGER).addScalar("structedCount", Hibernate.INTEGER).setResultTransformer(Transformers.aliasToBean(clas)).list();}});}public List<?> findBySql_totalTCountDetail(final String sql, final Class clas) {return super.getHibernateTemplate().execute(new HibernateCallback<List<?>>() {public List<?> doInHibernate(Session session)throws HibernateException, SQLException {return session.createSQLQuery(sql).addScalar("createTime", Hibernate.STRING).addScalar("baseUrlCount", Hibernate.INTEGER)//.addScalar("structedUrlCount", Hibernate.INTEGER).setResultTransformer(Transformers.aliasToBean(clas)).list();}});}public List<?> findBySql_totalSCountDetail(final String sql, final Class clas) {return super.getHibernateTemplate().execute(new HibernateCallback<List<?>>() {public List<?> doInHibernate(Session session)throws HibernateException, SQLException {return session.createSQLQuery(sql).addScalar("createTime", Hibernate.STRING)//.addScalar("baseUrlCount", Hibernate.INTEGER).addScalar("structedUrlCount",Hibernate.INTEGER).setResultTransformer(Transformers.aliasToBean(clas)).list();}});}public List<?> findBySql_totalTypeCountByDay(final String sql, final Class clas) {// TODO Auto-generated method stubreturn super.getHibernateTemplate().execute(new HibernateCallback<List<?>>() {public List<?> doInHibernate(Session session)throws HibernateException, SQLException {return session.createSQLQuery(sql).addScalar("createTime", Hibernate.STRING).addScalar("typeCount", Hibernate.INTEGER)//.addScalar("structedCount", Hibernate.INTEGER).setResultTransformer(Transformers.aliasToBean(clas)).list();}});}}
?IRecordBiz.java
package com.amg.biz;import java.util.List;import com.amg.po.Record;import com.amg.vo.PageBean;public interface IRecordBiz {public abstract int findByCount(String strHQL);public abstract PageBean findByPage(final int currentPage, final int pageSize, final String strHQL, final Object[] params);public abstract void addRecord(final Record record);public abstract void deleteRecord(final int id);public abstract Record findById(final int id);public abstract void updateRecord(final Record record);public abstract List<Record> findAll();public abstract List<java.util.Map> findByHQL(final String strHQL, final Object[] params);public abstract List<Record> findBySQL(final String strHQL, final Object[] params);public abstract List<Record> findByHQL_Old(final String strHQL, final Object[] params);public List<?> findBySql_totalTypeCount(String sql,Class clas);public List<?> findBySql_totalStrutedCount(String sql,Class clas);public List<?> findBySql_totalTCountDetail(String sql,Class clas);public List<?> findBySql_totalSCountDetail(String sql,Class clas);public List<?> find2VoBySql(String sql,Class clas);public List<?> findBySql_totalTypeCountByDay(String sql, Class clas);}
?RecordBizImpl.java
package com.amg.biz.impl;import java.util.List;import com.amg.biz.IRecordBiz;import com.amg.dao.IRecordDao;import com.amg.po.Record;import com.amg.vo.PageBean;public class RecordBizImpl implements IRecordBiz {private IRecordDao recordDao;public void setRecordDao(IRecordDao RecordDao) {this.recordDao = RecordDao;}public PageBean findByPage(final int currentPage, final int pageSize,final String strHQL, final Object[] params) {// String strHQL = "select d from Record as d order by d.Recordno";// Object[] params = new Object[]{};return this.recordDao.findByPage(currentPage, pageSize, strHQL, params);}public void addRecord(Record Record) {this.recordDao.create(Record);}public void deleteRecord(int Recordno) {this.recordDao.delete(Recordno);}public void updateRecord(Record Record) {this.recordDao.update(Record);}public Record findById(int Recordno) {return this.recordDao.findById(Recordno);}public List<Record> findAll() {return this.recordDao.findAll();}public List<java.util.Map> findByHQL(String strHQL, Object[] params) {return this.recordDao.findByHQL(strHQL, params);}public int findByCount(String strHQL) {return this.recordDao.findByCount(strHQL);}public List<Record> findByHQL_Old(String strHQL, Object[] params) {return this.recordDao.findByHQL_Old(strHQL, params);}public List<Record> findBySQL(String strSQL, Object[] params) {return this.recordDao.findBySQL(strSQL, params);}public List<?> findBySql_totalTypeCount(String sql, Class clas) {return this.recordDao.findBySql_totalTypeCount(sql, clas);}public List<?> find2VoBySql(String sql, Class clas) {return this.recordDao.find2VoBySql(sql, clas);}public List<?> findBySql_totalStrutedCount(String sql, Class clas) {return this.recordDao.findBySql_totalStrutedCount(sql, clas);}public List<?> findBySql_totalTCountDetail(String sql, Class clas) {// TODO Auto-generated method stubreturn this.recordDao.findBySql_totalTCountDetail(sql, clas);}public List<?> findBySql_totalSCountDetail(String sql, Class clas) {return this.recordDao.findBySql_totalSCountDetail(sql, clas);}public List<?> findBySql_totalTypeCountByDay(String sql, Class clas) {// TODO Auto-generated method stubreturn this.recordDao.findBySql_totalTypeCountByDay(sql, clas);}}
?