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

话述:

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

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

  项目结构:

  

  RunTest.java

 package com.charles.hibernate.test;

 import org.hibernate.SessionFactory;
import org.junit.Test; import com.charles.hibernate.util.HibernateUtil; /**
* <p>Type: RunTest</p>
* <p>Description: 测试方法.</p>
* @author baitang.<gy03554>
* @date 2018年10月17日 下午10:38:18
* @version v1.0.0
*/
public class RunTest { @Test
public void getSessionFactory() { SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
System.out.println("SessionFactory ===>>>> " + sessionFactory);
}
}

  HibernateUtil.java

 package com.charles.hibernate.util;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; /**
* <p>Type: HibernateUtil</p>
* <p>Description: Hibernate工具类.《Hibernate.5.x》</p>
* @author baitang.<gy03554>
* @date 2018年10月17日 下午8:53:52
* @version v1.0.0
*/
public class HibernateUtil { /** 创建Session的工厂:SessionFactory **/
private static SessionFactory sessionFactory = null; /** hibernate.cfg.xml配置文件的位置. **/
final static String CONFIGURE_RESOURCE = "hibernate.cfg.xml"; static {
/** SessionFactory在应用中被创建一次 **/
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure(CONFIGURE_RESOURCE).build(); try { sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) { /** sessionFacotry创建失败.销毁serviceRegistry **/
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
} /**
* 获取SessionFactory方法.
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() { return sessionFactory;
} /**
* 获取OpenSession方法
* @return Session
*/
public static Session getOpenSession() { return sessionFactory.openSession();
} /**
* 获取CurrentSession 方法
* @return Session
*/
public static Session getCurrentSession() { return sessionFactory.getCurrentSession();
} /**
* 关闭Session方法.
* @param session
*/
public static void closeSession(Session session) { if(null != session) {
if(session.isOpen()) {
session.close();
}
}
}
}

  hibernate.cfg.xml

  

 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.43.150:3306/hibernate</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">hibernate</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 显示SQL语句 -->
<property name="hibernate.show_sql">true</property> <!-- 格式化SQL语句 -->
<property name="hibernate.format_sql">true</property> <!-- 在启动时根据配置更新数据库 -->
<!-- <property name="hbm2ddl.auto">update</property> --> <!-- 在是使用getCuurentSesion方法获取session时,需配置 -->
<!-- <property name="hibernate.current_session_context_class">thread</property> --> </session-factory> </hibernate-configuration>

  运行项目:

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

    

  运行测试结果:

    

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

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

Hibernate.基础篇《一》.Hibernate工具类.的更多相关文章

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

    Hibernate.基础篇<二>. getOpenSession() 和 getCurrentSession() - 1 说明: 在Hibernate应用中,Session接口的使用最为广 ...

  2. hibernate hql where语句拼接工具类

    package com.zhaoshijie.tree.other; /** * hibernate HQL WHERE语句工具类 * * @author 赵士杰 * */public class H ...

  3. Hibernate基础学习(二)—Hibernate相关API介绍

    一.Hibernate的核心接口      所有的Hibernate应用中都会访问Hibernate的5个核心接口.      (1)Configuration接口: 配置Hibernate,启动Hi ...

  4. Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】

    对Lucene代码优化 我们再次看回我们上一篇快速入门写过的代码,我来截取一些有代表性的: 以下代码在把数据填充到索引库,和从索引库查询数据的时候,都出现了.是重复代码! Directory dire ...

  5. Hibernate入门篇<1>hibernate.cfg.xml学习小结

    Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性,这个配置文件应该位于应用程序或Web程序的类文件夹 classes中.Hibernate配置文件支持两种形式, ...

  6. cocos2dx基础篇(3) 常用重要类

    ---------------------------------------- 入口类main.cpp 主要控制类AppDelegate.cpp -------------------------- ...

  7. 基础篇:JAVA引用类型和ThreadLocal

    前言 平时并发编程,除了维护修改共享变量的场景,有时我们也需要为每一个线程设置一个私有的变量,进行线程隔离,java提供的ThreadLocal可以帮助我们实现,而讲到ThreadLocal则不得不讲 ...

  8. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  9. JAVA基础学习day17--集合工具类-Collections

    一.Collection简述 1.1.Collection与Collections的区别 Collections是集合的静态工具类 Collection:是集合的顶级接口 二.Sort 2.1.sor ...

随机推荐

  1. wpf中的数据模板

    wpf中的模板分为数据模板和控件模板,我们可以通过我们自己定制的数据模板来制定自己想要的数据表现形式.例如:时间的显示可以通过图片,也可以通过简单数字表现出来. 例如: (1)先在Demo这个命名空间 ...

  2. android gradle jnilibs

    https://blog.csdn.net/xx326664162/article/details/51167849 [ABIXCPU] Android jniLibs下目录详解(.so文件) htt ...

  3. nginx-upstream-bio/nio/netty-zuul2-apigateway-openresty-orange-lua-docker

    upstream_addr等到走了一些弯路,才发现nginx的upstream本来就有一个upstream_addr的模块,一下子我觉得找到了方向,不过看这个变量的说明,发现它主要用在记录log上面, ...

  4. get post header获取数据方方法

    /** * get方式获取数据 * @param $url * @param $data * @return bool|string */public function methodGet($url, ...

  5. LeetCode 872 Leaf-Similar Trees 解题报告

    题目要求 Consider all the leaves of a binary tree.  From left to right order, the values of those leaves ...

  6. HashMap如何解决取Value值为Null

    场景: 用HashMap方法时候,取Keys时候自认为敲的肯定是准确无误,然后能得到对应的Values 值.  但写脚本代码时候不好习惯,没事总喜欢敲个空格建,导致取Keys之后多空格. Featur ...

  7. pyqt5_eric6_Qt desinger

    麦子学院视频教程day1 1.创建pushbutton 绑定信号和槽 Ui_mainWindow.py 1 from PyQt5 import QtCore, QtGui, QtWidgets cla ...

  8. 【托业】【新托业TOEIC新题型真题】学习笔记12-题库八-P7

    155.political figure 政治人物 prominent 160.association n.协会,社团; 联合,联系; 联想; rarely adv.很少地; 罕有地; 极精彩地; 珍 ...

  9. (1)DBA查询:数据库

    1.数据库状态:[1]sys.databases   [2]exec sp_spaceused 2.数据文件状态:[1]sys.master_files [2]查看ldf与mdf:sp_helpfil ...

  10. 林兴爆料小程序很快可以支持各个 App 直接打开小程序

    在微信开放平台基础高级产品经理林兴演讲的当场,他爆料了微信小程序一个轰动性新能力:小程序很快可以支持各个 App 直接打开小程序!没错,你没有听错,简单来说,在不久以后,所有的 App 里面都可以看到 ...