在java項目開發中,對於DAO層的持久化框架非常的多,流行的Hibernate,Tooplink,OJB等等。Hibernate是一個全自動化的持久行框架,功能非常強大。要想用Hibernate進行開發,首先必須下載其jar包,這裏用的是hibernate-3.2.5版本的。
開發步驟如下:
1.新建一個java項目,項目名稱爲hibernateTest,在項目下新建一個lib文件夾,並將下載的jar包中lib文件夾中的所以jar包拷貝到新建的lib文件夾中,然後將拷貝jar包添加到項目中,同時不要忘記需要數據庫驅動的jar包。
2.在src目錄下新建一個hibernate.cfg.xml的xml文件。該配置文件配置了很多Hibernate框架工作必須的信息。包括數庫連接信息,連接池,映射文件等等
<!--hibernate.hbm2ddl.auto 當數據庫中沒有表時,創建表 取值除了create之外,還有create-drop,update,validate等-->
<!--dialect 配置方言 不同的數據庫都存在一定的差異 通過該配置項 Hibernate會針對不同的數據庫多相應操作 這裏的數據庫爲oracle數據庫-->
<!--show_sql 該配置用來控制是否顯示Hibernate執行的sql語句 true爲顯示sql語句-->
<!--mapping 指定關係映射文件的位置-->
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/hotmail/silence/domain/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.建立實體,在src下新建一個com.hotmail.silence.domain的包 ,在該包中建立一個Person類。
注意:(1). Person類就是一個標準的javaBean。也就是說,要有字段和相應的的getter和setter方法。
(2).有一個id屬性。
(3).必須要用一個無參數構造方法。
代碼如下:
package com.hotmail.silence.domain;
public class Person {
//實體類中一般都有一個id屬性 用來做主鍵
private int id;
private String name;
private int age;
private IdCard idCard;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.建立映射文件,在Person類的同一目錄下,新建一個Person.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
'-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
<!--package屬性用來指定包名,-->
<hibernate-mapping package="com.hotmail.silence.domain">
<!--name屬性用來指定類名稱 要和類名一致。table屬性用來指定映射生成的表名稱 默認和類名稱一致-->
<class name="Person" table="person">
<!--對應Person類的id屬性,在表中生成id字段 Hibernate框架中有一個native類(採用自增長)會自動給id賦值-->
<id name="id">
<generator class="native"/>
</id>
<!--對應Person類的name屬性 在表中會生成name字段-->
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
在hibernate.cfg.xml文件中加入 <mapping resource="com/hotmail/silence/domain/Person.hbm.xml"/> 參見hibernate.cfg.xml文件
4.新建一個含有main方法的測試類。進行測試。
在src目錄下新建一個com.hotmail.silence.test包並新建一個Test1類。
代碼如下:
package com.hotmail.silence.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hotmail.silence.domain.Person;
public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
try{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
org.hibernate.Transaction transaction = session.beginTransaction();
Person p = new Person();
p.setName("name");
p.setAge(10);
session.save(p);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}finally{
if(session != null)
session.close();
}
}
}
運行Test1 會輸出:
Hibernate: create sequence hibernate_sequence
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into tbl_person (name, age, id) values (?, ?, ?)
進入數據庫 執行select * from person
即可查詢出數據。
Hibernate: create sequence hibernate_sequence
2014-10-14 18:12:48 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into tbl_person (name, age, id) values (?, ?, ?)
Hibernate: create sequence hibernate_sequence
2014-10-14 18:12:48 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into tbl_person (name, age, id) values (?, ?, ?)Hibernate: create sequence hibernate_sequence
2014-10-14 18:12:48 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into tbl_person (name, age, id) values (?, ?, ?)