Hibernate通用Dao&Biz接口及实现类模板

本文介绍了一种通用的数据访问对象(DAO)设计方法,并详细展示了其在Hibernate框架下的具体实现,包括基本CRUD操作及复杂查询功能。此外,还提供了一个针对Record实体的具体DAO和业务逻辑接口及其实现。
摘要由CSDN通过智能技术生成

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 stub
		return 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 stub
		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();
					}
				});
	}

}

 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 stub
		return 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 stub
		return this.recordDao.findBySql_totalTypeCountByDay(sql, clas);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值