使用spring整合hibernate和struts

使用spring整合hibernate和struts

ID:33636250

大小:109.50 KB

页数:9页

时间:2019-02-27

使用spring整合hibernate和struts_第1页
使用spring整合hibernate和struts_第2页
使用spring整合hibernate和struts_第3页
使用spring整合hibernate和struts_第4页
使用spring整合hibernate和struts_第5页
资源描述:

《使用spring整合hibernate和struts》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、使用Spring整合Hibernate和Struts21、Spring和Hibernate的整合(DAO层整合)1.1由Spring的IoC容器创建sessionFactory注意:Øid名称任意,但

2、是建议为sessionFactory。Øproperty的name属性值固定Ø不再使用HibernateSessionFactory类获取SessionFactory,而是从Spring的Ioc容器获取1.2由Spring的IoC容器创建DAO

3、bean>DAO实现类的开发有两种方法。无论哪种方法,都需要在配置文件中对DAO进行以上相同配置方法1:使用原生API实现DaopublicclassStudentDaoImplimplementsStudentDao{privateSessionFactorysessionFactory;publicvoidsetSessionFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}publicListselectAll(){//Sessionsession=Hibernate

4、SessionFactory.getSession();Sessionsession=this.sessionFactory.openSession();Queryquery=session.createQuery("fromStudent");ListstuList=query.list();session.close();returnstuList;}}优点:在开发中不会使用到Spirng的任何类,就像没有Spring一样,便于日后移植,脱离Spring。缺点:无法利用Spring封装Hibernate时提供的额外功能,例如开发人员还必须进行异常处理。方法2:使用S

5、pring的HibernateDaoSupport类实现Dao。publicclassStudentDaoImplextendsHibernateDaoSupportimplementsStudentDao{publicListselectAll(){ListstuList=this.getHibernateTemplate().find("fromStudent");returnstuList;}publicvoidinsertStu(Studentstudent){this.getHibernateTemplate().save(

6、student);}publicStudentselectStu(intid){Studentstu=(Student)this.getHibernateTemplate().get(Student.class,id);returnstu;}publicvoidupdateStu(Studentstudent){this.getHibernateTemplate().update(student);}publicvoiddeleteStu(intid){this.getHibernateTemplate().delete(this.getHibernateTemplat

7、e().get(Student.class,id));}}缺点:在开发中会使用到Spring的类,对Spring产生依赖,不利于日后移植,脱离Spring。优点:可以利用Spring封装Hibernate时提供的额外功能,例如开发人员无需进行异常处理,无需开启和关闭Session,大大减少了编码量。。深入HibernateTemplate:Ø方法和Sessioin的许多方法相同,底层调用Session的相应方法ØHibernateTemplate类的核心方法是doExecute(),在其中提供了静态操作的实现(模板),比如获取sessio

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。