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);
最后要关闭连接
本文展示了如何使用Spring框架中的ORM(对象关系映射)功能与数据库进行交互,具体通过创建SQL查询来获取当前日期,并进行了日期的打印操作。
256

被折叠的 条评论
为什么被折叠?



