import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.svse.entity.TEmp;
/**
* S2SH 集成中的DAO 方法详细介绍
*
* @see com.svse.entity.TEmp
* @author MyEclipse Persistence Tools
*/
public class TEmpDAO extends HibernateDaoSupport {
private static final Logger log = LoggerFactory.getLogger(TEmpDAO.class);
// property constants
public static final String _ENAME = "EName";
public static final String _ESEX = "ESex";
protected void initDao() {
// do nothing
}
/****************************** 自定义方法,根据需求编写对应的方法 ********************************************/
// 专用查询结果集的 , 使用execiteFind();方法查询
public List getAll() {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
//返回需要对应的对象
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
//此时有session ,方便操作,可以使用HQL 查询,也可以使用QBC 查询
List ar = session.createCriteria(TEmp.class).createCriteria("TDept").list();
return ar;
}
});
}
//使用find方法查询
public List testFind(int id,String name){
return this.getHibernateTemplate().find("From TEmp a inner join fetch a.TDept where a.EId>? and a.EName like ?",
new Object[]{id,'%'+name});
}
//等等,可以根据需要写各种方法
/*************************************** 下面的方法都是系统自动生成的 **********************************/
// 增加
public void save(TEmp transientInstance) {
log.debug("saving TEmp instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
// 删除
public void delete(TEmp persistentInstance) {
log.debug("deleting TEmp instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
// 根据ID 查一个对象
public TEmp findById(java.lang.Integer id) {
log.debug("getting TEmp instance with id: " + id);
try {
TEmp instance = (TEmp) getHibernateTemplate().get(
"com.svse.entity.TEmp", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
// 根据对象查
public List findByExample(TEmp instance) {
log.debug("finding TEmp instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
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(String propertyName, Object value) {
log.debug("finding TEmp instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from TEmp as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
// 根据名称
public List findByEName(Object EName) {
return findByProperty(_ENAME, EName);
}
public List findByESex(Object ESex) {
return findByProperty(_ESEX, ESex);
}
// 查询所有
public List findAll() {
log.debug("finding all TEmp instances");
try {
String queryString = "from TEmp a inner join fetch a.TDept";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
// 修改
public TEmp merge(TEmp detachedInstance) {
log.debug("merging TEmp instance");
try {
TEmp result = (TEmp) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(TEmp instance) {
log.debug("attaching dirty TEmp instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(TEmp instance) {
log.debug("attaching clean TEmp instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
// 获取当前DAO 对象
public static TEmpDAO getFromApplicationContext(ApplicationContext ctx) {
return (TEmpDAO) ctx.getBean("TEmpDAO");
}
}
Spring 整合JDBC,s2sh集成DAO介绍
最新推荐文章于 2025-07-08 11:49:15 发布