SSH教學(xué)課件:C13 Spring與Struts、Hibernate的集成_第1頁
SSH教學(xué)課件:C13 Spring與Struts、Hibernate的集成_第2頁
SSH教學(xué)課件:C13 Spring與Struts、Hibernate的集成_第3頁
SSH教學(xué)課件:C13 Spring與Struts、Hibernate的集成_第4頁
SSH教學(xué)課件:C13 Spring與Struts、Hibernate的集成_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第十三章Spring與Struts,Hibernate的集成本章任務(wù)使用Spring依賴注入組裝后臺(tái)代碼給業(yè)務(wù)邏輯層添加事務(wù)支持

掌握Spring與Struts的集成掌握Spring與Hibernate的集成學(xué)會(huì)使用Spring實(shí)現(xiàn)聲明式事務(wù)本章目標(biāo)Spring與Hibernate集成使用Spring簡化Hibernate編程使現(xiàn)有JavaEE技術(shù)更易用Spring的目標(biāo)使用Hibernate的繁瑣步驟importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;publicclassStudentDAOextendsHibernateDaoSupport{publicvoidadd(StudentInfostudentInfo){super.getHibernateTemplate().add(studentInfo);}//...其他持久化方法的實(shí)現(xiàn)}使用Spring對(duì)Hibernate支持Spring與Hibernate集成使用Spring簡化Hibernate編程publicclassStudentDAOextendsHibernateDaoSupport{

publicList<StudentInfo>findAllStudent(){returngetSession().createCriteria(StudentInfo.class).list();

}}Spring與Hibernate集成Session在哪里創(chuàng)建?配置數(shù)據(jù)源和SessionFactory使用Spring依賴注入,只需配置,無需編碼StudentDAO中如何創(chuàng)建session的?HibernateDaoSupport提供了setSessionFactory方法Spring提供了LocalSessionFactoryBean用于創(chuàng)建SessionFactory,但需要通過setDataSource設(shè)置數(shù)據(jù)源數(shù)據(jù)源(DataSource)Spring與Hibernate集成依賴注入順序配置數(shù)據(jù)源<beanid="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close"><propertyname="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/><propertyname="url" value="jdbc:db2://localhost:50000/TOOLSDB"/><propertyname="username"value=“db2admin"/><propertyname="password"value=“admin"/></bean>數(shù)據(jù)庫連接信息dataSourcesessionFactoryStudentDAOServiceStudentActionSpring與Hibernate集成配置SessionFactory<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <propertyname="dataSource"> <refbean="dataSource"/> </property> <propertyname="configLocation"> <value>/WEB-INF/classes/hibernate.cfg.xml</value> </property></bean>注入dataSourceHibernate配置文件演示示例:Spring與Hibernate集成小結(jié)繼承自HibernateDaoSupport實(shí)現(xiàn)StudentDAO接口。publicinterfaceStudentDAO{ publicStudentInfogetById(Stringstuno); publicvoidadd(StudentInfostudentInfo); publicvoiddel(Stringstuno); publicvoidupdate(StudentInfostudentInfo); publicList<StudentInfo>getAll();}使用Spring重新組裝Web程序使用Spring管理依賴Web應(yīng)用中組件間的依賴關(guān)系組裝sessionFactory注入到DAO中DAO注入到Service中:首先增加setter方法<beanid=“studentDAO"class=“com.etp.hibernate.student.dao.StudentDAO"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid=“service" class="com.etp.hibernate.student.service.Service"><propertyname=“studentDao"ref=“studentDAO"/></bean>使用Spring重新組裝Web程序與Struts集成回顧:Struts機(jī)制我們用Spring創(chuàng)建Action,告訴Struts到Spring的Bean工廠中去取 ActionServlet請(qǐng)求ActionActionBean是由Struts創(chuàng)建的使用Spring重新組裝Web程序與Struts集成步驟1.在struts.xml配置插件<?xmlversion="1.0"encoding="GBK"?><struts> <constantname="struts.objectFactory"value="spring"></constant> <constantname="struts.objectFactory.spring.useClassCache“value="true"> </constant></struts>添加Spring支持使用Spring重新組裝Web程序與Struts集成步驟2.修改ActionBean配置<?xmlversion="1.0"encoding="GBK"?><struts>

<packagename="student"extends="struts-default"> <actionname="findAll"class="studentAction"method="findAllStudent"> <resultname="success">/Output.jsp</result> </action> </package></struts>使用Spring重新組裝Web程序與Struts集成步驟3.在Spring配置文件中配置ActionBean,將service注入<beanid="service"class="com.etp.hibernate.student.service.Service"> <propertyname="studentDao"> <refbean="studentDAO"/> </property></bean><beanid="studentAction"class="com.etp.hibernate

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論