1.4.1.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)。
<?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" > <!-- 主鍵的生成策略 native 本地的 --> <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.1.7、創建 Hibernate 的核心配置文件
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_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>
編寫測試類
package com.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.手動開啟事務: // 兼容 hibernate3,hibernate5不需要手動開啟事務 Transaction transaction = session.beginTransaction(); // 5.編寫代碼 Customer customer = new Customer(); customer.setCust_name("王西"); session.save(customer); // 6.事務提交 transaction.commit(); // 7.資源釋放 session.close(); sessionFactory.close(); } }
二、Hibernate 的常見配置
1、XML 提示的配置
2、Hibernate 映射的配置
【class 標簽的配置】
標簽用來建立類與表的映射關系
屬性:
name :類的全路徑
table :表名(類名與表名一致,table可以省略)
catalog :數據庫名
【id標簽的配置】
標簽用來建立類中的屬性與表中的主鍵的對應關系
屬性:
name :類中的屬性名
column :表中的字段名(類中的屬性名和表中的字段名如果一致,column可以省略)
length :長度
type :類型
【property標簽的配置】
標簽用來建立類中的普通屬性與表的字段的對應關系
屬性:
name :類中的屬性名
column :表中的字段名
length :長度(默認長度為255)
type :類型(可以不寫,hibernate 會自動轉化)
<!-- type 有三種形式 --> <!-- 1、Java中的類型 --> <property name="cust_name" column="cust_name" length="32" type="java.lang.Strinng" /> <!-- 2、Hibernate 中的類型 --> <property name="cust_name" column="cust_name" length="32" type="string" /> <!-- 3、數據庫中的類型 --> <property name="cust_name" column="cust_name" length="32"> <column name="cust_naem" sql-type="varchar"></column> </property>
not-null :設置非空
unique :設置唯一
3、hibernate 核心的配置
3.1、核心配置方式
① 屬性文件的方式(不常用)
hibernate.properties
hibernate.connection.driver_class=com.mysql.jdbc.Driver
…
hibernate.show_sql=true
屬性文件的方式不能引入映射文件(手動編寫代碼加載映射文件)
② XML文件的方式
hibernate.cfg.xml
3.2、核心的配置
【必須的配置】
連接數據庫的基本的參數
驅動類
url路徑
用戶名
密碼
方言
[可選的配置]
顯示SQL :hibernate.show_sql
格式化SQL :hibernate.format_sql
自動建表 :hibernate.hbm2ddl.auto
none :不使用 hibernate 的自動建表
create :如果數據庫中已經有表,刪除原有表,重新創建,如果沒有表,新建表。(做測試)
create-drop :如果數據庫中已經有表,刪除原有表,執行操作,刪除這個表。如果沒有表,新建一個,使用完了刪除該表。(做測試)
// 需要關閉工廠
sessionFactory.close();
update :如果數據庫中有表,使用原有表,如果沒有表,創建新表(更新表結構)
validate :如果沒有表,不會創建表,並且會報錯。只會使用數據庫中原有的表。(校驗映射和表結構)。
【映射文件的引入】
引入映射文件的位置
<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/>
三、Hibernate 的核心 API
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");
2、SessionFactory:Session 工廠
Session 內部維護了 Hibernate 的連接池和 Hibernate 的二級緩存(不講,企業在一般用 redis 代替 )。是線程安全的對象。一個項目創建一個對象即可。
配置連接池
需要引入 C3P0 的 jar 包 hibernate-release-5.0.7.Final\lib\optional\c3p0 optional(可選的)
加入到項目的 lib 文件目錄下,還需要 Build Path。
<?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_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> <!-- 默認提供連接池,也可以自己配置 --> <!-- 配置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> <mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
這時候通過日志就能看到 C3P0 的情況。
4、Transaction:事務對象
Hibernate 中管理事務的對象。
commit();
rollback();
注意:在 hibernate5 中,如果沒有配置C3P0 ,開啟事務和提交事務可以不寫;內置的連接池是可以自動幫你去提交的,配置好以后需要自己提交。並且 hibernate3 里面不自動提交,必須要寫。為了兼容 hibernate3 的代碼,這兩句最好寫上。
Transaction tx = session.beginTransaction();
...
tx.commit();