小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring2.0與Hibernate3整合 - J2EE之巔 - BlogJava

 nbtymm 2007-02-06

 

                                                      蔡超

北京天融信,軟件架構(gòu)師

SUN certified Enterprise Architect

Microsoft certified Solution Developer

IBM certified RUP Specialist

聯(lián)系方式 :cai_chao@topsec.com.cn,chaocai2001@yahoo.com.cn

010-82776427

 

Sping2 Hibernate3 都是如今流行的請(qǐng)量級(jí)框架,如何將兩者進(jìn)行整合呢,目前很多資料討論的都是老版本的整合方式,下面給出這兩種框架的新版整合方式。

1 配置數(shù)據(jù)源

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    <property name="driverClassName">

        <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>

    </property>

    <property name="url">

        <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webguide</value>

    </property>

    <property name="username">

        <value>sa</value>

    </property>

    <property name="password">

        <value>talent</value>

    </property>

   </bean>

2 配置 Hibernate SessionFactory

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="mappingResources">

       <!—Hibernate 映射文件列表 -->

           <list>

               <value>Greeting.hbm.xml</value>

           </list>

       </property>

       <property name="hibernateProperties">

           <props>

               <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="hbm2ddl.auto">update</prop>

           </props>

       </property>

       <property name="dataSource">

           <ref bean="myDataSource"/>

       </property>

      

   </bean>

3 配置事務(wù)

  <bean id="myTranAttri" class="org.springframework.transaction.interceptor.DefaultTransactionAttribute">

    <property name="propagationBehaviorName">

        <value>PROPAGATION_REQUIRED</value>

    </property>

    </bean>

    <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">

    <property name="transactionAttribute">

        <ref bean="myTranAttri"/>

    </property>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

         <property name="sessionFactory">

            <ref bean="mySessionFactory"/>

        </property>

    </bean>

          

    <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" lazy-init="true">

       <property name="transactionManager">

           <ref bean="transactionManager"/>

       </property>

       <property name="transactionAttributeSource">

           <ref bean="transactionAttributeSource"/>

       </property>

    </bean>

 

4 使用 Hibernate 實(shí)體

方法一:采用 AOP 的方式 package hibernate.integration.entity;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.SessionFactoryUtils;

 

/**

 * @author chao cai

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class GreetingLogic3 implements IGreetingLogic{

    private SessionFactory sessionFactory;

    /* (non-Javadoc)

     * @see hibernate.integration.entity.IGreetingLogic#loadGreeting(java.lang.Integer)

     */

    public Greeting loadGreeting(Integer id) {

       Session session = sessionFactory.getCurrentSession();

       Greeting greeting=(Greeting) session.get(Greeting.class,id);

       return greeting;

   

    }

 

 

    /**

     * @return Returns the sessionFactory.

     */

    public SessionFactory getSessionFactory() {

       return sessionFactory;

    }

    /**

     * @param sessionFactory The sessionFactory to set.

     */

    public void setSessionFactory(SessionFactory sessionFactory) {

       this.sessionFactory = sessionFactory;

    }

}

代碼樣例:

package hibernate.integration.entity;

 

/**

 * @author chao cai

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public interface IGreetingLogic {

    public Greeting loadGreeting(Integer id);

}

 

配置

    <bean id="myGreetingLogic" class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="proxyInterfaces">

            <value>hibernate.integration.entity.IGreetingLogic</value>

        </property>

        <property name="interceptorNames">

            <list>

                <value>txInterceptor</value>

                <value>myHibernateInterceptor</value>

             

            </list>

        </property>

        <property name="target">

         <ref bean="greetingLogic3"/>

        </property>

</bean>

客戶(hù)端代碼:

AbstractApplicationContext context=new FileSystemXmlApplicationContext("spring-hibernate.xml");

IGreetingLogic gl=(IGreetingLogic) context.getBean("myProductDao");

       greeting=gl.loadGreeting(new Integer(1));

       System.out.println(greeting.getGreeting());

 

方法二:采用 HibernateDaoSupport

代碼:

package hibernate.integration.entity;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.SessionFactoryUtils;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

/**

 * @author Chao Cai

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class GreetingLogic extends HibernateDaoSupport implements IGreetingLogic{

   

    public Greeting loadGreeting(Integer id){

       Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);

       Greeting greeting=(Greeting) session.get(Greeting.class,id);

       return greeting;

      

    }

}

    <bean id="greetingLogic" class="hibernate.integration.entity.GreetingLogic">

          <property name="sessionFactory">

            <ref bean="mySessionFactory"/>

        </property>

    </bean>

posted on 2007-02-05 17:15 超越巔峰 閱讀(520) 評(píng)論(4)  編輯 收藏 引用 網(wǎng)摘 所屬分類(lèi): Java EE

評(píng)論

# re: Spring2.0與Hibernate3整合 2007-02-05 20:19 anikin

為何不用<aop:config>和<tx:advice>來(lái)配置事務(wù)呢?難道這是你說(shuō)的老版本?  回復(fù)  更多評(píng)論   

# re: Spring2.0與Hibernate3整合 2007-02-05 21:53 Anubis

先保存,有空再學(xué)習(xí)  回復(fù)  更多評(píng)論   

# re: Spring2.0與Hibernate3整合[未登錄](méi) 2007-02-05 22:45 role0523

Spring 2.0增加了很多Schema的標(biāo)記,用起來(lái)比以前順手多了。
事務(wù)聲明中使用<aop:config>和<tx:advice>可以節(jié)省很多筆墨。  回復(fù)  更多評(píng)論   

# re: Spring2.0與Hibernate3整合 2007-02-06 09:13 learner

既然都是spring 2.0了 沒(méi)有體現(xiàn)aop的強(qiáng)大 感覺(jué)題目有些過(guò)了

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約