ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ package utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory sf; static { // 1. 创建,调用空参构造 Configuration conf = new Configuration().configure(); // 2. 根据配置信息,创建SessionFactory对象 sf = conf.buildSessionFactory(); } // 获得session => 获得全新session public static Session openSession() { Session session = sf.openSession(); return session; } // 获得session => 获得与线程绑定的session public static Session getCurrentSession() { // 获得session Session session = sf.getCurrentSession(); return session; } public static void main(String[] args) { //测试下 System.out.println(HibernateUtils.openSession()); } } ~~~