<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(1558) 評論(0)  編輯  收藏 所屬分類: Spring

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 天堂亚洲国产中文在线| 精品国产污污免费网站入口| 国产伦精品一区二区三区免费下载 | 中文字幕无码亚洲欧洲日韩| 又黄又爽的视频免费看| 美女视频黄a视频全免费网站色窝| 亚洲国产精品综合久久久| 午夜国产大片免费观看| 99免费视频观看| 大陆一级毛片免费视频观看| 一出一进一爽一粗一大视频免费的| 亚洲国产综合91精品麻豆| 国内自产少妇自拍区免费| 永久免费不卡在线观看黄网站| 中文字幕 亚洲 有码 在线| 国产av无码专区亚洲av果冻传媒| 最近免费中文字幕大全免费| 久久亚洲精品成人av无码网站| 免费看无码自慰一区二区| 日本免费久久久久久久网站| 亚洲暴爽av人人爽日日碰| 亚洲AV无码国产丝袜在线观看| 日韩免费视频一区| 67pao强力打造国产免费| a级毛片免费高清视频| 亚洲色大成网站www| 久久精品国产亚洲AV麻豆网站| 亚洲国产天堂久久久久久| 免费下载成人电影| 苍井空亚洲精品AA片在线播放| 色噜噜综合亚洲av中文无码| 亚洲精品国自产拍在线观看| 成人性生活免费视频| 99爱免费观看视频在线| 一级毛片试看60分钟免费播放| 亚洲色大成网站www永久网站| 亚洲高清不卡视频| 亚洲s色大片在线观看| 亚洲天堂中文字幕在线| 国产免费拔擦拔擦8X高清在线人| WWW国产亚洲精品久久麻豆|