Hibernate总结1
1.Hibernate是什么
1.java持久層的一個框架
2.一個開放源代碼的ORM(Object Relation Mapping,對象關系映射)框架,使得java開發人員可以使用面向對象編程的思想來操作數據庫
所謂的ORM就是利用描述對象和數據庫表之間的映射的元數據,自動把Java應用程序中的對象持久化到關系型數據庫的表中
2.創建映射文件
實體類Customer映射到Hibernate中的cst_customer表中
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 <!-- 建立類與表的映射 --> 7 <class name="com.itheima.hibernate.demo1.Customer" table="cst_customer"> 8 <!-- 建立類中的屬性與表中的主鍵的對應關系 --> 9 <id name="cust_id" column="cust_id"> 10 <generator class="uuid"></generator> 11 </id> 12 <!-- 建立類中的普通屬性和表的字段的對應 --> 13 <property name="cust_name" column="cust_name"></property> 14 <property name="cust_source" column="cust_source"></property> 15 <property name="cust_industry" column="cust_industry"></property> 16 <property name="cust_level" column="cust_level"></property> 17 <property name="cust_phone" column="cust_phone"></property> 18 <property name="cust_mobile" column="cust_mobile"></property> 19 </class> 20 </hibernate-mapping>3.創建Hibernate的核心配置文件
Hibernate的映射文件反映了持久化類和數據庫表的映射信息,而Hibernate的配置文件則主要用來配置數據庫的連接以及hibernate運行時所需要的各種屬性的值,文件創建在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.connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02</property> 10 <property name="hibernate.connection.username">root</property> 11 <property name="hibernate.connection.password">123</property> 12 <!-- 配置Hibernate的方言 --> 13 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 14 15 <!-- 可選的配置參數 --> 16 <property name="hibernate.show_sql">true</property> 17 <property name="hibernate.format_sql">true</property> 18 <property name="hibernate.hbm2ddl.auto">create</property> 19 <!-- 配置C3P0連接池 --> 20 <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 21 <!--在連接池中可用的數據庫連接的最少數目 --> 22 <property name="c3p0.min_size">5</property> 23 <!--在連接池中所有數據庫連接的最大數目 --> 24 <property name="c3p0.max_size">20</property> 25 <!--設定數據庫連接的過期時間,以秒為單位, 26 如果連接池中的某個數據庫連接處于空閑狀態的時間超過了timeout時間,就會從連接池中清除 --> 27 <property name="c3p0.timeout">120</property> 28 <!--每3000秒檢查所有連接池中的空閑連接 以秒為單位--> 29 <property name="c3p0.idle_test_period">3000</property> 30 31 <!-- 隔離級別配置 --> 32 <property name="hibernate.connection.isolation">4</property> 33 <!-- 配置當前線程綁定的Session --> 34 <property name="hibernate.current_session_context_class">thread</property> 35 36 <mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"></mapping> 37 38 </session-factory> 39 </hibernate-configuration>4.編寫測試代碼
1 public class HibernateDemo1 { 2 @Test 3 public void demo1() { 4 //1.加載Hibernate的核心配置文件 5 Configuration configuration = new Configuration().configure(); 6 //2.創建一個SessionFactory對象,類似于JDBC的連接池 7 SessionFactory sessionFactory = configuration.buildSessionFactory(); 8 //3.通過SessionFactory獲取到Session對象,類似于JDBC中的Connection 9 Session session = sessionFactory.openSession(); 10 //4.手動開啟事務 11 Transaction transaction = session.beginTransaction(); 12 //5.編寫代碼 13 Customer customer = new Customer(); 14 customer.setCust_name("王七"); 15 session.save(customer); 16 //6.事務提交 17 transaction.commit(); 18 //7.資源釋放 19 session.close(); 20 } 21 }4.1 Configuration:配置對象
Configuration用于啟動、加載、管理hibernate的配置文件信息。在啟動Hibernate的過程中,Configuration實例首先確定Hibernate配置文件的位置,然后讀取相關配置,最后創建一個唯一的SessionFactory實例,如果不想用默認的hibernate.cfg.xml配置文件,則向configure()方法中傳遞一個文件路徑的參數
Configuration configuration = new Configuration().configure("xlm文件位置");4.2 SessionFactory:Session工廠對象
SessionFactory接口負責Hibernate的初始化和建立Session對象
SessionFactory實例是通過Configuration對象獲得的
SessionFactory sessionFactory = configuration.buildSessionFactory();SessionFactory的特點:
1.它是線程安全的,它的同一個實例能夠供多個線程共享
2.他是重量級的,不能隨意的創建和銷毀它的實例
由于它的這些特點,一般情況下,一個項目只需要一個SessionFactory,因此可以抽取成一個工具類
1 public class HibernateUtils { 2 public static final Configuration cfg; 3 public static final SessionFactory sf; 4 static { 5 cfg = new Configuration().configure(); 6 sf = cfg.buildSessionFactory(); 7 } 8 public static Session openSession() { 9 return sf.openSession(); 10 } 11 }4.3 Session
Session是應用程序與數據庫之間交互的一個單線程對象,是Hibernate運作的中心,它的主要功能是為持久化對象提供創建、讀取和刪除的功能,所有持久化對象必須在Session管理下才能進行持久化操作
//3.通過SessionFactory獲取到Session對象,類似于JDBC中的ConnectionSession session = sessionFactory.openSession();4.4 Transaction
Transaction接口主要用于管理事務,他是Hibernate的數據庫事務接口,且對底層的事物接口進行了封裝
Transaction transaction = session.beginTransaction();常用的管理事物的方法有
- commit()方法:提交相關聯的session實例
- rollback()方法:撤銷事務操作
?
轉載于:https://www.cnblogs.com/QQ1171492144/p/10541504.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Hibernate总结1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 火遍全网的Hutool,如何使用Buil
- 下一篇: 官方认证:软件及信息技术从业者为新生代农