<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

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

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

    <?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 {

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲av激情无码专区在线播放 | 亚洲美女又黄又爽在线观看| 美女黄频免费网站| 国产免费观看a大片的网站| 色天使色婷婷在线影院亚洲| 亚洲国产精品碰碰| 国产免费无码AV片在线观看不卡| 亚洲黄色免费观看| 在线观看无码的免费网站| 日本特黄特色AAA大片免费| 亚洲AV无码乱码在线观看裸奔| 免费福利在线播放| 亚洲国产高清国产拍精品| 亚洲国产小视频精品久久久三级| 男人都懂www深夜免费网站| 亚洲AV无码专区在线亚| 免费成人黄色大片| 日韩人妻无码精品久久免费一| 亚洲午夜精品一区二区麻豆| 激情97综合亚洲色婷婷五| 4399影视免费观看高清直播| 美景之屋4在线未删减免费| 亚洲国产aⅴ成人精品无吗| 亚洲精品国产国语| 久久亚洲精品无码| 亚洲精品夜夜夜妓女网| 浮力影院第一页小视频国产在线观看免费| 一个人看的www在线免费视频| 18亚洲男同志videos网站| 亚洲电影国产一区| 亚洲精品免费视频| 中文字幕第一页亚洲| 成人免费在线观看网站| 日本亚洲欧洲免费天堂午夜看片女人员| 毛片基地看看成人免费| 亚洲伊人久久大香线蕉AV| 亚洲中文字幕AV每天更新| 亚洲国产成人精品无码区花野真一 | 久久精品国产亚洲夜色AV网站| 亚洲色成人中文字幕网站| 亚洲AV无码成人专区片在线观看|