Spring 整合JDBC,s2sh集成DAO介绍

本文详细介绍了如何使用Spring和Hibernate实现对TEmp实体的增删查改等操作,包括使用Hibernate进行数据库查询、实体管理以及事务处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值