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. [No0000F8]override和new的区别

    override 1. override是派生类用来重写(或覆盖)基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract. ...

  2. tensorRT 构建推理引擎

  3. webkit下面的CSS设置滚动条

    webkit下面的CSS设置滚动条 1.主要有下面7个属性: ::-webkit-scrollbar 滚动条整体部分,可以设置宽度啥的 ::-webkit-scrollbar-button 滚动条两端 ...

  4. select下拉菜单实现通过数据库查询来设置默认值

    查询网上各种资料要不比较难理解,要么有问题,现有一种简单通俗的理解方法 思路:读取数据库数据1,数据2需用到select选择菜单,但是又想每次查看是都显示读数据库的默认信息 demo: {% for ...

  5. tensorflow入门笔记(三) tf.GraphKeys

    tf.GraphKeys类存放了图集用到的标准名称. 该标准库使用各种已知的名称收集和检索图中相关的值.例如,tf.Optimizer子类在没有明确指定待优化变量的情况下默认优化被收集到tf.Grap ...

  6. 洛谷P2633 Count on a tree 主席树

    传送门:主席树 解题报告: 传送门! umm这题我还麻油开始做 所以 先瞎扯一波我的想法,如果错了我就当反面教材解释这种典型错误,对了我就不管了QwQ 就直接dfs,在dfs的过程中建树 然后就直接查 ...

  7. bug:使用UIImageView+AFNetworking 图片不能正常显示的原因

    今天调的东西涉及到图片加载,我刚看了下项目里以前导入了SDWebImage库,又发现整个就一个地方使用到了SDWebImage异步加载图片的方法,感觉占体积又鸡肋,干脆去掉,用UIImageView+ ...

  8. Requirejs 使用

    代码地址 参考地址1 参考地址2 一.不依赖其他模块的module创建 创建math的module // math.js define(function (){ var add = function ...

  9. 【Mock】mock基础、简单的单元测试代码练习。

    说到接口测试,必问 mock,mock 通俗一点来说就是模拟接口返回.解决接口的依赖关系,主要是为了解耦,单元测试用的多. 什么是Mock unittest.mock 是一个用于在 Python 中进 ...

  10. sql server误删数据恢复delete(低效版)

    关键词:sql server误删数据恢复,mssql误删数据恢复,delete --切换数据库 use master --构建函数 Create PROCEDURE Recover_Deleted_D ...