Spring 学习笔记四 基于Xml配置的事务配置

1.配置步骤

  • 1 配置三层架构之间的Bean标签及互相之间的依赖关系
  • 2.配置事务管理器
  • 3.配置事务通知并与事务管理器联系起来
  • 4.配置切入点表达式,并于事务通知连接起来
  • 事务通知的目的:通过动态代理的方式对方法进行增强,对异常事务可以进行回滚等操作
  • 切入点表达式的目的:是为了确定哪个类的哪个方法需要进行事务通知

2.配置参数

  • 配置事务的属性
    isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
    propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
    read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
    timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
    rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
    no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。

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"
       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">
    <!--通过xml的方式来配置Spring框架中的事物-->
    <!--1.配置控制层对象-->
    <bean id="AccountControl" class="com.itheima.Control.AccountControl">
        <property name="accountService" ref="AccountService"></property>
    </bean>
    <!--2.配置业务层 -->
    <bean id="AccountService" class="com.itheima.Service.AccountService">
        <property name="accountDao" ref="AccountDao"></property>
    </bean>
    <!--3.配置dao层-->
    <bean id="AccountDao" class="com.itheima.Dao.AccountDao">
        <property name="jdbcTemplate" ref="Jdbctemple"></property>
    </bean>
    <bean id="Jdbctemple" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="DataSource"></property>
    </bean>
    <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--4.配置事务管理器-->

    <bean id="transationManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="DataSource"></property>
    </bean>
    <!--5.配置事务的通知-->
    <!--在通知中配置事务的属性-->
    <tx:advice id="Txadvice" transaction-manager="transationManage">
        <tx:attributes>
            <!--配置方法名称 及对应的事务控制属性
            1、PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,该设置是最常用的设置。
            2、PROPAGATION_SUPPORTS:支持当前事务,如果当前存在事务,就加入该事务,如果当前不存在事务,就以非事务执行。‘
            在Service的方法进行增删改 用PROPAGATION_REQUIRED
            查询的时候,用PROPAGATION_SUPPORTS
            -->
            <tx:method name="transform" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--6.配置切入点表达式-->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.itheima.Service.*.*(..))"></aop:pointcut>
        <!--7.在切面中配置切入点表达式和通知的关系-->
        <aop:advisor advice-ref="Txadvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>

</beans>

4.其他代码

public class AccountControl {

    private AccountService accountService = null;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    public void transform(String resource, String target, float money){
        accountService.transform(resource,target,money);
    }

}

public class AccountService {
    /**
     * 完善事物的操作通过 TxManager类
     */
    /**
     * 通过Aop编程来简化增强的代码
     * Aop编程:通过动态代理的方式对方法进行增强,并可以简化重复代码块的书写
     * 我们可以对AccountService进行Aop编程,对其中的方法开启事物支持
     */

    private AccountDao accountDao = null;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void transform(String resource, String target, float money) {
        //开启事物
        try {
            accountDao.transform(resource,target,money);
            int i = 1/0;
        }catch (Exception e){
           throw new RuntimeException(e);
        }
    }
}
public class AccountDao implements IAccountDao {
    //需要注入queryRunner对象 并带有DataSource
    private JdbcTemplate jdbcTemplate = null;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

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

    }
    public void transform(String resource,String target,float money) throws SQLException {
        /**
         * 转账操作
         * 1.找到resource
         * 2.找到target
         * 3.resource-money
         * 4.target+money
         */

        List<Account> res = jdbcTemplate.query("select * from account where name = ? ",new BeanPropertyRowMapper<Account>(Account.class),resource);
        List<Account> tar  = jdbcTemplate.query("select * from account where name = ? ", new BeanPropertyRowMapper<Account>(Account.class),target);
        System.out.println("res = " + res);
        System.out.println("tar = " + tar);
        if(res==null||tar==null||res.size()>1||tar.size()>1){
            throw new RuntimeException("用户信息错误");
        }
        Account re = res.get(0);
        Account ta = tar.get(0);
        re.setMoney(re.getMoney()-money);
        ta.setMoney(ta.getMoney()+money);
        updateAccount(re);
        updateAccount(ta);
    }
}

public class Account {
    private Integer id;
    private String name;
    private float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值