您的位置:程序门 -> java -> j2ee / ejb / jms



applicationcontext.xml的问题,谢了!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


applicationcontext.xml的问题,谢了!
发表于:2007-09-07 00:57:15 楼主
为什么   在applicationcontext.xml文件中   需要用
第一步骤:
<bean   id= "hibernatetemplate "
class= "org.springframework.orm.hibernate3.hibernatetemplate ">
          <property   name= "sessionfactory ">
                  <ref   bean= "sessionfactory "   />
          </property>
</bean>
第二步骤
<bean   id= "isubitemdaoimpl "
class= "org.lxh.myzngt.dao.impl.isubitemdaoimpl "
parent= "isubitemdao ">
        <property   name= "hibernatetemplate ">
                <ref   bean= "hibernatetemplate "   />
        </property>
</bean>
用上面这样有什么好处吗,是不是上面可以自动关闭session,我以前都是用下面这样的啊
<bean   id= "isubitemdaoimpl "
class= "org.lxh.myzngt.dao.impl.isubitemdaoimpl "
parent= "isubitemdao ">
        <property   name= "sessionfactory ">
                <ref   bean= "sessionfactory "   />
        </property>
</bean>
发表于:2007-09-07 15:52:001楼 得分:0
引用spring   2.0文档~~~~~~
http://www.redsaga.com/spring_ref/2.0/html/orm.html#d0e14411
12.2.3.     hibernatetemplate  
对于特定的数据访问对象或业务对象的方法来说,基本的模板编程模型看起来像下面所示的代码那样。   对于这些外部对象来说,没有任何实现特定接口的要求,仅仅要求提供一个hibernate   sessionfactory。  

hibernatetemplate   会确保当前hibernate的   session   对象的正确打开和关闭,并直接参与到事务管理中去。   template实例不仅是线程安全的,同时它也是可重用的。因而他们可以作为外部对象的实例变量而被持有。对于那些简单的诸如find、load、saveorupdate或者delete操作的调用,hibernatetemplate   提供可选择的快捷函数来替换这种回调的实现。
发表于:2007-09-07 15:53:082楼 得分:0
public   class   productdaoimpl   implements   productdao   {

        private   sessionfactory   sessionfactory;

        public   void   setsessionfactory(sessionfactory   sessionfactory)   {
                this.sessionfactory   =   sessionfactory;
        }

        public   collection   loadproductsbycategory(final   string   category)   throws   dataaccessexception   {
                hibernatetemplate   ht   =   new   hibernatetemplate(this.sessionfactory);
                return   (collection)   ht.EXECute(new   hibernatecallback()   {
                        public   object   doinhibernate(session   session)   throws   hibernateexception   {
                                query   query   =   session.createquery(
                                        "from   test.product   product   where   product.category=? ");
                                query.setstring(0,   category);
                                return   query.list();
                        }
                });
        }
}
发表于:2007-09-07 21:00:123楼 得分:0
谢谢你的回复,想必楼上的兄弟一定是个高手吧


快速检索

最新资讯
热门点击