随笔-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 阅读(1541) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: