Hibernate.基础篇《一》.Hibernate工具类.
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工具类.的更多相关文章
- Hibernate.基础篇《二》. getOpenSession() 和 getCurrentSession() - 1
Hibernate.基础篇<二>. getOpenSession() 和 getCurrentSession() - 1 说明: 在Hibernate应用中,Session接口的使用最为广 ...
- hibernate hql where语句拼接工具类
package com.zhaoshijie.tree.other; /** * hibernate HQL WHERE语句工具类 * * @author 赵士杰 * */public class H ...
- Hibernate基础学习(二)—Hibernate相关API介绍
一.Hibernate的核心接口 所有的Hibernate应用中都会访问Hibernate的5个核心接口. (1)Configuration接口: 配置Hibernate,启动Hi ...
- Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】
对Lucene代码优化 我们再次看回我们上一篇快速入门写过的代码,我来截取一些有代表性的: 以下代码在把数据填充到索引库,和从索引库查询数据的时候,都出现了.是重复代码! Directory dire ...
- Hibernate入门篇<1>hibernate.cfg.xml学习小结
Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性,这个配置文件应该位于应用程序或Web程序的类文件夹 classes中.Hibernate配置文件支持两种形式, ...
- cocos2dx基础篇(3) 常用重要类
---------------------------------------- 入口类main.cpp 主要控制类AppDelegate.cpp -------------------------- ...
- 基础篇:JAVA引用类型和ThreadLocal
前言 平时并发编程,除了维护修改共享变量的场景,有时我们也需要为每一个线程设置一个私有的变量,进行线程隔离,java提供的ThreadLocal可以帮助我们实现,而讲到ThreadLocal则不得不讲 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- JAVA基础学习day17--集合工具类-Collections
一.Collection简述 1.1.Collection与Collections的区别 Collections是集合的静态工具类 Collection:是集合的顶级接口 二.Sort 2.1.sor ...
随机推荐
- [No0000D7]img生成器.bat合并所有图片到html网页中
@ECHO OFF IF EXIST %~nx0.html DEL /F /Q %~nx0.html ECHO ^<html^>^<head^>^<title^>% ...
- sess.run(tf.global_variables_initializer()) 做了什么?
当我们训练自己的神经网络的时候,无一例外的就是都会加上一句 sess.run(tf.global_variables_initializer()) ,这行代码的官方解释是 初始化模型的参数.那么,它到 ...
- 一种比较简单的实现ping的方式
<span style="font-family: Arial, Helvetica, sans-serif;">头文件</span> <span s ...
- ORACLE之PACKAGE-包、存储过程、函数
1,简单的包. 创建包规范: create or replace package pack_test1 is -- 定义过程1 procedure p_test1(p_1 in varchar2); ...
- falsk 与 django 钩子方法
falsk 四大钩子方法# 在第一次请求之前调用@app.before_first_requestdef before_first_request(): print("这是第一次请求之前调用 ...
- ios禁止页面下拉
document.querySelector('body').addEventListener('touchmove', function(e) { e.preventDefault(); } ...
- 原生js获取子元素、给元素增加div
//鼠标移入移出动画 解决页面闪屏问题 window.onload = function () { var el = document.createElement('div'); el.classNa ...
- RN全局的变量,方法,全局类,全局类方法
为了方便学习,很简单的小Demo,不懂可以下方留言,百分百原创,相互学习,相互进步 全局的方法 创建一个js文件,命名OvallAll //全局的方法 //这里export default 只能输出一 ...
- centos6.8上yum安装zabbix3.2
centos6.8上yum安装zabbix3.2 zabbix3.2安装文档:https://www.zabbix.com/documentation/3.2/manual/installation/ ...
- (转)github设置添加SSH
很多朋友在用github管理项目的时候,都是直接使用https url克隆到本地,当然也有有些人使用 SSH url 克隆到本地.然而,为什么绝大多数人会使用https url克隆呢? 这是因为,使用 ...