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 ...
随机推荐
- tensoflow模型中提示:ValueError: Variable rnn/basic_rnn_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? 解决办法
在利用tensorflow框架进行模型训练的时候,有时我们需要多次训练对结果求均值来得到一个均衡的评测结论.比如训练十次求平均值.但是tf的本质就是图,当变量第一次定义使用后,第二次再使用就是提示: ...
- linux:基本指令mkdir, rmdir 和rm
mkdir 建立文件夹 mkdir (make directory) 就是创建一个文件夹的意思, 使用起来很简单. 1.新建一个目录: $ mkdir folder2 如果你想在这个目录给 folde ...
- 子分区 复合分区 Subpartitioning
https://dev.mysql.com/doc/refman/8.0/en/partitioning-subpartitions.html
- 查找->静态查找表->分块查找(索引顺序表)
文字描述 分块查找又称为索引顺序查找,是顺序查找的一种改进方法.在此查找算法中,除表本身外, 还需要建立一个”索引表”.索引表中包括两项内容:关键字项(其值为该字表内的最大关键字)和指针项(指示该子表 ...
- es6学习笔记入门总结
1.let const block 作用域 let 代替var 来声明块级作用域,没有变量提升,只在块内有作用 const 可以声明一个常量,类似于指针,指向某一个引用,这个常量并非一成不变的,但是不 ...
- LeetCode 893 Groups of Special-Equivalent Strings 解题报告
题目要求 You are given an array A of strings. Two strings S and T are special-equivalent if after any nu ...
- 关于linux特殊含义的转义符\033
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- Servlet----------通过 HttpServlet 开发Servlet
通过继承HttpServlet抽象类,功能更强大. 通过HttpServlet方法开发Servlet需要重写doGet和doPost方法.这是目前用的最多的一种方法. 如: class MyHtt ...
- 移动端rem布局(阿里)
该方案使用相当简单,把下面这段已压缩过的 原生JS(源码已在文章底部更新,2017/5/3) 放到 HTML 的 head 标签中即可(注:不要手动设置viewport,该方案自动帮你设置) < ...
- dedecms怎样调用指定id文章?
前面我们聊了帝国cms如何调用指定id的文章到首页,作为同行的织梦cms应该也是可以实现的吧?那么,dedecms怎样调用指定id文章呢?使用idlist直接调用指定的ID这样的方法是比较好的.官方给 ...