J2EE之巔

           

          Spring2.0與Hibernate3整合

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

          北京天融信,軟件架構師

          SUN certified Enterprise Architect

          Microsoft certified Solution Developer

          IBM certified RUP Specialist

          聯系方式 :cai_chao@topsec.com.cn,chaocai2001@yahoo.com.cn

          010-82776427

          ?

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

          1 配置數據源

          ??? <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 配置事務

          ? <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>來配置事務呢?難道這是你說的老版本?  回復  更多評論   

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

          先保存,有空再學習  回復  更多評論   

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

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

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

          既然都是spring 2.0了 沒有體現aop的強大 感覺題目有些過了  回復  更多評論   

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

          暈了`~就這  回復  更多評論   

          導航

          統計

          常用鏈接

          留言簿(12)

          隨筆分類(54)

          隨筆檔案(59)

          文章分類(2)

          文章檔案(1)

          相冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吴川市| 河北区| 巨鹿县| 乐至县| 龙里县| 嘉荫县| 柘城县| 泸水县| 兴隆县| 祁门县| 禹城市| 四会市| 涟源市| 兴安县| 东平县| 南陵县| 扶余县| 金秀| 枣强县| 保德县| 奇台县| 武功县| 丹巴县| 晋中市| 克拉玛依市| 东城区| 柯坪县| 垦利县| 石河子市| 康定县| 宣武区| 永定县| 金堂县| 玉门市| 龙江县| 于都县| 新建县| 建阳市| 安徽省| 蚌埠市| 公主岭市|