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.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 >
对象映射文件 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 >
/* 被映射的对象类代码与类 T 一样,见下 */
package com.Test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="_T")
/*@Table 要选 javax 那个,如果 _T 不存在,则自动生成 */
public class T {
private int id;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="_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;
}
/* 以上为被映射的对象类,之后还要在 hibernate 文件中添加映射声明,详细见上 */
使用 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()));
}
}