ITeye博客

Spring获取SessionFactory

ctrain · 2013-04-26 17:38 · J2EE · Web前端 · Markdown


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);

最后要关闭连接

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。