JSF+Spring+Hibernate+范型DAO(2)

DAO层:主要使用Hibernate实现持久化等相关操作。本着基类足够强大,最好可以包罗万象,避免重发开发Dao的原则,使用范型DAO来处理对数据库进行重复性的80%增删改查操作。
参考代码:

public interface IGenericDao<T, ID extends Serializable> {

	public void saveOrUpdate(T t);

	public T load(Serializable ID);

	public T get(Serializable ID);
	
	public Object get(Class TClass, Serializable ID, LockMode lockMode);

	public boolean contains(T t) throws DataAccessException;

	public void delete(T t, LockMode lockMode) throws DataAccessException;

	public void delete(T t) throws DataAccessException;

	public void deleteAll(Collection<T> entities) throws DataAccessException;

	public List<T> find(String queryString, Object value)
			throws DataAccessException;

	public List<T> find(String queryString, Object[] values)
			throws DataAccessException;

	public List<T> find(String queryString) throws DataAccessException;

	public List<T> findByExample(Object exampleEntity, int firstResult,
			int maxResults) throws DataAccessException;

	public List<T> findByExample(Object exampleEntity)
			throws DataAccessException;

	public List<T> findByNamedParam(String queryString, String paramName,
			Object value) throws DataAccessException;

	public List<T> findByNamedParam(String queryString, String[] paramNames,
			Object[] values) throws DataAccessException;

	public Object load(Class TClass, Serializable ID, LockMode lockMode)
			throws DataAccessException;

	public void load(T t, Serializable ID) throws DataAccessException;

	public Object load(String TName, Serializable ID, LockMode lockMode)
			throws DataAccessException;

	public Object load(String TName, Serializable ID)
			throws DataAccessException;

	public void refresh(T t, LockMode lockMode) throws DataAccessException;

	public void refresh(T t) throws DataAccessException;

	public Serializable save(T t) throws DataAccessException;

	public void saveOrUpdate(String TName, T t) throws DataAccessException;

	public void saveOrUpdateAll(Collection<T> entities)
			throws DataAccessException;

	public void update(T t, LockMode lockMode) throws DataAccessException;

	public void update(T t) throws DataAccessException;

	public void update(String TName, T t, LockMode lockMode)
			throws DataAccessException;

	public void update(String TName, T t) throws DataAccessException;

	public List<T> loadAll();
	
	public List<T> loadAll(Class TClass);

	public List<T> list();
	
	public List<T> list(Class TClass);

	public PaginationSupport findPageByCriteria(
			final DetachedCriteria detachedCriteria, final int pageSize,
			final int startIndex);

	public PaginationSupport findPageByQuery(final String hsql,
			final int pageSize, final int startIndex);

}

 

@SuppressWarnings("unchecked")
public class GenericDao<T, ID extends Serializable> extends HibernateDaoSupport	implements IGenericDao<T, ID> {
	protected Log logger = LogFactory.getLog(getClass());

	protected Class<T> entityClass;

	public GenericDao() {
		
	}
	protected Class getEntityClass() {
		if (entityClass == null) {
			entityClass = (Class<T>) ((ParameterizedType) getClass()
					.getGenericSuperclass()).getActualTypeArguments()[0];
			logger.debug("T class = " + entityClass.getName());
		}
		return entityClass;
	}

	public void saveOrUpdate(T t) {
		this.getHibernateTemplate().saveOrUpdate(t);
	}

	public T load(Serializable ID) {

		T load = (T) getHibernateTemplate().load(getEntityClass(), ID);
		return load;
	}

	public T get(Serializable ID) {
		T load = (T) getHibernateTemplate().get(getEntityClass(), ID);
		return load;
	}
	
	public Object get(Class TClass,Serializable ID, LockMode lockMode) {
		Object vo = getHibernateTemplate().get(TClass, ID, lockMode);
		return vo;
	}

	public boolean contains(T t) throws DataAccessException {
		return getHibernateTemplate().contains(t);
	}

	public void delete(T t, LockMode lockMode) throws DataAccessException {
		getHibernateTemplate().delete(t, lockMode);
	}

	public void delete(T t) throws DataAccessException {
		getHibernateTemplate().delete(t);
	
	}
	
	public void deleteAll(Collection<T> entities) throws DataAccessException {
		getHibernateTemplate().deleteAll(entities);
	}
	
	public List<T> find(String queryString, Object value)
			throws DataAccessException {
		List<T> find = (List<T>) getHibernateTemplate().find(queryString,
				value);
		return find;
	}

	public List<T> find(String queryString, Object[] values)
			throws DataAccessException {
		List<T> find = (List<T>) getHibernateTemplate().find(queryString,
				values);
		return find;
	}

	public List<T> find(String queryString) throws DataAccessException {
		return (List<T>) getHibernateTemplate().find(queryString);
	}

	public List<T> findByExample(Object exampleEntity, int firstResult,
			int maxResults) throws DataAccessException {
		return getHibernateTemplate().findByExample(exampleEntity, firstResult,
				maxResults);
	}

	public List<T> findByExample(Object exampleEntity) throws DataAccessException {
		return getHibernateTemplate().findByExample(exampleEntity);
	}

	public List<T> findByNamedParam(String queryString, String paramName,
			Object value) throws DataAccessException {
		return getHibernateTemplate().findByNamedParam(queryString, paramName,
				value);
	}

	public List<T> findByNamedParam(String queryString, String[] paramNames,
			Object[] values) throws DataAccessException {
		return getHibernateTemplate().findByNamedParam(queryString, paramNames,
				values);
	}

	public Object load(Class TClass, Serializable ID, LockMode lockMode)
			throws DataAccessException {
		return getHibernateTemplate().load(TClass, ID, lockMode);
	}

	public void load(T t, Serializable ID) throws DataAccessException {
		getHibernateTemplate().load(t, ID);
	}

	public Object load(String TName, Serializable ID, LockMode lockMode)
			throws DataAccessException {
		return getHibernateTemplate().load(TName, ID, lockMode);
	}

	public Object load(String TName, Serializable ID)
			throws DataAccessException {
		return getHibernateTemplate().load(TName, ID);
	}
	
	public Object load(Class TClass, Serializable ID)
			throws DataAccessException {
		return getHibernateTemplate().load(TClass, ID);
	}

	public void refresh(T t, LockMode lockMode) throws DataAccessException {
		getHibernateTemplate().refresh(t, lockMode);
	}

	public void refresh(T t) throws DataAccessException {
		getHibernateTemplate().refresh(t);
	}

	public Serializable save(T t) throws DataAccessException {
		return getHibernateTemplate().save(t);
	}

	public void saveOrUpdate(String TName, T t) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(TName, t);
	}

	public void saveOrUpdateAll(Collection<T> entities)
			throws DataAccessException {
		getHibernateTemplate().saveOrUpdateAll(entities);
	}

	public void update(T t, LockMode lockMode) throws DataAccessException {
		getHibernateTemplate().update(t, lockMode);
	}

	public void update(T t) throws DataAccessException {
		getHibernateTemplate().update(t);
	}

	public void update(String TName, T t, LockMode lockMode)
			throws DataAccessException {
		getHibernateTemplate().update(TName, t, lockMode);
	}

	public void update(String TName, T t) throws DataAccessException {
		getHibernateTemplate().update(TName, t);
	}

	public List<T> loadAll() {
		return getHibernateTemplate().loadAll(getEntityClass());
	}

	public List<T> loadAll(Class TClass) {
		return getHibernateTemplate().loadAll(TClass);
	}

	public List<T> list() {
		Criteria criteria = getSession().createCriteria(getEntityClass());
		return criteria.list();
	}

	public List<T> list(Class TClass) {
		Criteria criteria = getSession().createCriteria(TClass);
		return criteria.list();
	}

	public PaginationSupport findPageByCriteria(
			final DetachedCriteria detachedCriteria, final int pageSize,
			final int startIndex) {
		return (PaginationSupport) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = detachedCriteria
								.getExecutableCriteria(session);
						int totalCount = ((Integer) criteria.setProjection(
								Projections.rowCount()).uniqueResult())
								.intValue();
						criteria.setProjection(null);
						List items = criteria.setFirstResult(startIndex)
								.setMaxResults(pageSize).list();
						PaginationSupport ps = new PaginationSupport(items,
								totalCount, pageSize, startIndex);
						return ps;
					}
				}, true);
	}

	public PaginationSupport findPageByQuery(final String hsql,
			final int pageSize, final int startIndex) {
		return (PaginationSupport) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hsql);
						int totalCount = query.list().size();
						query.setFirstResult(startIndex);
						query.setMaxResults(pageSize);
						List items = query.list();
						PaginationSupport ps = new PaginationSupport(items,
								totalCount, pageSize, startIndex);
						return ps;

					}
				}, true);
	}

 VO: 主要使用hibernateSynchronizer工具自动生成,然后根据Hibernate Annotation进行修改。
样例代码:

public class WorklistSetPKVO implements Serializable {

	protected int hashCode = Integer.MIN_VALUE;

	private java.lang.Integer processstatus;
	private java.lang.Integer key;


	public WorklistSetPKVO () {}
	
	public WorklistSetPKVO (
		java.lang.Integer processstatus,
		java.lang.Integer key) {

		this.setProcessstatus(processstatus);
		this.setKey(key);
	}


	/**
	 * Return the value associated with the column: PROCESSSTATUS
	 */
	public java.lang.Integer getProcessstatus () {
		return processstatus;
	}

	/**
	 * Set the value related to the column: PROCESSSTATUS
	 * @param processstatus the PROCESSSTATUS value
	 */
	public void setProcessstatus (java.lang.Integer processstatus) {
		this.processstatus = processstatus;
	}



	/**
	 * Return the value associated with the column: KEY
	 */
	public java.lang.Integer getKey () {
		return key;
	}

	/**
	 * Set the value related to the column: KEY
	 * @param key the KEY value
	 */
	public void setKey (java.lang.Integer key) {
		this.key = key;
	}




	public boolean equals (Object obj) {
		if (null == obj) return false;
		if (!(obj instanceof WorklistSetPKVO)) return false;
		else {
			WorklistSetPKVO mObj = (WorklistSetPKVO) obj;
			if (null != this.getProcessstatus() && null != mObj.getProcessstatus()) {
				if (!this.getProcessstatus().equals(mObj.getProcessstatus())) {
					return false;
				}
			}
			else {
				return false;
			}
			if (null != this.getKey() && null != mObj.getKey()) {
				if (!this.getKey().equals(mObj.getKey())) {
					return false;
				}
			}
			else {
				return false;
			}
			return true;
		}
	}

	public int hashCode () {
		if (Integer.MIN_VALUE == this.hashCode) {
			StringBuilder sb = new StringBuilder();
			if (null != this.getProcessstatus()) {
				sb.append(this.getProcessstatus().hashCode());
				sb.append(":");
			}
			else {
				return super.hashCode();
			}
			if (null != this.getKey()) {
				sb.append(this.getKey().hashCode());
				sb.append(":");
			}
			else {
				return super.hashCode();
			}
			this.hashCode = sb.toString().hashCode();
		}
		return this.hashCode;
	}
}

 

@Entity
@Table(name="")
public class WorklistSetVO  implements Serializable {


	// constructors
	public WorklistSetVO () {
		initialize();
	}

	/**
	 * Constructor for primary key
	 */
	public WorklistSetVO (WorklistSetPKVO id) {
		this.setId(id);
		initialize();
	}

	protected void initialize () {}



	private int hashCode = Integer.MIN_VALUE;

	// primary key
	private WorklistSetPKVO id;

	// fields
	private java.lang.Integer re;
	private java.lang.Integer upl;
	private java.lang.Integer co;
	private java.lang.Integer lscco;
	private java.lang.Integer lscnonco;



	/**
	 * Return the unique identifier of this class
     * @hibernate.id
     */
	@EmbeddedId
	public WorklistSetPKVO getId () {
		return id;
	}

	/**
	 * Set the unique identifier of this class
	 * @param id the new ID
	 */
	public void setId (WorklistSetPKVO id) {
		this.id = id;
		this.hashCode = Integer.MIN_VALUE;
	}




	/**
	 * Return the value associated with the column: RE
	 */
	public java.lang.Integer getRe () {
		return re;
	}

	/**
	 * Set the value related to the column: RE
	 * @param re the RE value
	 */
	public void setRe (java.lang.Integer re) {
		this.re = re;
	}



	/**
	 * Return the value associated with the column: UPL
	 */
	public java.lang.Integer getUpl () {
		return upl;
	}

	/**
	 * Set the value related to the column: UPL
	 * @param upl the UPL value
	 */
	public void setUpl (java.lang.Integer upl) {
		this.upl = upl;
	}



	/**
	 * Return the value associated with the column: CO
	 */
	public java.lang.Integer getCo () {
		return co;
	}

	/**
	 * Set the value related to the column: CO
	 * @param co the CO value
	 */
	public void setCo (java.lang.Integer co) {
		this.co = co;
	}



	/**
	 * Return the value associated with the column: LSCCO
	 */
	public java.lang.Integer getLscco () {
		return lscco;
	}

	/**
	 * Set the value related to the column: LSCCO
	 * @param lscco the LSCCO value
	 */
	public void setLscco (java.lang.Integer lscco) {
		this.lscco = lscco;
	}



	/**
	 * Return the value associated with the column: LSCNONCO
	 */
	public java.lang.Integer getLscnonco () {
		return lscnonco;
	}

	/**
	 * Set the value related to the column: LSCNONCO
	 * @param lscnonco the LSCNONCO value
	 */
	public void setLscnonco (java.lang.Integer lscnonco) {
		this.lscnonco = lscnonco;
	}




	public boolean equals (Object obj) {
		if (null == obj) return false;
		if (!(obj instanceof WorklistSetVO)) return false;
		else {
			WorklistSetVO worklistSetVO = (WorklistSetVO) obj;
			if (null == this.getId() || null == worklistSetVO.getId()) return false;
			else return (this.getId().equals(worklistSetVO.getId()));
		}
	}

	public int hashCode () {
		if (Integer.MIN_VALUE == this.hashCode) {
			if (null == this.getId()) return super.hashCode();
			else {
				String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();
				this.hashCode = hashStr.hashCode();
			}
		}
		return this.hashCode;
	}


	public String toString () {
		return super.toString();
	}


}

 


大体结构即是如此。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值