posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          spring 與 hibernate 整合(事務)

          Posted on 2008-05-09 13:47 G_G 閱讀(3157) 評論(0)  編輯  收藏 所屬分類: hibernate 、Spring 、AOP
          參考:第?9?章?事務管理 - Spring Framework reference 2.0.5 參考手冊中文版
          http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09.html

          先從配置文件開始:
          源碼:springAop.rar

          需要jar
          <?xml?version="1.0"?encoding="UTF-8"?>
          <classpath>
          ????
          <classpathentry?kind="src"?path="java"/>
          ????
          <classpathentry?kind="con"?path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
          ????
          <classpathentry?kind="lib"?path="lib/aspectjrt.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/aspectjweaver.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/spring.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/spring-sources.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/commons-logging-1.0.4.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/cglib-nodep-2.1_3.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/hibernate3.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/log4j-1.2.11.jar"/>
          ????
          <classpathentry?kind="con"?path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
          ????
          <classpathentry?kind="lib"?path="lib/dom4j-1.6.1.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/commons-collections-2.1.1.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/mysql.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/jta.jar"/>
          ????
          <classpathentry?kind="lib"?path="lib/antlr-2.7.6.jar"/>
          ????
          <classpathentry?kind="output"?path="bin"/>
          </classpath>


          spring 配置
          <?xml?version="1.0"?encoding="UTF-8"?>
          <beans?xmlns="http://www.springframework.org/schema/beans"
          ????xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
          ????xmlns:aop
          ="http://www.springframework.org/schema/aop"
          ????xmlns:tx
          ="http://www.springframework.org/schema/tx"
          ????xsi:schemaLocation
          ="http://www.springframework.org/schema/beans?
          ??http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          ??http://www.springframework.org/schema/aop?
          ??http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
          ??http://www.springframework.org/schema/tx?
          ??http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
          >

          ????
          <!--?demo?start?-->
          ????
          <import?resource="demo_spring.xml"?/>
          ????
          <!--?demo?end?-->

          ????
          <bean?id="dataSource"
          ????????class
          ="org.springframework.jdbc.datasource.DriverManagerDataSource">
          ????????
          <property?name="driverClassName"
          ????????????value
          ="com.mysql.jdbc.Driver">
          ????????
          </property>
          ????????
          <property?name="url"
          ????????????value
          ="jdbc:mysql://localhost:3306/aop?characterEncoding=utf8">
          ????????
          </property>
          ????????
          <property?name="username"?value="root"></property>
          ????????
          <property?name="password"?value=""></property>
          ????
          </bean>

          ????
          <!--?hibernate3?sessionFactory??-->
          ????
          <bean?id="sessionFactory"
          ????????class
          ="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          ????????
          <!--?此次??為?spring?事務需要使用?dataSource?;為空事務由Hibernian自己維護?-->
          ????????
          <property?name="dataSource"?ref="dataSource"?/>
          ????????
          <property?name="configLocation"
          ????????????value
          ="classpath:hibernate.cfg.xml"?/>
          ????
          </bean>



          ????
          <!--?事務適配器?-->
          ????
          <bean?id="txManager"
          ????????class
          ="org.springframework.orm.hibernate3.HibernateTransactionManager">
          ????????
          <property?name="sessionFactory"?ref="sessionFactory"?/>
          ????
          </bean>






          ????
          <!--?aop?與事務聯系?aopBean<->txAdvice??-->
          ????
          <aop:config>
          ????????
          <!--?邏輯攔截?-->
          ????????
          <aop:pointcut?id="demoAopBean"
          ????????????expression
          ="execution(*?demo*.*.*(..))"?/>
          ????????
          <aop:advisor?advice-ref="demoTxAdvice"
          ????????????pointcut-ref
          ="demoAopBean"?/>
          ????
          </aop:config>

          ????
          <!--?事務原子?具體方法進行什么事務?-->
          ????
          <tx:advice?id="demoTxAdvice"?transaction-manager="txManager">
          ????????
          <tx:attributes>
          ????????????
          <tx:method?name="get*"?propagation="SUPPORTS"
          ????????????????read-only
          ="true"?rollback-for="NoProductInStockException"?/>
          ????????????
          <tx:method?name="save*"?propagation="REQUIRED"
          ????????????????rollback-for
          ="NoProductInStockException"?/>
          ????????????
          <tx:method?name="update*"?propagation="REQUIRED"
          ????????????????rollback-for
          ="NoProductInStockException"?/>
          ????????????
          <tx:method?name="remove*"?propagation="REQUIRED"
          ????????????????rollback-for
          ="NoProductInStockException"?/>
          ????????????
          <tx:method?name="*"?propagation="SUPPORTS"
          ????????????????rollback-for
          ="NoProductInStockException"?/>
          ????????
          </tx:attributes>
          ????
          </tx:advice>



          ????
          <!--?daoCalss?:?extends?HibernateDaoSupport?implements?BeanDao?-->
          ????
          <bean?id="beanDao"
          ????????class
          ="demo.springHibernate.dao.imp.BeanDaoImp">
          ????????
          <property?name="sessionFactory">
          ????????????
          <ref?bean="sessionFactory"></ref>
          ????????
          </property>
          ????
          </bean>


          ????
          <bean?id="helloAction"?class="demo.struts2Spring.HelloWorld"
          ????????scope
          ="prototype">
          ????????
          <property?name="bds"?ref="beanDao"></property>
          ????
          </bean>
          ????
          </beans>


          hibernate 配置
          <?xml?version="1.0"?encoding="UTF-8"?>
          <!DOCTYPE?hibernate-configuration?PUBLIC
          ????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
          ????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
          >
          <hibernate-configuration>
          <session-factory?name="asdf">
          ????
          <property?name="hibernate.dialect">mysql</property>
          ????
          <property?name="myeclipse.connection.profile">
          ????????com.mysql.jdbc.Driver
          ????
          </property>
          ????
          <property?name="connection.url">
          ????????jdbc:mysql://localhost/aop
          ????
          </property>
          ????
          <property?name="show_sql">true</property>
          ????
          ????
          <property?name="connection.username">root</property>
          ????
          <property?name="connection.password"></property>
          ????
          <property?name="connection.driver_class">
          ????????com.mysql.jdbc.Driver
          ????
          </property>
          ????
          <property?name="dialect">
          ????????org.hibernate.dialect.MySQLDialect
          ????
          </property>
          ????
          ????
          <mapping?resource="bean/UnitBean.hbm.xml"?/>
          ????
          </session-factory>
          </hibernate-configuration>


          dao 類(接口)
          package?dao.imp;

          import?java.util.List;

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

          import?bean.UnitBean;

          import?dao.BeanDao;

          public?class?BeanDaoImp?extends?HibernateDaoSupport?implements?BeanDao{
          ????
          public?void?addBean(UnitBean?unitBean)?{
          ????????
          this.getHibernateTemplate().save(unitBean);
          ????}

          ????
          public?List<UnitBean>?getBeanByAll()?{
          ????????
          return?this.getHibernateTemplate().find("?from?"+UnitBean.class.getName());
          ????}

          ????
          public?void?removeBean(long?beanId)?{
          ????????
          this.getHibernateTemplate().delete(
          ????????????????getHibernateTemplate().get(UnitBean.
          class,?beanId)
          ????????????);
          ????}
          ????
          }

          Main 類
          package?unit;

          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.support.ClassPathXmlApplicationContext;

          import?dao.BeanDao;
          import?bean.UnitBean;

          public?class?Main?{
          ????
          public?static?void?main(String[]?args)?{
          ???????????ApplicationContext?ctx?
          =?new?ClassPathXmlApplicationContext("beans.xml");
          ???????????BeanDao?dao?
          =?(BeanDao)?ctx.getBean("beanDao");
          ???????????UnitBean?bean?
          =?new?UnitBean();
          ???????????bean.setName(
          "xx");
          ???????????bean.setPass(
          "11");
          ???????????dao.addBean(bean);
          ???????????
          ???????????
          for(UnitBean?unitBean?:?dao.getBeanByAll()?){
          ???????????????System.out.println(?unitBean.getId()?);
          ???????????}
          ???????????
          ???????????dao.removeBean(bean.getId());
          ???????????
          ????}
          }
          結果:
          Hibernate: insert into bean (name, pass) values (?, ?)
          Hibernate: select unitbean0_.id as id0_, unitbean0_.name as name0_, unitbean0_.pass as pass0_ from bean unitbean0_
          1
          Hibernate: select unitbean0_.id as id0_0_, unitbean0_.name as name0_0_, unitbean0_.pass as pass0_0_ from bean unitbean0_ where unitbean0_.id=?
          Hibernate: delete from bean where id=?






          主站蜘蛛池模板: 江源县| 遂昌县| 平江县| 广宗县| 松潘县| 乌什县| 宜兰县| 鹤岗市| 克东县| 阜城县| 孟津县| 安陆市| 曲靖市| 孟州市| 济阳县| 勃利县| 汉寿县| 柏乡县| 当雄县| 青田县| 通州市| 佛坪县| 舞阳县| 贡嘎县| 万载县| 华蓥市| 吕梁市| 仙游县| 美姑县| 临泽县| 黄浦区| 东山县| 广州市| 泾阳县| 灵武市| 台湾省| 梁河县| 达孜县| 东莞市| 宜都市| 江陵县|