隨筆-75  評論-193  文章-5  trackbacks-0

          由于要寫一個Spring的培訓教材,要做Spring的事務樣例,于是開始寫樣例,寫好了一測,控制臺有SQL輸出,數據庫卻查詢不到數據,查亞查亞,花了一個多小時,原來是獲取的Service不是經過代理的Service,自然事務不起作用,數據庫里就沒有數據了,鄙視一下自己。

          配置文件樣例如下(已經修改了dao和service的命名,減少了寫錯的可能性,以后命名問題一定要注意):

          <?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:context
          ="http://www.springframework.org/schema/context"
              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.5.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-2.5.xsd
                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
          >

             
          <context:annotation-config />
             
          <context:component-scan base-package="com.*" />

             
          <bean id="sessionFactory" 
                      class
          ="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
                 
          <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
                 
          <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
             
          </bean> 

             
          <!-- 定義事務管理器(聲明式的事務) --> 
             
          <bean id="transactionManager"
                  class
          ="org.springframework.orm.hibernate3.HibernateTransactionManager">
                 
          <property name="sessionFactory" ref="sessionFactory" />
             
          </bean>
             
             
          <!-- 配置DAO -->
             
          <bean id="generatorDaoTarget" class="com.*.spring.dao.GeneratorDaoImpl">
                 
          <property name="sessionFactory" ref="sessionFactory" />
             
          </bean>
             
             
          <bean id="generatorDao" 
                  class
          ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
                 
          <!-- 配置事務管理器 --> 
                 
          <property name="transactionManager"><ref bean="transactionManager" /></property> 
                 
          <property name="target"><ref bean="generatorDaoTarget" /></property> 
                 
          <property name="proxyInterfaces"><value>com.*.spring.dao.GeneratorDao</value></property>
                 
          <!-- 配置事務屬性 --> 
                 
          <property name="transactionAttributes"> 
                     
          <props> 
                         
          <prop key="*">PROPAGATION_REQUIRED</prop>
                     
          </props> 
                 
          </property> 
             
          </bean> 

             
          <bean id="plantDaoTarget" class="com.*.spring.dao.PlantDaoImpl">
                 
          <property name="sessionFactory" ref="sessionFactory" />
             
          </bean>
             
             
          <bean id="plantDao" 
                  class
          ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
                    
          <!-- 配置事務管理器 --> 
                 
          <property name="transactionManager"><ref bean="transactionManager" /></property>    
                 
          <property name="target"><ref bean="plantDaoTarget" /></property> 
                 
          <property name="proxyInterfaces"><value>com.*.spring.dao.PlantDao</value></property>          
                 
          <!-- 配置事務屬性 --> 
                 
          <property name="transactionAttributes"> 
                     
          <props> 
                         
          <prop key="*">PROPAGATION_REQUIRED</prop>
                     
          </props> 
                 
          </property> 
             
          </bean>
             
             
          <!-- 配置Service -->
             
          <bean id="plantGeneratorServiceTarget" 
                  class
          ="com.*.spring.service.PlantGeneratorServiceImpl"> 
                 
          <property name="plantDao"> 
                     
          <ref bean="plantDao" /> 
                 
          </property> 
                 
          <property name="generatorDao"> 
                     
          <ref bean="generatorDao" /> 
                 
          </property> 
             
          </bean>       
             
             
          <bean id="plantGeneratorService" 
                  class
          ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
                    
          <!-- 配置事務管理器 --> 
                    
          <property name="transactionManager"><ref bean="transactionManager" /></property>    
                 
          <property name="target"><ref bean="plantGeneratorServiceTarget" /></property> 
                 
          <property name="proxyInterfaces"><value>com.*.spring.service.PlantGeneratorService</value></property>
                 
          <!-- 配置事務屬性 --> 
                 
          <property name="transactionAttributes"> 
                     
          <props> 
                         
          <prop key="*">PROPAGATION_REQUIRED</prop> 
                     
          </props> 
                 
          </property> 
             
          </bean> 
          </beans>
          <?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>

             
          <!-- 各屬性的配置-->
             
          <!-- 為true表示將Hibernate發送給數據庫的sql顯示出來 -->
             
          <property name="hibernate.show_sql">true</property>
             
          <property name="hibernate.hbm2ddl.auto">none</property> 

             
          <!-- SQL方言,這邊設定的是MySQL -->
             
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
             
          <!--連接數據庫的Driver-->
             
          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
             
          <!--數據庫連接url-->
             
          <property name="connection.url">jdbc:mysql://localhost:3306/test</property>

             
          <!--用戶名-->
             
          <property name="connection.username">root</property>
             
          <!--密碼-->
             
          <property name="connection.password">123456</property>

             
          <!-- 映射文件  -->
             
          <mapping class="com.*.spring.domain.Generator" />
             
          <mapping class="com.*.spring.domain.Plant" />
          </session-factory>
          </hibernate-configuration>
          public interface GeneratorDao {

             
          /**
               * 獲取所有機組數據
               *
          @return
              
          */
             
          public List<Generator> listGenerators();
             
             
          /**
               * 保存機組數據
               *
          @param generator 機組數據
              
          */
             
          public void save(Generator generator);   
          }
          public class GeneratorDaoImpl extends HibernateDaoSupport implements GeneratorDao {
                
              @SuppressWarnings(
          "unchecked")
             
          public List<Generator> listGenerators() {
                 
          return this.getSession().createQuery("from Generator").list();
              }

             
          public void save(Generator generator) {
                 
          this.getSession().save(generator);   
              }
          }
          posted on 2009-03-16 22:24 The Matrix 閱讀(1563) 評論(0)  編輯  收藏 所屬分類: Spring

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 桦南县| 蓬莱市| 汉寿县| 宝兴县| 东乡族自治县| 丰台区| 定南县| 琼中| 镇平县| 镇江市| 黔东| 翁源县| 收藏| 南郑县| 烟台市| 措美县| 布尔津县| 福清市| 临湘市| 巴南区| 和静县| 邯郸县| 五大连池市| 通化市| 开原市| 宜章县| 金乡县| 卫辉市| 颍上县| 无极县| 延庆县| 金溪县| 邵阳县| 如东县| 微博| 根河市| 自贡市| 道真| 怀柔区| 盘锦市| 许昌县|