zoukankan      html  css  js  c++  java
  • hibernateUtils

    hibernate3.x版本

     1 package hibernate_01;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.hibernate.cfg.Configuration;
     6 
     7 public class hibernateUtils {
     8     private static Configuration cfg = null;
     9     private static SessionFactory sessionFactory = null;
    10     private static Session session = null;
    11     static{
    12         cfg = new Configuration().configure();
    13         sessionFactory = cfg.buildSessionFactory();
    14     }
    15     public static Session getSession(){
    16         if(sessionFactory!=null){
    17             session = sessionFactory.openSession();
    18         }else{
    19             sessionFactory = cfg.buildSessionFactory();
    20         }
    21         return session = sessionFactory.openSession();
    22     }
    23     public static void closeSession(){
    24         if(session != null && session.isOpen()){
    25             session.close();
    26         }
    27     }
    28 }

    hibernate4.x

     1 package cn.siggy.util;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
     6 import org.hibernate.cfg.Configuration;
     7 
     8 public class HibernateUtil {
     9     private static Configuration cfg=null;
    10     private static SessionFactory factory=null;
    11     private static Session session = null;
    12     static{
    13         cfg = new Configuration().configure();
    14         factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
    15         .applySettings(cfg.getProperties()).build());
    16     }
    17     public static Session getSession(){
    18         if(factory!=null)
    19             return factory.openSession();
    20         factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
    21         .applySettings(cfg.getProperties()).build());
    22         return session = factory.openSession();
    23     }
    24     public static void closeSession(){
    25         if(session!=null&&session.isOpen())
    26             session.close();
    27     }
    28 }
  • 相关阅读:
    纸壳CMS替换默认实现
    ASP.Net Core 2.2 InProcess托管的Bug:unable to open database file
    GitHub设置使用SSH Key,用TortoiseGit进行Clone仓库
    在Docker中运行纸壳CMS并配置使用MySql
    使用vs code开发纸壳CMS并启用Razor智能提示
    ASP .Net Core路由(Route)
    .Net Core在Middleware中解析RouteData
    纸壳CMS可视化建站系统搭建多语言网站
    Redis的初识
    C#/Java 动态生成电子发票
  • 原文地址:https://www.cnblogs.com/jiangjianzhu/p/5549342.html
Copyright © 2011-2022 走看看