使用Spring 來配置 hibernate 對應檔的 兩種方法

來源:互聯網
上載者:User

在spring 中 配置 hibernate 映射 中 可以通過以下 兩種方法來實現

 

1:當 需要的對應檔比較少的時候,可以通過 mappingResources 來實現

 

具體實現如下:

 

<property name="mappingResources"><br /> <list><br /><value>com/yourcompany/model/Teacher.hbm.xml</value><br /><value>com/yourcompany/model/Student.hbm.xml</value><br /><value>com/yourcompany/model/School.hbm.xml</value><br /> </list><br /></property>

 

主要就是 在<list></list> 中 用<value> </value>來羅列出來你的 對應檔所在的位置和名稱;

 

這種實現的優點:比較直觀,易於理解;弊端就在於:如果對應檔過多的話,一是重複代碼過多,二是如果有寫錯的地方則要費力去找。

 

所以我們就可以採用下面的方法,只給 spring 一個檔案夾,其他的事情,就讓spring 來做。

 

2:當 需要映射的檔案過多時,我們可以通過:mappingDirectoryLocations 來實現

 

具體如下:

可以通過classpath來指定 或者將 classpath:/com/yourcompany 改為 檔案夾的位置: 如 WEB-INF/modelMappings

 

<property name="mappingDirectoryLocations"><br /> <list><br /> <value>classpath:/com/yourcompany/model/</value><br /> </list><br /></property>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.