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

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

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

    隨筆-75  評(píng)論-193  文章-5  trackbacks-0

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

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

    <?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ù)庫(kù)的sql顯示出來(lái) -->
       
    <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ù)庫(kù)的Driver-->
       
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
       
    <!--數(shù)據(jù)庫(kù)連接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 閱讀(1558) 評(píng)論(0)  編輯  收藏 所屬分類: Spring

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 特级毛片aaaa级毛片免费| 亚洲一区二区三区深夜天堂| 亚洲精品无码久久久久秋霞| 国拍在线精品视频免费观看 | 亚洲AV无码一区二三区| 亚洲成a人片在线观看天堂无码| 成人免费一级毛片在线播放视频| 亚洲高清视频在线播放| 亚洲一级免费毛片| 亚洲人成电影在线观看青青| 日本zzzzwww大片免费| 亚洲二区在线视频| 成人毛片18岁女人毛片免费看| 国产精品亚洲四区在线观看| 好吊妞在线新免费视频| 国产精品亚洲色婷婷99久久精品| 国产成人无码区免费A∨视频网站| 爱情岛论坛亚洲品质自拍视频网站 | 亚洲AV无码成人专区| 久久精品无码一区二区三区免费 | 亚洲精品在线免费观看| 无码精品A∨在线观看免费| 国产AV旡码专区亚洲AV苍井空| 日本无吗免费一二区| 成年大片免费高清在线看黄| 国产成人A亚洲精V品无码| 久9久9精品免费观看| 亚洲伊人久久大香线蕉啊| 国产三级免费观看| 中文字幕手机在线免费看电影 | 中文字幕中韩乱码亚洲大片 | 91免费在线播放| 亚洲欧美日韩综合久久久 | 亚洲a一级免费视频| 国产一卡二卡3卡四卡免费| 国产成人亚洲综合无| 亚洲av日韩av高潮潮喷无码| 午夜毛片不卡高清免费| WWW免费视频在线观看播放| 亚洲图片校园春色| 亚洲国产精品人人做人人爽|