J2EE之巔

           

          Spring2.0與Hibernate3整合

          ????????????????????????????????????????????????????? 蔡超

          北京天融信,軟件架構(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 都是如今流行的請量級框架,如何將兩者進(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 實體

          方法一:采用 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>

          客戶端代碼:

          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 超越巔峰 閱讀(6507) 評論(5)  編輯  收藏 所屬分類: Java EE

          評論

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

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

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

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

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

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

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

          既然都是spring 2.0了 沒有體現(xiàn)aop的強(qiáng)大 感覺題目有些過了  回復(fù)  更多評論   

          # re: Spring2.0與Hibernate3整合 2007-02-06 09:22 看客

          暈了`~就這  回復(fù)  更多評論   

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(12)

          隨筆分類(54)

          隨筆檔案(59)

          文章分類(2)

          文章檔案(1)

          相冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 禄劝| 修水县| 宣化县| 比如县| 策勒县| 平舆县| 扶风县| 成武县| 定安县| 闻喜县| 南靖县| 莱阳市| 绍兴市| 孙吴县| 政和县| 土默特左旗| 基隆市| 台北市| 泾源县| 噶尔县| 巴东县| 焦作市| 龙游县| 贵德县| 武宣县| 庆云县| 阜康市| 九寨沟县| 鄂州市| 道真| 梓潼县| 昌平区| 沙洋县| 乃东县| 沛县| 宁德市| 即墨市| 枣阳市| 芒康县| 黄龙县| 镇安县|