zoukankan      html  css  js  c++  java
  • hibernate 启动和辅助类实现资源的重复使用

    来自API:
    1.2.5.  启动和辅助类

    是时候来加载和储存一些Event对象了,但首先我们得编写一些基础的代码以完成设置。我们必须启动Hibernate,此过程包括创建一个全局的SessoinFactory,并把它储存在应用程序代码容易访问的地方。SessionFactory可以创建并打开新的Session。一个Session代表一个单线程的单元操作,SessionFactory则是个线程安全的全局对象,只需要被实例化一次。

    我们将创建一个HibernateUtil辅助类(helper class)来负责启动Hibernate和更方便地操作SessionFactory。让我们来看一下它的实现:

    package util;
    
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    
    public class HibernateUtil {
    
        private static final SessionFactory sessionFactory;
    
        static {
            try {
                // Create the SessionFactory from hibernate.cfg.xml
                sessionFactory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }
    
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
    }
  • 相关阅读:
    ApplicationContext.xml修改
    springmvc.xml约束
    log4j.properties
    SqlMapConfig.xml配置文件
    Mybatis注解式开发坐标
    字符串函数
    vim基础快捷键
    format的使用
    lambda匿名函数
    字典的基础使用
  • 原文地址:https://www.cnblogs.com/flying607/p/3474394.html
Copyright © 2011-2022 走看看