- Hibernate介紹
1.1什麼是框架
框架:指的是軟件的半成品,已經完成了部分功能。
1.2 EE的經典三層結構
1.3 什麼是Hibernate
Hibernate是一個持久層的ORM框架
1.4 什麼是ORM
ORM:Object Relational Mapping(對象關係映射)。指的是將一個Java中的對象與關係型數據庫中的表建立一種映射關係,從而操作對象就可以操作數據庫中的表。
1.5 爲什麼要學習Hibernate
1.6入門
1.6.1 下載Hibernate的開發環境
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
1.6.2 解壓
- documentation :Hibernate開發的文檔
- lib :Hibernate開發包
- required :Hibernate開發的必須的依賴包
- optional :Hibernate開發的可選的jar包
- project :Hibernate提供的項目
1.7創建項目,引入jar包
- 數據庫驅動包
- Hibernate開發的必須的jar包
- Hibernate引入日誌記錄包
1.8 創建表
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
`cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客戶信息來源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定電話',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移動電話',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
1.9 創建實體類
package com.dk.hibernate;
/*
*
CREATE TABLE `cst_customer` (
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
`cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客戶信息來源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定電話',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移動電話',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
*
*
*/
public class Customer {
private long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
public long getCust_id() {
return cust_id;
}
public void setCust_id(long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
}
文檔結構
1.10 創建映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dk.hibernate.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
1.11 創建核心配置文件
Hibernate的核心配置文件的名稱:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置hibernate方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可選配置 -->
<!-- 打印sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 自動創建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/dk/hibernate/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.12 編寫測試代碼
package com.dk.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/*
*
* hibernate入門案例
*/
public class HibernateDemo1 {
@Test
//保存客戶案例
public void demo1() {
//1.加載hibernate核心文件
Configuration configuration=new Configuration().configure();
//2.創建一個sessionFactory對象
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.通過sessionFactory獲取到session對象,類似jdbc的connection
Session session = sessionFactory.openSession();
//4.手動開啓事務
Transaction transaction = session.beginTransaction();
//5.編寫代碼
Customer customer=new Customer();
customer.setCust_name("dddf");
session.save(customer);
//6.事務提交
transaction.commit();
//7.資源釋放
session.close();
}
}
1.13 刷新sql查看結果
2.xml配置
2.1 核心配置 hibernate.cfg.xml
- 必須的配置
- 連接數據庫的基本的參數
- 驅動類
- url路徑
- 用戶名
- 密碼
- 方言
- 連接數據庫的基本的參數
- 可選的配置
- 顯示SQL :hibernate.show_sql
- 格式化SQL :hibernate.format_sql
- 自動建表 :hibernate.hbm2ddl.auto
- none :不使用hibernate的自動建表
- create :如果數據庫中已經有表,刪除原有表,重新創建,如果沒有表,新建表。(測試)
- create-drop :如果數據庫中已經有表,刪除原有表,執行操作,刪除這個表。如果沒有表,新建一個,使用完了刪除該表。(測試)
- update :如果數據庫中有表,使用原有表,如果沒有表,創建新表(更新表結構)
- validate :如果沒有表,不會創建表。只會使用數據庫中原有的表。(校驗映射和表結構)。
- 映射文件的引入
- 引入映射文件的位置
3.Hibernate的API
3.1Configuration:Hibernate的配置對象
加載方式hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
3.2 SessionFactory:Session工廠
3.3 Session:類似Connection對象是連接對象
Session代表的是Hibernate與數據庫的鏈接對象。不是線程安全的。與數據庫交互橋樑。
- Session中的API
- 保存方法:
- Serializable save(Object obj);
- 查詢方法:
- T get(Class c,Serializable id);
- T load(Class c,Serializable id);
- get方法和load方法的區別?
- 保存方法:
-
- 修改方法
- void update(Object obj);
- 修改方法
-
- 刪除方法
- void delete(Object obj);s
- 刪除方法
-
- 保存或更新
- void saveOrUpdate(Object obj)
- 保存或更新
查詢所有
3.4 Transaction:事務對象
Hibernate中管理事務的對象。
- commit();
- rollback();