使用注解方式配置spring中的jdbc事务

来源:互联网 发布:初中物理辅导软件 编辑:程序博客网 时间:2024/06/05 05:57
jdbc事务管理,使用注解的方式步骤如下
  * 引入命名空间
        * xmlns:context="http://www.springframework.org/schema/context"
             http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
        
        * xmlns:aop="http://www.springframework.org/schema/aop"
             http://www.springframework.org/schema/aop 
             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        --事务的命名空间
        * xmlns:tx="http://www.springframework.org/schema/tx"  
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd


  * 在beans.xml文件中增加如下配置
      <!--1 增加spring的自动扫描功能  -->
      <context:component-scan base-package="cn.itcast" />
  
      <!-- 2 配置dbcp连接池 -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://localhost:3306/test"/>
         <property name="username" value="root"/>
         <property name="password" value="root"/>
         <!--配置初始化连接数  -->
         <property name="initialSize" value="5"/>
         <!-- 配置最大连接数 -->
         <property name="maxActive" value="20"/>
         <!-- 配置最大空闲数  防止洪峰退去时,连接池中连接数过多-->
         <property name="maxIdle" value="10"/>
         <!-- 配置最小空闲数  防止洪峰到来时,连接池中连接池中连接的数量过少,引起的性能开销-->
         <property name="minIdle" value="5"/>
         <!--设置最大等待时间,如果超过这个时间,连接池将抛出异常-->
         <property name="maxWait" value="5000"/>
         <property name="defaultAutoCommit" value="true"/>
      </bean>
 
   <!--3  配置jdbcTemplate -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     <property name="dataSource" ref="dataSource"></property>
   </bean>
   
   <!--4  配置jdbc的事务管理器   Aop的切面-->
   <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 注入数据源 -->
      <property name="dataSource" ref="dataSource" />
   </bean>
   
   <!-- 5 配置事务级别的注解使用的事务管理器 -->
   <tx:annotation-driven transaction-manager="txManager"/>
    * 处理dao层    
         * 处理存款的dao层
               * 创建InAccountDaoImpl对象 使用@Repository注解
@Repository("inAccountDao")
public class InAccountDaoImpl implements InAccountDao

  * 注入jdbcTemplate到该dao层
      @Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
    * 处理账户的dao层
        * 创建AccountDaoImpl对象 使用@Repository注解
             @Repository("accountDao")
                       public class AccountDaoImpl implements AccountDao
                  
            * 注入jdbcTemplate到该dao层
       @Resource(name="jdbcTemplate")
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

     * 处理业务层
        * 创建业务层的对象
               @Service("inAccountService")
               public class InAccountServiceImpl implements InAccountService
         
        * 注入dao层的对象
            @Resource(name="inAccountDao")
private InAccountDao inAccountDao;

@Resource(name="accountDao")
private AccountDao accountDao;   
   * 处理事务
         * 定义类级别的事务(设置事务时只读的)
          @Transactional(readOnly=true)
                  @Service("inAccountService")
                  public class InAccountServiceImpl implements InAccountService
                  
              * 定义方法级别的事务      
                   @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false)
              public void saveInAccount(InAccount inAccount)
     
                注:方法级别的事务会覆盖类级别的事务
                
      * 测试
原创粉丝点击