hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<hibernate-configuration><session-factory><!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于查错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率--><property name="show_sql">true</property><property name="format_sql">true</property><!--设置数据库的连接url:jdbc:mysql://localhost/student,其中localhost表示mysql服务器名称,此处为本机,student是数据库名--><property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序--> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property><!--连接数据库的用户名和密码--><property name="connection.username">root</property><property name="connection.password">root</property><!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接哪种类型的数据库服务器。是必须的,用户配置Hibernate使用不同的数据库类型--> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property><property name="current_session_context_class">thread</property> <!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。FetchSize设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->  <!-- <property name="jdbc.fetch_size">50</property> --><!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。BatchSize越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->  <!-- <property name="jdbc.batch_size">23</property> --><!--jdbc.use_scrollable_resultset是否允许Hibernate用JDBC的可滚动的结果集。对分页的结果集。对分页时的设置非常有帮助-->  <!-- <property name="jdbc.use_scrollable_resultset">false</property> --><!--数据库连接池的大小-->  <property name="hibernate.connection.pool.size">20</property><property name="hibernate.hbm2ddl.auto">update</property></session-factory>
</hibernate-configuration>

HibernateSessionFactory

public class HibernateSessionFactory {// 指定Hibernate配置文件路径private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";// 创建ThreadLocal对象private static final ThreadLocal<Session> sessionThreadLocal = new ThreadLocal<Session>();// 创建Configuration对象private static Configuration configuration = new Configuration();// 定义SessionFactory对象private static SessionFactory sessionFactory;// 定义configFile属性并复制private static String configFile = CONFIG_FILE_LOCATION;// 静态代码块来启动Hibernate,该类被加载时执行一次,用于创建SessionFactory。所以SessionFactory是线程安全的,只能被实例化一次.static {try {StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();sessionFactory = metadata.getSessionFactoryBuilder().build();} catch (HibernateException e) {e.printStackTrace();}}//创建无参的HibernateSessionFactory构造方法private HibernateSessionFactory() {}//获得SessionFactory对象(sessionFactory的get方法)public static SessionFactory getSessionFactory() {return sessionFactory;}//重建SessionFactorypublic static void rebuildSessionFactory() {synchronized (sessionFactory) {try {StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure(configFile).build();Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();sessionFactory = metadata.getSessionFactoryBuilder().build();} catch (HibernateException e) {// TODO: handle exceptione.printStackTrace();}}}//获得Session对象public static Session getSession() {// 获得ThreadLocal对象管理的session对象Session session = sessionThreadLocal.get();try {// 判断Session对象是否已经存在或是打开if (session == null || !session.isOpen()) {// 如果Session对象为空或未打开,再判断sessionFactory对象是否为空if (sessionFactory == null) {// 如果sessionFactory为空,创建SessionFactoryrebuildSessionFactory();}// 如果sessionFactory不为空,则打开Sessionsession = (sessionFactory != null) ? sessionFactory.openSession() : null;sessionThreadLocal.set(session);}} catch (HibernateException e) {// TODO: handle exceptione.printStackTrace();}return session;}//关闭Session对象public static void closeSession() {Session session = sessionThreadLocal.get();sessionThreadLocal.set(null);try {if (session != null && session.isOpen()) {session.close();}} catch (HibernateException e) {// TODO: handle exceptione.printStackTrace();}}//configFile 属性的set方法public static void setConfigFile(String configFile) {HibernateSessionFactory.configFile = configFile;sessionFactory = null;}//configuration 属性的get方法public static Configuration getConfiguration() {return configuration;}
}

Hibernate之代码创建SessionFactory相关推荐

  1. [Hibernate系列—] 2. 创建SessionFactory 与 Session

    Configuration 对象创建 要创建SessionFactory , 首先要创建Configuration 对象. 这个对象就是去读取hibernate 的一些配置信息. 默认状况下, hib ...

  2. Hibernate的执行流程——SessionFactory的创建

    Hibernate的执行流程: 1.创建Configuration类实例,用来读取并解析配置文件(如Hibernate.cfg.xml),一个Configuration实例代表hibernate所有P ...

  3. Hibernate初始化创建SessionFactory,Session,关闭SessonFactory,session

    1.hibernate.cfg.xml 1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configur ...

  4. Hibernate5新的创建SessionFactory方式,使用Hibernate4的方式报异常XXX is not mapped

    Hibernate4创建SessionFactory的方式 Configuration configuration = new Configuration().configure();ServiceR ...

  5. 实例演示如何在spring4.2.2中集成hibernate5.0.2并创建sessionFactory

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/49388209 文章作者:苏生米沿 本文目的:使用spring4.2.2集成hibern ...

  6. 使用Struts2,Hibernate和MySQL创建个人MusicManager Web应用程序的研讨会

    概述: 在本研讨会教程中,我们将使用Struts 2,Hibernate和MySQL数据库开发一个个人音乐管理器应用程序. 该Web应用程序可用于将您的音乐收藏添加到数据库中. 我们将显示用于添加唱片 ...

  7. Hibernate开发之创建POJO-配置文件-映射文件

    目录: 1.确定已在oracle数据库中建表 1.1.查询表 1.2.删除表中已有记录 1.3.确定已有sequence 2.创建java project工程 创建class Student 2.1 ...

  8. AndroidStudio git 提交代码,创建分支,合并分支,回滚版本,拉取代码

    主要有: 提交代码,创建分支,合并分支,回滚版本,拉去代码 1 首先电脑中下载git 2 新建的项目把.git 仓库放到项目总中as 工具的右下角 会显示 Git:master 点击有一个弹框如下 然 ...

  9. akka actor java_Akka:使用非默认构造函数在Scala中定义一个actor并从Java代码创建它 - java...

    Akka Scala演员必须扩展akka.actor.Actor Akka Java actor必须扩展akka.actor.UntypedActor 因此,在使用非默认构造函数定义Scala act ...

最新文章

  1. MySQL的存储引擎与日志说明
  2. ES6/7 异步编程学习笔记
  3. 面试题及答案_NET
  4. 在Centos 7中开放80端口
  5. python目录下的文件夹_Python列出当前文件夹下文件的两种方法
  6. 万能钥匙也不能解开的wifi?那用Python帮你轻松解决
  7. 对抗恶意程序的反虚拟化,百度安全提最新检测技术,具备三大特性
  8. 【HDOJ7055】Yiwen with Sqc(字符串,区间出现次数平方和,两次差分)
  9. 百叶窗叶片锋利,不安全
  10. ARM base instruction -- lsl asl lsr asr ror rrx
  11. matlab2010b数值分析,matlab2010b教程
  12. NMEA-0183 协议
  13. 重要知识结构-持续更新中
  14. javascript小方法之数组去重、数字转成逗号分隔、html元素去标签
  15. LA 4490 Help Bubu
  16. 用matlab求一组数据的分布函数,求任意一组数据的概率密度函数
  17. 【论文笔记】知识图谱推理PRA——Relational retrieval using a combination of path-constrained random walks
  18. Vijos 1836题:HYS与七夕节大作战
  19. 什么是 JWT -- JSON WEB TOKEN
  20. Style Transfer(PyTorch)

热门文章

  1. 关于计算机的英语小品,幽默英语小品剧本
  2. 计算机组成原理 原码一位乘法(C语言实现)
  3. matlab 画三维图 及 画图
  4. android缓存策略跟cdn,缓存学习(五)CDN缓存(下)-CDN缓存策略、CDN缓存和浏览器缓存之间的关系、回源和回源比...
  5. 13篇基于Anchor free的目标检测方法
  6. vue管理后台项目中使用wangEditor富文本编辑器
  7. mysql导出建库语句_mysql建库建表,导出表结构
  8. 阅读笔记:养女儿的四大要点!
  9. 欢度中秋节!从零开始实现一个月饼检测器
  10. java3d圆柱曲面_Java3D毕业设计---基于JAVA3D的复杂曲面创意设计.doc