1. 概念
ORM(Object/Relationship Mapping):對象/關係映射
利用面相對象思想編寫的數據庫應用程序最終都是把對象信息保存在關係型數據庫中,於是要編寫很多和底層數據庫相關的SQL語句。
Hibernate:
是Java領域的一款開源的ORM框架技術,是對JDBC進行了非常輕量級的對象封裝。
其它主流的ORM框架技術:
(1). MyBatis:前身就是著名的iBatis
(2). Toplink:後備Oracle收購,並重新包裝爲Oracle AS Toplink
(3). EJB:本身是JavaEE的規範,重量級框架,使用起來比較複雜
2. 首個Hibernate程序
(1). 創建工程,導入所需Jar包
在下載的hibernate-release目錄的lib下的required目錄下的所有Jar包
(2). 創建Hibernate配置文件
右鍵src目錄創建other,然後選擇Hibernate Configuration File,點擊finish即可。
<?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.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
(3). 創建持久化類
創建類遵循JavaBean原則:
public class Student {
private int id;
private String name;
private String gender;
private Date birthday;
private String address;
public Student() {
}
public Student(int id, String name, String gender, Date birthday,
String address) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", gender=" + gender
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
(4). 創建對象-關係映射文件
右鍵src創建other,選擇Hibernate XML Mapping file,選擇我們剛纔創建的Student,finishi。
然後在hibernate.cfg.xml中加入這個<mapping>配置:
<mapping resource="Student.hbm.xml" />
我們在MySQL中創建數據庫hibernate,選擇字符集:utf8-utf8 general_ci,完成。
(5). 創建測試類
創建Source Folder目錄,創建測試類StudentTest:
public class StudentTest {
@Before
public void init() {
}
@Test
public void testSaveStudent() {
}
@After
public void destory() {
}
}
(6). 通過Hibernate API編寫訪問數據庫的代碼
補充我們的測試文件:
@Before
public void init() {
// 創建配置對象
Configuration config = new Configuration().configure();
// 創建服務註冊對象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(config.getProperties()).buildServiceRegistry();
// 創建會話工廠對象
sessionFactory = config.buildSessionFactory(serviceRegistry);
// 會話對象f
session = sessionFactory.openSession();
// 開啓事務
transaction = session.beginTransaction();
}
@Test
public void testSaveStudent() {
// 生成學生對象
Student s1 = new Student(1, "張三", "男", new Date(), "西安");
Student s2 = new Student(2, "張三", "男", new Date(), "西安");
session.save(s1); // 保存對象進入數據庫
session.save(s2); // 保存對象進入數據庫
}
@After
public void destory() {
transaction.commit(); // 提交事務
session.close(); // 關閉會話
sessionFactory.close(); // 關閉會話工廠
}
右鍵testSaveStudent方法, 運行junit,進入MySQL查看數據,成功。