自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析Hibernate加載配置文件

開發(fā) 后端
這里介紹Hibernate加載配置文件Hibernate.properties和Hibernate.cfg.xml的過程,以及怎么樣將Hibernate提供的配置文件的訪問方法靈活運(yùn)用到單元測試中。

在向大家詳細(xì)介紹Hibernate加載配置文件之前,首先讓大家了解下使用Hibernate的經(jīng)驗(yàn),然后全面介紹Hibernate加載配置文件。

Hibernate是一個(gè)流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認(rèn)同,在采用了Hibernate的項(xiàng)目中如何保證測試的自動(dòng)化和持續(xù)性呢?本文討論了Hibernate加載配置文件Hibernate.properties和Hibernate.cfg.xml的過程,以及怎么樣將Hibernate提供的配置文件的訪問方法靈活運(yùn)用到單元測試中。

注意:本文以Hibernate2.1作為討論的基礎(chǔ),不保證本文的觀點(diǎn)適合于其他版本。

對于Hibernate的初學(xué)者來說,***次使用Hibernate的經(jīng)驗(yàn)通常是:

1.安裝配置好Hibernate,我們后面將%Hibernate_HOME%作為對Hibernate安裝目錄的引用,

2.開始創(chuàng)建好自己的***個(gè)例子,例如Hibernate手冊里面的類Cat,

3.配置好hbm映射文件(例如Cat.hbm.xml,本文不討論這個(gè)文件內(nèi)配置項(xiàng)的含義)和數(shù)據(jù)庫(如hsqldb),

4.在項(xiàng)目的classpath路徑下添加一個(gè)Hibernate.cfg.xml文件,如下(***次使用Hibernate最常見的配置內(nèi)容):

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-configuration  
  3. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> 
  5.  
  6. <hibernate-configuration> 
  7. <session-factory> 
  8.  
  9. <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> 
  10. <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
  11. <property name="connection.username">sa</property> 
  12. <property name="connection.password"></property> 
  13. <property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property> 
  14.  
  15. <property name="hibernate.show_sql">false</property> 
  16.  
  17. <mapping resource="Cat.hbm.xml"/> 
  18.  
  19. </session-factory> 
  20. </hibernate-configuration> 

5.然后還需要提供一個(gè)類來測試一下創(chuàng)建,更新,刪除和查詢Cat,對于熟悉JUnit的開發(fā)人員,可以創(chuàng)建一個(gè)單元測試類來進(jìn)行測試,如下:

  1. import junit.framework.TestCase;  
  2. import net.sf.hibernate.HibernateException;  
  3. import net.sf.hibernate.Session;  
  4. import net.sf.hibernate.Transaction;  
  5. import net.sf.hibernate.cfg.Configuration;  
  6.  
  7.  
  8. public class CatTest extends TestCase {  
  9.  
  10. private Session session;  
  11. private Transaction tx;  
  12.  
  13. protected void setUp() throws Exception {  
  14. Configuration cfg = new Configuration().configure();
  15. //注意這一行,這是本文重點(diǎn)討論研究的地方。  
  16. session = cfg.buildSessionFactory().openSession();  
  17. tx = session.beginTransaction();  
  18. }  
  19.  
  20. protected void tearDown() throws Exception {  
  21. tx.commit();  
  22. session.close();  
  23. }  
  24.  
  25. public void testCreate() {  
  26. //請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。  
  27. }  
  28. public void testUpdate() {  
  29. //請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。  
  30. }  
  31. public void testDelete() {  
  32. //請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。  
  33. }  
  34. public void testQuery() {  
  35. //請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。  
  36. }  
  37.  

以上介紹Hibernate加載配置文件。

【編輯推薦】

  1. Hibernate類庫簡單描述
  2. 淺析Hibernate延遲加載
  3. Spring Hibernate簡單討論
  4. 介紹Hibernate版本的更新
  5. 簡單講述Hibernate實(shí)例
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-09-22 10:23:15

Hibernate配置

2009-09-24 11:41:46

Hibernate延遲

2009-09-29 15:52:26

Hibernate X

2009-09-29 17:29:43

Hibernate S

2009-09-27 13:25:22

2009-06-17 14:55:26

Hibernate數(shù)據(jù)

2009-08-13 09:16:57

C#讀取配置文件

2012-02-06 13:34:49

HibernateJava

2009-07-21 10:05:10

ASP.NET配置文件

2022-04-20 20:27:51

Hydra配置文件開發(fā)工具

2009-09-24 10:07:21

Hibernate M

2009-09-22 14:44:18

Hibernate.c

2009-09-28 09:35:10

Hibernate實(shí)現(xiàn)實(shí)體對象延遲加載

2009-08-05 10:57:17

ASP.NET配置文件配置文件格式

2009-06-23 18:19:32

單元測試Hibernate配置

2009-06-02 14:12:26

Hibernate配置文件格式

2010-02-02 18:19:52

Linux mplay

2009-09-28 17:23:51

Hibernate E

2009-08-05 11:16:26

ASP.NET配置文件

2024-04-23 14:13:38

開發(fā)配置文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號