静态方法不能获取非静态属性
private static SessionFactory sessionFactory; // 会话工厂属性 // 构造方法私有化,保证单例模式 private MyHibernateSessionFactory(){} // 公有的静态方法,获得会话工厂对象 public static SessionFactory getSessionFactory(){ if(sessionFactory==null){ Configuration config = new Configuration().configure(); ServiceReqistry serviceReqistry = new ServiceReqistryBuilder().applySettings(config.getProperties()).buildServiceReqistry(); sessionFactory = config.buildSessionFactory(serviceReqistry); return sessionFactory; } else{ return sessionFactory; } }
单例SessionFactory