Spring获取SessionFactory

String sql = "select sysdate from dual";
		
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 
String[] names = wac.getBeanDefinitionNames();
for(int i=0; i<names.length; i++){
	System.out.println("---"+names[i]);
	
}

SessionFactory sf  = (SessionFactory)wac.getBean("sessionFactory");   
Session session = sf.openSession();
SQLQuery query = session.createSQLQuery(sql);
//query.addScalar("visitorID", new org.hibernate.type.LongType());
List children = query.list(); 
Date currentDate = null;
currentDate = (Date)children.iterator().next(); 
System.out.println(currentDate);

最后要关闭连接

你可能感兴趣的:(sessionFactory)