01 | package top.dragong; |
02 |
03 | import org.hibernate.*; |
04 | import org.hibernate.cfg.*; |
05 | import org.hibernate.service.*; |
06 |
07 | /** |
08 | * Description: |
09 | * @author VipMao |
10 | * @version 1.0 |
11 | */ |
12 |
13 | /** |
14 | * 该工具类提供了一个属性:SessionFactory sessionFactory 并创建了sessionFactory 将它设置成static |
15 | * 这样其他程序就可以直接通过此工具类引用 提供了二个方法: 1:通过线程创建Session-->currentSession() |
16 | * 2:关闭Session-->closeSession() 需要在主类中手动关闭sessionFactory |
17 | */ |
18 | public class HibernateUtil { |
19 | private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml" ; |
20 | private static final ThreadLocal<Session> sessionThreadLocal = new ThreadLocal<Session>(); |
21 | private static Configuration configuration = new Configuration(); |
22 | private static SessionFactory sessionFactory; |
23 | private static String configFile = CONFIG_FILE_LOCATION; |
24 | static { |
25 | configuration.configure(); |
26 | ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) |
27 | .applySettings(configuration.getProperties()).buildServiceRegistry(); |
28 | sessionFactory = configuration.buildSessionFactory(serviceRegistry); |
29 |
30 | } |
31 |
32 | private HibernateUtil() { |
33 | } |
34 |
35 | public static SessionFactory getSessionFactory() { |
36 | return sessionFactory; |
37 | } |
38 |
39 | public static void rebuildSessionFactory() { |
40 | try { |
41 | configuration.configure(configFile); |
42 | ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() |
43 | .applySettings(configuration.getProperties()) |
44 | .buildServiceRegistry(); |
45 | sessionFactory = configuration.buildSessionFactory(serviceRegistry); |
46 | } catch (HibernateException e) { |
47 | e.printStackTrace(); |
48 | } |
49 | } |
50 |
51 | public static Session getSession() { |
52 | Session session = (Session) sessionThreadLocal.get(); |
53 | try { |
54 | if (session == null || !session.isOpen()) { |
55 | if (sessionFactory == null ) { |
56 | rebuildSessionFactory(); |
57 | } |
58 | } |
59 | session = (sessionFactory != null ) ? sessionFactory.openSession() : null ; |
60 | sessionThreadLocal.set(session); |
61 | } catch (HibernateException e) { |
62 | e.printStackTrace(); |
63 | } |
64 | return session; |
65 | } |
66 |
67 | public static void closeSession() { |
68 | Session session = (Session) sessionThreadLocal.get(); |
69 | sessionThreadLocal.set( null ); |
70 | try { |
71 | if (session != null && session.isOpen()) { |
72 | session.close(); |
73 | } |
74 |
75 | } catch (HibernateException e) { |
76 | e.printStackTrace(); |
77 | } |
78 |
79 | } |
80 |
81 | public static void setConfigFile(String configFile) { |
82 | HibernateUtil.configFile = configFile; |
83 | sessionFactory = null ; |
84 | } |
85 |
86 | public static Configuration getConfiguration() { |
87 | return configuration; |
88 | } |
89 | } |

13043860zj LV16
2020年8月20日
段朝洪 LV15
2019年11月25日
caozhou LV14
2019年3月11日
我的名字是凑得 LV8
2018年12月23日
wangmeng20 LV14
2018年11月1日
z980693651 LV10
2018年10月20日
wlzyf冰冰 LV13
2018年7月5日
gongjunjienb LV15
2018年6月2日
mkl_mzq LV10
2018年5月16日
812992358 LV11
2018年5月8日