首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring 与hibernate Annotation配备(dao)

2012-10-11 
spring 与hibernateAnnotation配置(dao)(1)BaseDao.javaimport java.io.Serializableimport java.sql.SQL

spring 与hibernate Annotation配置(dao)

(1)BaseDao.java

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDao extends HibernateDaoSupport {

?private static final Log log = LogFactory.getLog(BaseDao.class);
?private SessionFactory mySessionFactory;
?
?public SessionFactory getMySessionFactory() {
??return mySessionFactory;
?}
?@Autowired
?public void setMySessionFactory(SessionFactory mySessionFactory) {
??this.mySessionFactory = mySessionFactory;
?}
?
?@PostConstruct??
?private void setSuperSessionFactory(){//BaseDao被初始化里执行这个方法
??super.setSessionFactory(mySessionFactory);
?}

?/* @Resource(name="sessionFactory")
?private void setSuperSessionFactory(SessionFactory sessionFactory){
??this.setSessionFactory(sessionFactory);
?}
?*/
?public boolean createObj(Object o) {
??boolean rtn = false;
??log.debug("开始保存实例:"+o.getClass().getName());
??try{
???HibernateTemplate ht = this.getHibernateTemplate();
???ht.save(o);
???ht.flush();
???rtn = true;
???log.debug("保存实例["+o.getClass().getName()+"]成功!");
??}catch(HibernateException e){
???rtn = false;
???log.error("保存"+o.getClass().getName()+"实例到数据库失败!",e);
??}
??return rtn;
?}

}

?

(2)CompanyDao.java

?

import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.entry.TCompanyInfo;

?

@Repository("companyDao")
public class CompanyDao extends BaseDao {

?

}

热点排行