使用Spring都需要通过applicationContext.xml来生成一个Spring上下文
这里讲述的是Hibernate的查询效率,而对Spring生成的上下文能大大的提高查询效率
使用Hibernate,一般都是通过Spring上下文获取SessionFactory,然后通过SessionFactory产生session对数据库的增、删、改、查等操作。
但是由于每次的操作,都需要产生一个Spring上下文,这就对效率产生影响
采用的做法是:单例模式
public AbstractApplicationContext getInstance(){
if(appContext == null){
appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
return appContext;
}
/*取得Session*/
public Session getCurrentSession(){
if(sessionFactory==null){
sessionFactory=(SessionFactory)this.getInstance().getBean("sessionFactory");
}
return sessionFactory.getCurrentSession();
}
测试的时候可以通过
long beforeTime = System.currentTimeMillis();
ink_OrganList = ink_OrganDao.findByParentCode(parentCode);
System.out.println(System.currentTimeMillis() - beforeTime);
对比着查看操作时间。