⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicdao.java

📁 这是本人在学习T4SH框架时
💻 JAVA
字号:
package com.zxx.common.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zxx.common.dao.IBaseHibernateDAO;

/**
 * Data access object (DAO) for domain model
 * 
 * @author MyEclipse - Hibernate Tools
 */
public class BasicDAO extends HibernateDaoSupport implements IBaseHibernateDAO {

	private static final Log log = LogFactory.getLog(BasicDAO.class);
	
	// property constants
	public static final String TYPE_NAME = "typeName";

	public static final String TYPE_DESCRIPTION = "typeDescription";

	public Serializable save(Object entityClass) {
		log.debug("saving " + entityClass.getClass().getName()
				+ " instance");
		Serializable id=null;
		try {
			id=this.getHibernateTemplate().save(entityClass);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			throw re;
		}
		return id;
	}

	
	public boolean edit(Object entityObj){
		log.debug("editing " + entityObj.getClass().getName()
				+ " instance");
		boolean bln = true;
		try{
			this.getHibernateTemplate().update(entityObj);
			log.debug("edit successful");
		}catch(RuntimeException re){
			log.error("edit failed", re);
			re.printStackTrace();
			bln = false;
			throw re;
		}
		return bln;
	}
	
	
	public boolean delete(Object entityObj) {
		log.debug("deleting " + entityObj.getClass().getName()
				+ " instance");
		boolean bln=false;
		try {
			this.getHibernateTemplate().delete(entityObj);
			bln=true;
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			re.printStackTrace();
			throw re;
		}
		return bln;
	}

	public Object findById(Class entityClass, Serializable id) {
		log.debug("getting " + entityClass.getName() 
				+ " instance with id: " + id);
		try {
			return this.getHibernateTemplate().get(entityClass, id);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findByExample(Object entityObj) {
		log.debug("finding " + entityObj.getClass().getName() + " instance by example");
		try {
			List results = getSession().createCriteria(entityObj.getClass())
					.add(Example.create(entityObj)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(Class entityClass, String propertyName,
			Object value) {
		log.debug("finding " + entityClass.getName()
				+ " instance with property: " + propertyName + ", value: "
				+ value);
		try {
			String queryString = "from " + entityClass.getName()
					+ " as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTypeName(Class entityClass, Object typeName) {
		return findByProperty(entityClass, TYPE_NAME, typeName);
	}

	public List findByTypeDescription(Class entityClass, Object typeDescription) {
		return findByProperty(entityClass, TYPE_DESCRIPTION, typeDescription);
	}

	public Object merge(Class entityClass) {
		log.debug("merging UserType instance");
		try {
			Object result = getSession().merge(entityClass);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Object instance) {
		log.debug("attaching dirty " + instance.getClass().getName()
				+ " instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Object instance) {
		log.debug("attaching clean " + instance.getClass().getName()
				+ " instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -