【Hibernate(一)】Hibernate框架的概述
阿新 • • 發佈:2018-11-08
1. 框架的概述
1.1 什麼是框架
框架:指的是軟體的半成品,已經完成了部分功能。
1.2 Java EE的三層結構
1.2.1 Java EE的經典三層結構
1.3 Hibernate概述
1.3.1 什麼是Hibernate
Hibernate:Hibernate是一個
的
框架。
1.3.2 什麼是ORM
ORM:Object Relational Mapping(物件關係對映)。指的是將一個Java中的物件與關係型資料庫中的表建立一種對映關係,從而操作物件就可以操作資料庫中的表。
1.3.3 為什麼要學習Hibernate
1.4 Hibernate的入門
1.4.1 下載Hibernate的開發環境
Hibernate3.x Hibernate4.x Hibernate5.x
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
1.4.2 解壓Hibernate
- documentation :Hibernate開發的文件
- lib :Hibernate開發包
- required :Hibernate開發的必須的依賴包
- optional :Hibernate開發的可選的jar包
- project :Hibernate提供的專案
1.4.3 建立一個專案,引入jar包
- 資料庫驅動包
- Hibernate開發的必須的jar包
- Hibernate引入日誌記錄包
1.4.4 建立表
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.4.5 建立實體類
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;
}
1.4.1.6 建立對映
對映需要通過XML的配置檔案來完成,這個配置檔案可以任意命名。儘量統一命名規範(類名.hbm.xml)
Customer.hbm.xml檔案配置(一般和實體類放在同一個包下):
<?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.itheima.hibernate.demo1.Customer" table="cst_customer">
<!-- 建立類中的屬性與表中的主鍵對應 -->
<id name="cust_id" column="cust_id" >
<generator class="native"/>
</id>
<!-- 建立類中的普通的屬性和表的欄位的對應 -->
<property name="cust_name" column="cust_name" length="32" />
<property name="cust_source" column="cust_source" length="32"/>
<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.4.7 建立一個Hibernate的核心配置檔案
Hibernate的核心配置檔案的名稱:hibernate.cfg.xml
hibernate.cfg.xml檔案配置(一般直接放到src下)
<?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://localhost/hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abc</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/itheima/hibernate/demo1/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.4.8 編寫測試程式碼
package com.itheima.hibernate.demo1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
* Hibernate的入門案例
* @author jt
*
*/
public class HibernateDemo1 {
@Test
// 儲存客戶的案例
public void demo1(){
// 1.載入Hibernate的核心配置檔案
Configuration configuration = new Configuration().configure();
// 手動載入對映
// configuration.addResource("com/itheima/hibernate/demo1/Customer.hbm.xml");
// 2.建立一個SessionFactory物件:類似於JDBC中連線池
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("王西");
session.save(customer);
// 6.事務提交
transaction.commit();
// 7.資源釋放
session.close();
sessionFactory.close();
}
}
1.5 Hibernate的常見配置
1.5.1 XML提示的配置
- 配置XML提示問題
1.5.2 Hibernate的對映的配置
對映的配置
【class標籤的配置】
- 標籤用來建立類與表的對映關係
- 屬性:
- name:類的全路徑
- table:表名(類名與表名一致,table可以省略)
- catalog:資料庫名
【id標籤的配置】
- 標籤用來建立類中的屬性與表中的主鍵的對應關係
- 屬性:
- name:類中的屬性名
- column:表中的欄位名(類中的屬性名和表中的欄位名如果一致,column可以省略)
- length:長度
- type:型別
【property標籤的配置】
- 標籤用來建立類中的普通屬性與表的欄位的對應關係
- 屬性:
- name;類中的屬性名
- column:表中的欄位名
- length:長度
- type:型別
- not-null:設定非空
- unique:設定唯一
1.5.3 Hibernate的核心的配置
Hibernate的核心配置方式
- 一種方式:屬性檔案的方式
- hibernate.properties
- hibernate.connection.driver_class=com.mysql.jdbc.Driver
- …
- hibernate.show_sql=true
- 屬性檔案的方式不能引入對映檔案(手動編寫程式碼載入對映檔案)
- 二種方式:XML檔案的方式
- hibernate.cfg.xml
核心的配置
- 必須的配置
- 連線資料庫的基本的引數
- 驅動類
- url路徑
- 使用者名稱
- 密碼
- 方言
- 連線資料庫的基本的引數
- 可選的配置
- 顯示SQL:hibernate.show_sql
- 格式化SQL:hibernate.format_sql
- 自動建表:hibernate.hbm2ddl.auto
- none:不適用hibernate的自動建表
- create:如果資料庫中已經有表,刪除原有表,重新建立,如果沒有表,新建表。(測試)
- create-drop:如果資料庫中已經有表,刪除原有表,執行操作,刪除這個表。如果沒有表,新建一個,使用完了刪除該表。(測試)
- update:如果資料庫中有表,使用原有表,如果沒有表,建立新表(更改新表結構)
- validate:如果沒有表,不會建立表。只會使用資料庫中原有的表。(校驗對映和表結構)
- 對映檔案的引入
- 引入對映檔案的位置
<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/>
1.6 Hibernate的核心API
1.6.1 Configuration:Hibernate的配置物件
- 作用:
- 載入核心配置檔案
- hibernate.properties
Configuration cfg = new Configuration();
- hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
- 載入對映檔案
// 手動載入對映
configuration.addResource("com/itheima/hibernate/demo1/Customer.hbm.xml");
1.6.2 SessionFactory:Session工廠
SessionFactory內部維護了Hibernate的連線池和Hibernate的二級快取(不講)。是執行緒安全的物件。一個專案建立一個物件即可。
配置連線池:(瞭解)
引入C3P0的jar包,然後將以下內容貼上到核心配置檔案hibernate.cfg.xml中
<!-- 配置C3P0連線池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--在連線池中可用的資料庫連線的最少數目 -->
<property name="c3p0.min_size">5</property>
<!--在連線池中所有資料庫連線的最大數目 -->
<property name="c3p0.max_size">20</property>
<!--設定資料庫連線的過期時間,以秒為單位,
如果連線池中的某個資料庫連線處於空閒狀態的時間超過了timeout時間,就會從連線池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒檢查所有連線池中的空閒連線 以秒為單位-->
<property name="c3p0.idle_test_period">3000</property>
抽取工具類
public class HbernateUtils {
public static final Configuration cfg;
public static final SessionFactory sf;
static {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
}
public static Session openSession(){
return sf.openSession();
}
}
Hibernate工具類的測試
public class HibernateDemo2 {
@Test
//儲存客戶
public void demo1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setCust_name("王曉東");
session.save(customer);
tx.commit();
session.close();
}
}
1.6.3 Session:類似Connection物件是連線物件
Session代表的是Hibernate與資料庫的連結物件。不是執行緒安全的。與資料庫互動橋樑。
- 查詢所有
1.6.4 Transaction:事務物件
Hibernate中管理事務的物件。
- commit();
- rollback();