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

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

          配置文件樣例如下(已經(jīng)修改了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> 

             
          <!-- 定義事務(wù)管理器(聲明式的事務(wù)) --> 
             
          <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"> 
                 
          <!-- 配置事務(wù)管理器 --> 
                 
          <property name="transactionManager"><ref bean="transactionManager" /></property> 
                 
          <property name="target"><ref bean="generatorDaoTarget" /></property> 
                 
          <property name="proxyInterfaces"><value>com.*.spring.dao.GeneratorDao</value></property>
                 
          <!-- 配置事務(wù)屬性 --> 
                 
          <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"> 
                    
          <!-- 配置事務(wù)管理器 --> 
                 
          <property name="transactionManager"><ref bean="transactionManager" /></property>    
                 
          <property name="target"><ref bean="plantDaoTarget" /></property> 
                 
          <property name="proxyInterfaces"><value>com.*.spring.dao.PlantDao</value></property>          
                 
          <!-- 配置事務(wù)屬性 --> 
                 
          <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"> 
                    
          <!-- 配置事務(wù)管理器 --> 
                    
          <property name="transactionManager"><ref bean="transactionManager" /></property>    
                 
          <property name="target"><ref bean="plantGeneratorServiceTarget" /></property> 
                 
          <property name="proxyInterfaces"><value>com.*.spring.service.PlantGeneratorService</value></property>
                 
          <!-- 配置事務(wù)屬性 --> 
                 
          <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發(fā)送給數(shù)據(jù)庫的sql顯示出來 -->
             
          <property name="hibernate.show_sql">true</property>
             
          <property name="hibernate.hbm2ddl.auto">none</property> 

             
          <!-- SQL方言,這邊設(shè)定的是MySQL -->
             
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
             
          <!--連接數(shù)據(jù)庫的Driver-->
             
          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
             
          <!--數(shù)據(jù)庫連接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 {

             
          /**
               * 獲取所有機組數(shù)據(jù)
               *
          @return
              
          */
             
          public List<Generator> listGenerators();
             
             
          /**
               * 保存機組數(shù)據(jù)
               *
          @param generator 機組數(shù)據(jù)
              
          */
             
          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 閱讀(1553) 評論(0)  編輯  收藏 所屬分類: Spring

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 上蔡县| 洛宁县| 罗平县| 上饶县| 沁水县| 尉氏县| 班戈县| 晋江市| 汝南县| 淮阳县| 金堂县| 波密县| 乌海市| 包头市| 奎屯市| 河北区| 西安市| 新邵县| 芜湖县| 沈阳市| 开原市| 台北市| 濮阳市| 卫辉市| 邢台市| 澜沧| 贵港市| 马鞍山市| 乐东| 东平县| 苏尼特左旗| 通道| 巴林右旗| 蒲江县| 册亨县| 柞水县| 玉门市| 龙南县| 石台县| 茶陵县| 黄平县|