zoukankan      html  css  js  c++  java
  • Hibernate.基础篇《一》.Hibernate工具类.

    Hibernate.基础篇《一》.Hibernate工具类.

    话述:

      Hibernate.基础篇第一篇,前面是代码、后面再加理论&实践。

      Hibernate使用的版本是:5.x,在《Hibernate.基础篇《一》.Hibernate工具类.》已有具体描述,并实现了创建SessionFactory的方式,本篇对Hibernate工具类进行了小小的改良,如下:

      项目结构:

      

      RunTest.java

     1 package com.charles.hibernate.test;
     2 
     3 import org.hibernate.SessionFactory;
     4 import org.junit.Test;
     5 
     6 import com.charles.hibernate.util.HibernateUtil;
     7 
     8 /**
     9  * <p>Type: RunTest</p>
    10  * <p>Description: 测试方法.</p>
    11  * @author baitang.<gy03554>
    12  * @date 2018年10月17日 下午10:38:18
    13  * @version v1.0.0
    14  */
    15 public class RunTest {
    16 
    17     @Test
    18     public void getSessionFactory() {
    19     
    20         SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    21         System.out.println("SessionFactory ===>>>> " + sessionFactory);
    22     }
    23 }

      HibernateUtil.java 

     1 package com.charles.hibernate.util;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.hibernate.boot.MetadataSources;
     6 import org.hibernate.boot.registry.StandardServiceRegistry;
     7 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
     8 
     9 /**
    10  * <p>Type: HibernateUtil</p>
    11  * <p>Description: Hibernate工具类.《Hibernate.5.x》</p>
    12  * @author baitang.<gy03554>
    13  * @date 2018年10月17日 下午8:53:52
    14  * @version v1.0.0
    15  */
    16 public class HibernateUtil {
    17 
    18     /** 创建Session的工厂:SessionFactory **/
    19     private static SessionFactory sessionFactory = null;
    20 
    21     /** hibernate.cfg.xml配置文件的位置. **/
    22     final static String CONFIGURE_RESOURCE = "hibernate.cfg.xml";
    23 
    24     static {
    25         /** SessionFactory在应用中被创建一次 **/
    26         StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure(CONFIGURE_RESOURCE).build();
    27 
    28         try {
    29             
    30             sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
    31         } catch (Exception e) {
    32             
    33             /** sessionFacotry创建失败.销毁serviceRegistry **/
    34             StandardServiceRegistryBuilder.destroy(serviceRegistry);
    35         }
    36     }
    37     
    38     /**
    39      * 获取SessionFactory方法.
    40      * @return SessionFactory
    41      */
    42     public static SessionFactory getSessionFactory() {
    43         
    44         return sessionFactory;
    45     }
    46     
    47     /**
    48      * 获取OpenSession方法
    49      * @return Session
    50      */
    51     public static Session getOpenSession() {
    52         
    53         return sessionFactory.openSession();
    54     }
    55     
    56     /**
    57      * 获取CurrentSession 方法
    58      * @return Session
    59      */
    60     public static Session getCurrentSession() {
    61         
    62         return sessionFactory.getCurrentSession();
    63     }
    64     
    65     /**
    66      * 关闭Session方法.
    67      * @param session
    68      */
    69     public static void closeSession(Session session) {
    70         
    71         if(null != session) {
    72             if(session.isOpen()) {
    73                 session.close();
    74             }
    75         }
    76     }
    77 }

      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>

      运行项目:

        打开TestRun.java类,选择要执行的方法(getSessionFactory())鼠标右键--->>Run As -->> Junit Test

        

      运行测试结果:

        

    如有问题,欢迎纠正!!!

    如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9807719.html

  • 相关阅读:
    xx
    java
    SAFe 4.0参考指南:精益软件与系统工程的规模化敏捷框架
    Scaled Agile Framework – SAFe for Lean Enterprises
    云宏信息科技股份有限公司
    k8s~kubectl常用命令
    springboot~mvn多个关联项目打包问题
    java~线程池的总结~续
    java~线程池的总结
    电子工程师必备:九大系统电路识图宝典
  • 原文地址:https://www.cnblogs.com/Charles-Yuan/p/9807719.html
Copyright © 2011-2022 走看看