1. 程式人生 > >Hibernate.基礎篇《二》. getOpenSession() 和 getCurrentSession() - 1

Hibernate.基礎篇《二》. getOpenSession() 和 getCurrentSession() - 1

Hibernate.基礎篇《二》. getOpenSession() 和 getCurrentSession() - 1

說明:

  在Hibernate應用中,Session介面的使用最為廣泛,也成為持久化管理器,提供與持久化相關的操作,如:新增、更新、刪除、載入即查詢物件等。可以簡單的理解為Session是JDBC中的Connection的封裝。那也就是說,如果你想操作資料庫(CRUD操作),那麼一定會使用到Session,而Session的建立有兩種:1.  getOpenSession()    2. getCurrentSession() 。程式碼如下:

 

專案上僅多了一個SessionTest.java類,其它保持不變,結構如下:

  

 

  SessionTest.java 

 1 package com.charles.hibernate.test;
 2 
 3 import org.hibernate.Session;
 4 import org.junit.Test;
 5 
 6 import com.charles.hibernate.util.HibernateUtil;
 7 
 8 /**
 9  * <p>Type: SessionTest</p>
10 * <p>Description: Session</p> 11 * @author baitang.<gy03554> 12 * @date 2018年10月20日 上午12:27:37 13 * @version v1.0.0 14 */ 15 public class SessionTest { 16 17 @Test 18 public void getOpenSession() { 19 20 // 獲取OpenSession 21 Session openSession = HibernateUtil.getOpenSession();
22 System.out.println("OpenSession :" + openSession ); 23 24 // 比較在同一個執行緒中,每次獲取的Session是否為同一個? 25 Session openSession1 = HibernateUtil.getOpenSession(); 26 Session openSession2 = HibernateUtil.getOpenSession(); 27 System.out.println(openSession1 == openSession2); // #輸出結果:false 28 } 29 30 @Test 31 public void getCurrentSession() { 32 33 /** 34 * 獲取CurrentSession 35 * 注意: 36 * 獲取CurrentSession的時候, hibernate.cfg.xml配置檔案中需要配置如下配置: 37 * <property name="hibernate.current_session_context_class">thread</property> 38 * 如果不配置上面這一行,那麼是拿不到CurrentSession的。 39 */ 40 Session currentSession = HibernateUtil.getCurrentSession(); 41 System.out.println("CurrentSession :" + currentSession); 42 43 // 比較在同一個執行緒中,每次獲取的Session是否為同一個? 44 Session currentSession1 = HibernateUtil.getCurrentSession(); 45 Session currentSession2 = HibernateUtil.getCurrentSession(); 46 System.out.println(currentSession1 == currentSession2); // #輸出結果:true 47 } 48 }

 

  hibernate.cfg.xml 

 1 <?xml version='1.0' encoding='UTF-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <!-- Generated by MyEclipse Hibernate Tools. -->
 7 <hibernate-configuration>
 8 
 9     <session-factory>
10 
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://192.168.43.150:3306/hibernate</property>
13         <property name="connection.username">hibernate</property>
14         <property name="connection.password">hibernate</property>
15 
16         <!-- 方言 -->
17         <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
18 
19         <!-- 顯示SQL語句 -->
20         <property name="hibernate.show_sql">true</property>
21 
22         <!-- 格式化SQL語句 -->
23         <property name="hibernate.format_sql">true</property>
24 
25         <!-- 在啟動時根據配置更新資料庫 -->
26         <!-- <property name="hbm2ddl.auto">update</property> -->
27 
28         <!-- 在是使用getCuurentSesion方法獲取session時,需配置 -->
29         <property name="hibernate.current_session_context_class">thread</property>
30 
31     </session-factory>
32 
33 </hibernate-configuration>

 

 

Session總結《》:

  上面的一段程式碼說明了,在使用openSession()方法獲取一個Session的時候,是重新建立一個新的session;而getCurrentSession() 則是獲取當前執行緒裡的session。

 

如有問題,歡迎糾正!!!

如有轉載,請標明源處:https://www.cnblogs.com/Charles-Yuan/p/9820045.html