1、hibernate连接信息需要整合到spring配置文件中,不要用单独的hibernate配置文件 2、使用myeclipse整合spring与hibernate4,spring配置文件默认生存事物管理类bean与事务注解扫描,推荐使用注解声明事物
class=\"org.springframework.orm.hibernate4.HibernateTransactionManager\">
3、Spring与hibernate3整合时可以使用的hibernateTemplater类,在整合hibernate4中无法再使用,上网查了一下,主要原因是spring认为hibernate4的代码写的足够好了,不用自己再写一个支持类了。所以就建议使用hibernate4的hibernateDAO了。
4、整合时获取session不再使用openSession()的方法来打开session,而是使用getCurrentSession()方法来获取session
5、那么配置要保证能够从线程中获取session,需要配置openSessionInView过滤器,spring自带了此过滤器,直接配置即可
openSession
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSession /*
6、在实现类中如果要操作数据库,还是使用hibernate的的api操作 7、一般在实现了创建sessionFactory属性,并通过spring注入 8、在实际编程中可自行创建工具类方便操作
publicclass HibernateSessionFactory {
private SessionFactory sessionFactory;
public Session getSession(){
return getSessionFactory().getCurrentSession(); }
public SessionFactory getSessionFactory() { returnsessionFactory; }
publicvoid setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }
}
该工具类还可添加常用的数据库操作方法。例如增删查改等。类似于自定义一个hiberntaTemplater类。