Spring基于xml和注解事务控制

1.明确:
Spring提供了一组处理事务的接口,这组接口在Spring-tx中。
2.xml配置:

<?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: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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


<!--配置service-->
<bean id="accountService" class="com.service.IAccountServiceImpl">
   <property name="accountDao" ref="accountDao"></property>
</bean>

<!--配置数据源-->
<bean id="accountDao"  class="com.dao.IAccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--注入数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

   <!--配置事务管理器-->
   <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
   </bean>

   <!--配置事务通知-->
   <tx:advice id="txAdvice" transaction-manager="transactionManger">

      <!--配置事物的通知-->
      <tx:attributes>
         <tx:method name="*" propagation="REQUIRED" read-only="false" ></tx:method>
         <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
      </tx:attributes>
   </tx:advice>



   <!--配置aop-->
   <aop:config>
      <aop:pointcut id="pt1" expression="execution(com *..*.*(..))"></aop:pointcut>

      <!--建立关系-->
      <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>

   </aop:config>
</beans>

3.注解:

<?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:aop="http://www.springframework.org/schema/aop"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:http="http://www.springframework.org/schema/c"
            xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

   <context:component-scan base-package="com"></context:component-scan>

<!--配置Jdbc-->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource"></property>
   </bean>

   <!-- 配置数据源-->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
      <property name="url" value="jdbc:mysql:///spring"></property>
      <property name="username" value="root"></property>
      <property name="password" value="root"></property>
   </bean>

   <bean id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
   </bean>

   <!-- 开启 spring 对注解事务的支持 -->
   <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


</beans>

4。参数

<!--
   1.配置事务管理器
   2.配置事务通知
      2.1导入事务约束:tx(事务),aop
      2.2 tx:advice:配置标签事务
           属性:id
                 transaction-manger:给事务通知提供一个事务管理器引用
   3.配置Aop中的通用切入点表达式
   4.建立切入点表达式和事务的对应关系
   5.配置事务的属性
       在tx:Advice标签内配置
            属性:isolation:指定事物的隔离级别,默认值为default。表示使用数据库的隔离级别。
                  propagation:用于指定事物的传播行为,默认值是REQUIRED,表示一定会有事务,增删改的选择,
                              查询方法可选择SUPPORTS
                  read-only:指定事务是否只读,只有查询方法才能设置为true,默认值为false,表示读写。
                  time-out:事物的超时时间,默认值为-1,表示永不超时,指定了数值的话,以秒为单位。
                  rollback-for:指定异常,当产生该异常时,事务回滚,产生其他异常事务不回滚。没有默认值,表示任何事务都回滚。
                  no-rollback-for:相反。但是:没有默认值,表示任何事务都回滚。

   -->


注意:JdbcSupportDao不要轻易用,他里面有JdbcTemplate的set方法,但是他只能在xml里使用。
持久层代码:

public class AccountRowMapper implements RowMapper<Account> {
    public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
        Account account = new Account();
        account.setId(rs.getInt("id"));
        account.setName(rs.getString("name"));
        account.setMoney(rs.getFloat("money"));
        return account;
    }
}

package com.dao;

import com.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository("accountDao")

public class IAccountDaoImpl implements IAccountDao {
    @Autowired
private JdbcTemplate jdbcTemplate;


    public Account findAccountById(Integer id) {
       List<Account> list =jdbcTemplate.query("select * from account where id = ? ",
           new AccountRowMapper(),id);
        return list.isEmpty()?null:list.get(0);


    }

    public Account findAccountByName(String name) {
        List<Account> list =  jdbcTemplate.query("select * from account where name = ? ",
                new AccountRowMapper(),name);
        if(list.isEmpty()){    return null;   }   if(list.size()>1){
            throw new RuntimeException("结果集不唯一,不是只有一个账户对象");   }
        return list.get(0);

    }

    public void UpdateAccount(Account account) {
        jdbcTemplate.update("update account set money = ? where id = ? ",
                account.getMoney(),account.getId());
    }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值