HibernateUtil类(可用于连接多个数据库)

Code: 我常用的HibernateUtil类,嘿嘿

  1. public final class HibernateUtil{  
  2.     private static SessionFactory sessionFactory;  
  3.   
  4.   
  5.     private HibernateUtil(){  
  6.     }  
  7.   
  8.   
  9.     //初始化  
  10.     static{  
  11.       Configuration cfg = new Configuration();  
  12.           cfg.configure();  
  13.       sessionFactory = cfg.buildSessionFactory();  
  14.     }      
  15.       
  16.     public static SessionFactory getSessionFactory(){  
  17.     return sessionFactory;  
  18.     }  
  19.      
  20.     public static Session getSession(){  
  21.     return sessionFactory.openSession();  
  22.     }  
  23. }  

上面是单数据库的HibernateUtil,Hibernate.cfg.xml文件默认在classpath下(src文件夹下)。

我自己写了个可以解析多个Hibernate.cfg.xml文件,访问多个数据库的HibernateUtil类(这就意味着你的项目可以使用多个数据库啊,不过对于多个数据库的事务管理我还不清楚,还是有点问题):

Code:

  1. import org.hibernate.Query;  
  2. import org.hibernate.Session;  
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.cfg.Configuration;  
  5.   
  6. public final class HbnUtil {// 根据hibernate.cfg.xml创建一个静态sessionFactory  
  7.   
  8.     private static SessionFactory sf;  
  9.     private static SessionFactory MSsf;  
  10.     static {  
  11.         try {  
  12.             sf = new Configuration().configure("/hbn-sqlserver.cfg.xml") 
  13.                     .buildSessionFactory();  
  14.             MSsf = new Configuration().configure("/hbn-mysql.cfg.xml") 
  15.                     .buildSessionFactory();  
  16.         } catch (Exception e) {  
  17.             System.err.println("%%%% Error Creating SessionFactory %%%%");  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.   
  22.     public static SessionFactory getSessionFactory() {  
  23.         return sf;  
  24.     }  
  25.   
  26.     public static Session getSession() {  
  27.         Session s = null;  
  28.         if (!sf.isClosed())  
  29.             s = sf.openSession();  
  30.         return s;  
  31.     }  
  32.   
  33.     public static Session getSessionByDB(String DBName) {// 根据DBName判断  
  34.         Session s = null;  
  35.   
  36.         if ("mysql".equals(DBName.toLowerCase())) {  
  37.             if (!MSsf.isClosed())  
  38.                 s = MSsf.openSession();  
  39.         } else if ("sqlserver".equals(DBName.toLowerCase())) {  
  40.             if (!sf.isClosed())  
  41.                 s = sf.openSession();  
  42.         } else {  
  43.             System.out.println("错误的 DBName!");  
  44.         }  
  45.   
  46.         return s;  
  47.     }  
  48.   
  49.     public static void closeSessionFactory() {  
  50.         if (!sf.isClosed()) {  
  51.             sf.close();  
  52.         }  
  53.     }  
  54.   
  55.     public static void closeSessionFactoryByDB(String DBName) {  
  56.   
  57.         if ("mysql".equals(DBName.toLowerCase())) {  
  58.             if (!MSsf.isClosed()) {  
  59.                 MSsf.close();  
  60.             }  
  61.         } else if ("sqlserver".equals(DBName.toLowerCase())) {  
  62.             if (!sf.isClosed()) {  
  63.                 sf.close();  
  64.             }  
  65.         } else {  
  66.             System.out.println("错误的 DBName!");  
  67.         }  
  68.     }  
  69.   
  70.   
  71. }  

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值