Hibernate筆記1
一.Hibernate概述
Hibernate是一個實現了ORM思想的,開源的,輕量級的,內部封裝了JDBC操作的持久層框架.
實現了ORM思想的:不再重點關註sql語句的編寫
開源的:開放源代碼的
輕量級的:消耗的資源少(內存),依賴的jar包比較少
註:ORM思想(O:object R:relation M:mapping--對象關系映射)
目的:操作實體類就相當於操作數據庫表
條件:1.創建實體類和表的映射關系
2.創建屬性和字段的映射關系
二.環境搭建
1.導入jar包:必選/log4j/mysql驅動包
2.編寫映射關系文件:實體類.hbm.xml,放在實體類包下
映射關系文件內容:
(1)實體類與sql表的關系
(2)實體類屬性與sql表字段的關系
模板:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <!-- 1.創建實體類與表的映射關系 -->7 <!-- 8 lazy:配置懶加載 9 true:使用懶加載(使用時才發送sql語句); 10 false:立即加載; 11 --> 12 <class name="linkman.Linkman" table="cst_linkman" lazy="false"> 13 <!-- 2.創建屬性與字段值的映射關系 --> 14<!--2.1配置主鍵 --> 15 <id name="lkmId" column="lkm_id"> 16 <generator class="native"></generator> 17 </id> 18 <!--2.2其它屬性與字段 --> 19 <property name="lkmName" column="lkm_name"></property> 20 <property name="lkmGender" column="lkm_gender"></property> 21 <property name="lkmPhone" column="lkm_phone"></property> 22 <property name="lkmMobile" column="lkm_mobile"></property> 23 <property name="lkmEmail" column="lkm_email"></property> 24 <property name="lkmPosition" column="lkm_position"></property> 25 <property name="lkmMemo" column="lkm_memo"></property> 26 </class> 27 </hibernate-mapping>
3.編寫hibernate核心配置文件:hibernate.cfg.xml,放在src包下
核心配置文件內容(順序不能變):
(1)數據庫配置信息;
(2)hibernate基本配置信息;
(3)映射關系文件的位置
模板:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!--數據庫配置 --> 8 <!-- 9 hibernate.hbm2ddl.auto : hibernate生成數據定義語言 10 數據庫的建表語句,使用hibernate自動創建數據庫表 11 create-drop :程序開始的時候創建表(有表刪表,再創建,沒表直接創建),程序結束的時候刪除表 12 create : 有表現刪除再創建表,沒表直接創建 13 update :沒表創建表,有表的話如果映射關系發生了變化,更新表 14 validate :不會創建表,驗證映射文件的配置,如果映射文件配置發生了變化,拋出異常 15 --> 16 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 17 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 18 <property name="hibernate.connection.url">jdbc:mysql:///cst_linkman</property> 19 <property name="hibernate.connection.username">root</property> 20 <property name="hibernate.connection.password">root</property> 21 <!--hibernate基本配置 --> 22 <property name="hibernate.hbm2ddl.auto">update</property> 23 <property name="hibernate.show_sql">true</property> 24 <property name="hibernate.format_sql">true</property> 25 <!--指定映射文件位置 --> 26 <mapping resource="linkman/Linkman.hbm.xml"/> 27 </session-factory> 28 </hibernate-configuration>
附:關於使用Eclipse軟件編寫以上兩種配置文件的自動提示相關問題:
1.配置Eclipse:
菜單欄:window-->preferences-->XML-->Catalog,選中User Specified Entries,單擊Add,選擇File System,選中dtd文件(hibernate-configuration-3.0.dtd或者hibernate-mapping-3.0.dtd)的路徑;Key Type選擇uri,將對應的dtd文件中的uri(http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd或者http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd)復制進去即可;重啟Eclipse,輸入標簽<>即可自動提示;
2.關於標簽內屬性的提示,需按快捷鍵alt+/
三.Hibernate使用步驟:七步
1.加載配置文件--Configuration
2.構建sessionFactory--cfg.buildSessionFactory
3.打開新的session--factory.openSession
4.開啟事務--tx.beginTransaction
5.CRUD
6.提交事務--tx.commit
7.釋放資源--session.close()/factory.close()(若使用工具類,factory不能釋放)
工具類:HibernateUtils
1 //使用靜態代碼塊創建factory,僅在啟動時創建一次,提高效率 2 import org.hibernate.Session; 3 import org.hibernate.SessionFactory; 4 import org.hibernate.cfg.Configuration; 5 public class HibernateUtils { 6 private static SessionFactory factory; 7 static { 8 // 加載配置文件 9 Configuration cfg = new Configuration(); 10 cfg.configure(); 11 // 構建sessionFactory 12 factory = cfg.buildSessionFactory(); 13 } 14 public static Session getSession() { 15 return factory.openSession(); 16 } 17 }
============================================================
舉例:
1.創建sql表
2.創建實體類
3.編寫映射關系文件:Linkman.hbm.xml
4.編寫hibernate配置文件:hibernate.cfg.xml
5.編寫工具類
6.編寫執行代碼
------------------------------------------------------------
1.創建sql表
1 CREATE TABLE `cst_linkman` ( 2 `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘聯系人編號(主鍵)‘, 3 `lkm_name` varchar(16) DEFAULT NULL COMMENT ‘聯系人姓名‘, 4 `lkm_gender` varchar(10) DEFAULT NULL COMMENT ‘聯系人性別‘, 5 `lkm_phone` varchar(16) DEFAULT NULL COMMENT ‘聯系人辦公電話‘, 6 `lkm_mobile` varchar(16) DEFAULT NULL COMMENT ‘聯系人手機‘, 7 `lkm_email` varchar(64) DEFAULT NULL COMMENT ‘聯系人郵箱‘, 8 `lkm_position` varchar(16) DEFAULT NULL COMMENT ‘聯系人職位‘, 9 `lkm_memo` varchar(512) DEFAULT NULL COMMENT ‘聯系人備註‘, 10 PRIMARY KEY (`lkm_id`) 11 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
2.創建實體類
1 public class Linkman { 2 private Long lkmId;// 聯系人編號(主鍵) 3 private String lkmName;// 姓名 4 private String lkmGender;// 性別 5 private String lkmPhone;// 辦 公電話 6 private String lkmMobile;// 手機 7 private String lkmEmail;// 郵箱 8 private String lkmPosition;// 職位 9 private String lkmMemo;// 備註 10 // getter/setter 11 public Long getLkmId() { 12 return lkmId; 13 } 14 15 public void setLkmId(Long lkmId) { 16 this.lkmId = lkmId; 17 } 18 19 public String getLkmName() { 20 return lkmName; 21 } 22 23 public void setLkmName(String lkmName) { 24 this.lkmName = lkmName; 25 } 26 27 public String getLkmGender() { 28 return lkmGender; 29 } 30 31 public void setLkmGender(String lkmGender) { 32 this.lkmGender = lkmGender; 33 } 34 35 public String getLkmPhone() { 36 return lkmPhone; 37 } 38 39 public void setLkmPhone(String lkmPhone) { 40 this.lkmPhone = lkmPhone; 41 } 42 43 public String getLkmMobile() { 44 return lkmMobile; 45 } 46 47 public void setLkmMobile(String lkmMobile) { 48 this.lkmMobile = lkmMobile; 49 } 50 51 public String getLkmEmail() { 52 return lkmEmail; 53 } 54 55 public void setLkmEmail(String lkmEmail) { 56 this.lkmEmail = lkmEmail; 57 } 58 59 public String getLkmPosition() { 60 return lkmPosition; 61 } 62 63 public void setLkmPosition(String lkmPosition) { 64 this.lkmPosition = lkmPosition; 65 } 66 67 public String getLkmMemo() { 68 return lkmMemo; 69 } 70 71 public void setLkmMemo(String lkmMemo) { 72 this.lkmMemo = lkmMemo; 73 } 74 75 // toString 76 @Override 77 public String toString() { 78 return "Linkman [lkmId=" + lkmId + ", lkmName=" + lkmName + ", lkmGender=" + lkmGender + ", lkmPhone=" 79 + lkmPhone + ", lkmMobile=" + lkmMobile + ", lkmEmail=" + lkmEmail + ", lkmPosition=" + lkmPosition 80 + ", lkmMemo=" + lkmMemo + "]"; 81 } 82 83 } 84
3.編寫映射關系文件:Linkman.hbm.xml--放在實體類所在包下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 <!-- 1.創建實體類與表的映射關系 --> 8 <class name="linkman.Linkman" table="cst_linkman"> 9 <!-- 2.創建屬性與字段值的映射關系 --> 10 <!--2.1配置主鍵 --> 11 <id name="lkmId" column="lkm_id"> 12 <generator class="native"></generator> 13 </id> 14 <!--2.2其它屬性與字段 --> 15 <property name="lkmName" column="lkm_name"></property> 16 <property name="lkmGender" column="lkm_gender"></property> 17 <property name="lkmPhone" column="lkm_phone"></property> 18 <property name="lkmMobile" column="lkm_mobile"></property> 19 <property name="lkmEmail" column="lkm_email"></property> 20 <property name="lkmPosition" column="lkm_position"></property> 21 <property name="lkmMemo" column="lkm_memo"></property> 22 </class> 23 </hibernate-mapping>
4.編寫hibernate配置文件:hibernate.cfg.xml--放在src下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!--數據庫配置 --> 8 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 9 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="hibernate.connection.url">jdbc:mysql:///cst_linkman</property> 11 <property name="hibernate.connection.username">root</property> 12 <property name="hibernate.connection.password">root</property> 13 <!--hibernate基本配置 --> 14 <property name="hibernate.hbm2ddl.auto">update</property> 15 <property name="hibernate.show_sql">true</property> 16 <property name="hibernate.format_sql">true</property> 17 <!--指定映射文件位置 --> 18 <mapping resource="linkman/Linkman.hbm.xml"/> 19 </session-factory> 20 </hibernate-configuration>
5.編寫工具類
1 import org.hibernate.Session; 2 import org.hibernate.SessionFactory; 3 import org.hibernate.cfg.Configuration; 4 5 public class HibernateUtils { 6 private static SessionFactory factory; 7 8 static { 9 // 加載配置文件 10 Configuration cfg = new Configuration(); 11 cfg.configure(); 12 // 構建sessionFactory 13 factory = cfg.buildSessionFactory(); 14 } 15 16 public static Session getSession() { 17 return factory.openSession(); 18 } 19 20 } 21
6.編寫執行代碼--增刪改
1 package demo; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 import org.junit.Test; 6 7 import linkman.Linkman; 8 import utils.HibernateUtils; 9 10 public class Demo02 { 11 // 增加 12 @Test 13 public void add() { 14 // 利用工具類獲取session 15 Session session = HibernateUtils.getSession(); 16 // 開啟事務 17 Transaction tx = session.beginTransaction(); 18 // crud/ 19 Linkman man = new Linkman(); 20 man.setLkmName("Jack"); 21 session.save(man); 22 // 提交事務 23 tx.commit(); 24 // 釋放資源 25 session.close(); 26 27 } 28 29 // 修改id=2的名字為rose,id一定要正確,否則報空指針 30 @Test 31 public void modify() { 32 // 獲取session 33 Session session = HibernateUtils.getSession(); 34 // 開啟事務 35 Transaction tx = session.beginTransaction(); 36 // 修改 37 Linkman man = session.get(Linkman.class, 2L); 38 man.setLkmName("Rose"); 39 // 提交事務 40 tx.commit(); 41 // 釋放資源 42 session.close(); 43 } 44 45 //刪除 46 @Test 47 public void del() { 48 // 獲取session 49 Session session = HibernateUtils.getSession(); 50 // 開啟事務 51 Transaction tx = session.beginTransaction(); 52 // crud-del 53 Linkman man = session.get(Linkman.class, 2L); 54 session.delete(man); 55 // 提交事務 56 tx.commit(); 57 // 釋放 58 session.close(); 59 } 60 } 61
Hibernate筆記1