spring整合hibernate—xml篇和注釋篇_第1頁
spring整合hibernate—xml篇和注釋篇_第2頁
spring整合hibernate—xml篇和注釋篇_第3頁
spring整合hibernate—xml篇和注釋篇_第4頁
spring整合hibernate—xml篇和注釋篇_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、spring整合 hibernate xml 篇和注釋篇這里介紹一下spring 加載 sessionfactory 的這 2種方式1、通過配置datasource來配置sessionfactoryapplicationcontext.xml org.hibernate.dialect.mysqldialect true classpath:com/tukechina/mms/pojos 2、通過加載hibernate.cfg.xml來配置sessionfactoryapplicationcontext.xml hibernate.cfg.xml com.mysql.jdbc.driver 1

2、234 jdbc:mysql:/localhost/goodshool root org.hibernate.dialect.mysqldialect true 20 5 120 100 120 2 true 對于第二種配置方案,找到的資料很少,大多數(shù)采用第一種,其實(shí)還有一種較好的配置3、通過配置perties文件分離數(shù)據(jù)庫的配置mysqljdbc.driverclassname=com.mysql.jdbc.driver mysqljdbc.url=jdbc:mysql:/localhost/goodshool mysqljdbc.username=root mysqljdb

3、c.password=1234 # second cache statistics hibernate.generate_statistics=true # property that determines the hibernate dialect # (only applied with applicationcontext-hibernate.xml) hibernate.dialect=org.hibernate.dialect.mysqldialect hibernate.show_sql=true applicationcontext.xml $mysqljdbc.drivercl

4、assname $mysqljdbc.url $mysqljdbc.username $mysqljdbc.password 5 2 20 600 120 !- - $hibernate.generate_statistics $hibernate.dialect $hibernate.show_sql classpath:com/shangx/pojos - 下面是注釋篇,上面第3種方法的修改 $jdbc.driverclassname $jdbc.url $jdbc.username $jdbc.password $hibernate.generate_statistics org.hib

5、ernate.dialect.oracle9dialect true com.wf.pojo.rmenurole com.wf.pojo.rproduct com.wf.pojo.rholl com.wf.pojo.rcity com.wf.pojo.rorder com.wf.pojo.rprovince com.wf.pojo.rproductactivity com.wf.pojo.rmenu com.wf.pojo.rpstyle com.wf.pojo.rrole com.wf.pojo.rroleuser com.wf.pojo.ruser com.wf.pojo.ractivit

6、y 備注:注意紅色標(biāo)注部分在 spring+hibernate環(huán)境下使用hibernate annotation,其實(shí)主要的就是對hibernate 實(shí)體類的注解。這里要注意的一個(gè)問題是要使用包javax.persistence.entity;下面的注解,而不是 org.hibernate.annotation.entity; 接下來就是在spring 配置文件中配置,使其支持注解方式即可。 hibernate.cfg.xml 作為 hibernate 的配置文件另外在hibernate主鍵屬性定義時(shí),需要注意:今天用 hibernate操作 mysql 數(shù)據(jù)庫設(shè)置主鍵竟除了問題,主鍵設(shè)為uu

7、id.string,啟動(dòng)服務(wù)時(shí)報(bào)錯(cuò):could not interpret id generator strategy: uuid.string查了些資料最后得出結(jié)論:在hibernate2.1中,主鍵生成策略中uuid分為uuid.hex和uuid.string,但是從hibernate3.0開始已經(jīng)不再支持uuid.string正好在復(fù)習(xí)下主鍵策略了。(1) assigned 主鍵由外部程序負(fù)責(zé)生成,無需hibernate 參與。(2) hilo 通過 hi/lo 算法實(shí)現(xiàn)的主鍵生成機(jī)制,需要額外的數(shù)據(jù)庫表保存主鍵生成歷史狀態(tài)。(3) seqhilo 與 hilo 類似,通過 hi/lo

8、算法實(shí)現(xiàn)的主鍵生成機(jī)制,只是主鍵歷史狀態(tài)保存在sequence 中,適用于支持sequence 的數(shù)據(jù)庫,如oracle。(4) increment 主鍵按數(shù)值順序遞增。此方式的實(shí)現(xiàn)機(jī)制為在當(dāng)前應(yīng)用實(shí)例中維持一個(gè)變量,以保存著當(dāng)前的最大值,之后每次需要生成主鍵的時(shí)候?qū)⒋酥导?作為主鍵。這種方式可能產(chǎn)生的問題是:如果當(dāng)前有多個(gè)實(shí)例訪問同一個(gè)數(shù)據(jù)庫,那么由于各個(gè)實(shí)例各自維護(hù)主鍵狀態(tài),不同實(shí)例可能生成同樣的主鍵,從而造成主鍵重復(fù)異常。因此,如果同一數(shù)據(jù)庫有多個(gè)實(shí)例訪問,此方式必須避免使用。(5) identity 采用數(shù)據(jù)庫提供的主鍵生成機(jī)制。如db2、sql server 、mysql 中的主鍵生

9、成機(jī)制。(6) sequence 采用數(shù)據(jù)庫提供的sequence 機(jī)制生成主鍵。如oralce 中的 sequence 。(7) native 由 hibernate 根據(jù)底層數(shù)據(jù)庫自行判斷采用identity 、hilo、sequence 其中一種作為主鍵生成方式。(8) uuid.hex 由 hibernate基于 128 位唯一值產(chǎn)生算法生成16 進(jìn)制數(shù)值(編碼后以長度32 的字符串表示)作為主鍵。(9) uuid.string 與 uuid.hex 類似,只是生成的主鍵未進(jìn)行編碼(長度16) 。在某些數(shù)據(jù)庫中可能出現(xiàn)問題(如 postgresql ) 。(10) foreign 使用

10、外部表的字段作為主鍵。一般而言,利用uuid.hex 方式生成主鍵將提供最好的性能和數(shù)據(jù)庫平臺(tái)適應(yīng)性。另外由于常用的數(shù)據(jù)庫,如oracle、db2、sqlserver 、mysql 等,都提供了易用的主鍵生成機(jī)制( auto-increase 字段或者sequence ) 。我們可以在數(shù)據(jù)庫提供的主鍵生成機(jī)制上,采用 generator-class=native的主鍵生成方式。不過值得注意的是,一些數(shù)據(jù)庫提供的主鍵生成機(jī)制在效率上未必最佳,大量并發(fā)insert數(shù)據(jù)時(shí)可能會(huì)引起表之間的互鎖。數(shù)據(jù)庫提供的主鍵生成機(jī)制,往往是通過在一個(gè)內(nèi)部表中保存當(dāng)前主鍵狀態(tài)(如對于自增型主鍵而言, 此內(nèi)部表中就維護(hù)著當(dāng)前的最大值和遞增量),之

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論