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. 【Python基础】json.dumps()和json.loads()、json.dump()和json.load()的区分

    json文件处理涉及的四个函数json.dumps()和json.loads().json.dump()和json.load()的区分 一.概念理解 1.json.dumps()和json.loads ...

  2. zabbix宏(macro)使用:自定义监控阈值

    一.简单应用场景 zabbix在监控cpu load时并没有考虑客户端cpu的个数和核心数量,当平均5分钟的负载达到5时zabbix执行报警动作,这样是非常不合理的,笔者的被监控机器有四核和单核,现在 ...

  3. 使用Nginx搭建集群

    反向代理: 1.首先启动一个项目,启动后可以通过http://localhost:8080/getResult访问到接口,如图: 2.修改nginx配置文件,监听www.ouyan.com的80端口, ...

  4. 教你一步步composer安装Magento2.3

    以前外贸建站一直用zencart,这段时间ytkah比较有时间,就决定用magento来创建一下站点.magento不像普通的程序一样下载就可以直接安装,需要借助composer安装,还没没compo ...

  5. Java基础知识(重载和覆盖)

    重载(overload): 在一个类中,如果出现了两个或者两个以上的同名函数,只要它们的参数的个数,或者参数的类型不同,即可称之为该函数重载了. 即当函数同名时,只看参数列表.和返回值类型没关系. 重 ...

  6. 001-分布式理论-CAP定理

    一.概述 CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性)这三个基本需求 ...

  7. redis.conf密码设置的问题

    requirepass是终端客户端登录需要的密码,配置在服务端 masterauth是从服务器端登录master端需要的密码,配置在从服务端

  8. elemet-paging

    <template> <card-layout :title="L('Prosuct')" :actions="actions" @click ...

  9. vue-父组件向子组件传递方法

    1.父组件向子组件传递方法,使用的是事件绑定机制 v-on:传递给子组件的方法名=“父组件中的方法”

  10. [py]类的专有方法

    陆陆续续总结一些用到的类的特殊方法 看源码总会看到一些奇奇怪怪的写法: 掺杂着设计模式 https://coding.net/u/RuoYun/p/Python-design-pattern/git/ ...