可以使用注釋建立hibernate應用程式。 有許多註釋可用於建立hibernate應用程式,如@Entity,@Id,@Table等。
Hibernate註釋基於JPA 2規範,並支援所有功能。
所有JPA註釋都在javax.persistence.*
包中定義。 Hibernate EntityManager實現由JPA規範定義的介面和生命週期。
使用hibernate註釋的核心優點是我們不需要建立對映(*.hbm.xml
)檔案直接建立物件關聯。 在這裡,hibernate註釋用於提供後設資料。
完整的專案結構圖如下 -
建立具有註釋的hibernate應用程式有4
個步驟。分別如下 -
對於MySQL,您需要新增mysql-connector-java-5.1.40-bin.jar
檔案,以及 hibernate5的核心類庫,如下所示 -
在這裡,我們建立一個持久化類: Employee
。 並使用注釋完成與資料庫表:tb_employee
的對映關聯。
@Entity
注釋將此類標記為實體。@Table
注釋指定要保留此實體的資料的表名。 如果不使用@Table
注釋,hibernate將使用類名作為表名稱bydefault
。@Id
注釋標記此實體的識別符號。@Column
注釋指定此屬性或欄位的列的詳細資訊。如果未指定@Column
注釋,則屬性名稱將用作列名稱bydefault
。
Employee.java 檔案的程式碼如下 -
package com.yiibai;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_employee")
public class Employee {
@Id
private int id;
private String firstName, lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
開啟hibernate.cfg.xml
檔案,並新增如下的對映資源條目:
<mapping class="com.yiibai.Employee"/>
現在完整的組態檔案: hibernate.cfg.xml
,將如下所示:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping class="com.yiibai.Employee"/>
</session-factory>
</hibernate-configuration>
在這個類中,我們只是將Employee
類的物件儲存到資料庫中。 在這裡,我們使用StandardServiceRegistryBuilder
類和MetadataSources
類從持久化類獲取對映的資訊。
Test.java
測試類程式碼如下 -
package com.yiibai;
import org.hibernate.*;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.*;
public class Test {
public static void main(String[] args) {
//Session session = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml")
.build();
// 2. 根據服務註冊類建立一個後設資料資源集,同時構建後設資料並生成應用一般唯一的的session工廠
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
/**** 上面是組態準備,下面開始我們的資料庫操作 ******/
Session session = sessionFactory.openSession();// 從對談工廠獲取一個session
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setId(1001);
e1.setFirstName("Yii");
e1.setLastName("Bai");
Employee e2 = new Employee();
e2.setId(1002);
e2.setFirstName("Min");
e2.setLastName("Su");
session.persist(e1);
session.persist(e2);
t.commit();
session.close();
System.out.println("successfully saved");
}
}
執行上面(Test.java)範例,得到以下結果 -
... ...
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: insert into tb_employee (firstName, lastName, id) values (?, ?, ?)
Hibernate: insert into tb_employee (firstName, lastName, id) values (?, ?, ?)
successfully saved
檢視資料庫表 tb_employee
中的資料,應該會看到以下結果 -