搭建環境:
Eclipse3.4
Build Path Configeration: 建立 hibernate 庫,并加進外部類包:
基本的有 hibernate3 以及 hibernate-distribution-3.3.2.GA/lib/required 目錄下的全部類包
還有 hibernate-annotations , hibernate-commons-annotations , ejb3-persistence
日志系統的: slf4j-nop-1.5.11 和 slf4j-api-1.5.11
測試系統的: junit-4.8.2
Hibernate 可以通過 XML 解析或 annotations 标注對實體對象進行映射,實作用面向對象的方式操作資料庫。
Hibernate 基本配置:
Hibernate.cfg.xml 檔案:(注意該檔案最好不要私自改名,且要放在項目的根目錄下)
最主要的聲明部分:
< hibernate-configuration >
< session-factory >
<!-- Database connection settings -->
< property name = "connection.driver_class" >
com.microsoft.sqlserver.jdbc.SQLServerDriver
</ property >
< property name = "connection.url" >
jdbc:sqlserver://localhost :1433;databaseName=Test
</ property >
< property name = "connection.username" > username </ property >
< property name = "connection.password" > *************
</ property >
<!-- JDBC connection pool (use the built-in) -->
< property name = "connection.pool_size" > 1 </ property >
<!-- SQL dialect -->
< property name = "dialect" >
org.hibernate.dialect.SQLServerDialect
</ property >
<!-- Enable Hibernate's automatic session context management -->
< property name = "current_session_context_class" >
Thread
</ property >
<!-- Disable the second-level cache -->
< property name = "cache.provider_class" >
org.hibernate.cache.NoCacheProvider
</ property >
<!-- Echo all executed SQL to stdout -->
< property name = "show_sql" > true </ property >
<!-- Drop and re-create the database schema on startup -->
< property name = "hbm2ddl.auto" > update </ property >
<!—-format the sql-->
<property name="format_sql">true</property>
<!-- 實體映射對象 -->
< mapping resource = "com/Test/Demo.hbm.xml" />
< mapping class = "com.Test.Demo" />
<!-- 以上聲明為 XML 映射方式,下面為 annotations 映射方式的聲明 -->
< mapping class = "com.Test.T" />
</ session-factory >
</ hibernate-configuration >
XML 解析方式:
對象映射檔案 Demo.hbm.xml ,命名規則一般把被映射的類的類名作為最頂級的名再加上字尾 .hbm.xml , 并與被映射的類放在同一個包裡 。
< hibernate-mapping >
< class name = "com.Test.Demo" table = "[dbo].[T]" >
< id name = "id" column = "id" type = "java.lang.Integer" >
< generator class = "native" />
</ id >
< property name = "name" column = "name" />
< property name = "no" column = "no" />
</ class >
</ hibernate-mapping >
Annotations 注析方式:
package com.Test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="_T")
public class T {
private int id;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="_name")
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
private String Name;
private int no;
}
Junit 測試
使用 Junit 進行對象映射的測試,同時驗證資料庫裡的資料
package com.TestHibernate; // 測試包命名規則,一般為 Test+ 被測試的項目名
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
//assertThat 和 is 斷言要用到的外部類包
import com.Test.T;
import junit.framework.TestCase;
public class TestT extends TestCase {
Session session ;
SessionFactory sf ;
T t ;
@Before
public void setUp() throws Exception {
AnnotationConfiguration cfg =
new AnnotationConfiguration();
sf = cfg.configure().buildSessionFactory();
session = sf .openSession();
session .beginTransaction();
t = new T();
}
@After
public void tearDown() throws Exception {
session .close();
sf .close();
}
@Test
public void testT() {
int x= 3;
String y = "x" ;
int z = 11;
t .setId(x);
t .setName(y);
t .setNo(z);
session .save( t );
session .getTransaction().commit();
assertThat (x,is ( t .getId()));
assertThat (y,is ( t .getName()));
assertThat (z,is ( t .getNo()));
}
}