版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度建筑工程施工安全責(zé)任承諾書3篇
- 二零二五年度品牌發(fā)布會(huì)晚會(huì)舞臺(tái)建設(shè)及現(xiàn)場(chǎng)表演合同3篇
- 二零二五年度企業(yè)間短期借款協(xié)議書模板3篇
- 二零二五版塔吊設(shè)備租賃與安裝技術(shù)支持合同3篇
- 二零二五年度個(gè)人投資貸款合同范本及風(fēng)險(xiǎn)評(píng)估3篇
- 二零二五年度高端別墅裝修質(zhì)量保證及維護(hù)服務(wù)協(xié)議2篇
- 質(zhì)量管理自查報(bào)告5篇
- 舊建筑物拆除工程施工合同
- 工程業(yè)務(wù)提成協(xié)議書
- 廣州市房屋租賃管理委托授權(quán)合同
- 2025貴州貴陽市屬事業(yè)單位招聘筆試和高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2024年住院醫(yī)師規(guī)范化培訓(xùn)師資培訓(xùn)理論考試試題
- 期末綜合測(cè)試卷(試題)-2024-2025學(xué)年五年級(jí)上冊(cè)數(shù)學(xué)人教版
- 招標(biāo)采購基礎(chǔ)知識(shí)培訓(xùn)
- 2024年廣東省公務(wù)員錄用考試《行測(cè)》試題及答案解析
- 電力系統(tǒng)分布式模型預(yù)測(cè)控制方法綜述與展望
- 五年級(jí)口算題卡每天100題帶答案
- 結(jié)構(gòu)力學(xué)本構(gòu)模型:斷裂力學(xué)模型:斷裂力學(xué)實(shí)驗(yàn)技術(shù)教程
- 2024年貴州省中考理科綜合試卷(含答案)
- 無人機(jī)技術(shù)與遙感
- 恩施自治州建始東升煤礦有限責(zé)任公司東升煤礦礦產(chǎn)資源開發(fā)利用與生態(tài)復(fù)綠方案
評(píng)論
0/150
提交評(píng)論