zoukankan      html  css  js  c++  java
  • Hibernate.基础篇《二》. getOpenSession() 和 getCurrentSession()

    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

  • 相关阅读:
    NumPy使用图解教程
    Pytorch 网络结构可视化
    OpenCV基于字节指针进行高效像素遍历
    scrapy工具创建爬虫工程
    网络爬虫基本概念与Scrapy工具包使用
    CentOS 7命令行安装GNOME桌面
    sicily 1154. Easy sort (tree sort& merge sort)
    全连通图求最小生成树边权之积(邻接矩阵/prim/kruskal)
    sicily 1024. Magic Island
    写在Web考试后的一点小总结
  • 原文地址:https://www.cnblogs.com/Charles-Yuan/p/9820045.html
Copyright © 2011-2022 走看看