spring基于XML的声明式事务控制-配置步骤

1.maven 导入相关依赖jar
在这里插入图片描述在这里插入图片描述

    <!--打包方式-->
    <packaging>jar</packaging>
<dependencies>
    <!--spring 包-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--spring 内置跟事物相关的jar-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--spring内置对数据库的操作jar-->
    <!--具体参照此链接  https://blog.csdn.net/chszs/article/details/43971981  -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--mysql 数据库包-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!--aspectjweaver是spring的AOP切入点表达式需要用的包-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
    </dependency>
    <!--单元测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!--和单元测试相依赖的jar-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
</dependencies>

2.创建账户实体类
在这里插入图片描述3.写数据访问层和业务逻辑层的接口的相应方法
在这里插入图片描述4.写数据访层的实现类
在这里插入图片描述

//账户持久层实现类
public class Account1DaoImpl extends JdbcDaoSupport implements IAccount1Dao {
//    //JdbcTemplate 数据库操作类对象,作为属性。通过xml配置注入
//    private JdbcTemplate jdbcTemplate;
//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//        this.jdbcTemplate = jdbcTemplate;
//    }
    //id单查
    public Account1 findById(int id) {
         List<Account1> accs = super.getJdbcTemplate().query("select * from account1  where id = ?",
                new BeanPropertyRowMapper<Account1>(Account1.class),id);
        return accs.isEmpty()?null:accs.get(0);
    }
    //名字查询操作
    public Account1 findByName(String name) {
        List<Account1> accs = super.getJdbcTemplate().query("select * from account1  where name = ?",
                new BeanPropertyRowMapper<Account1>(Account1.class),name);
        if(accs.isEmpty()){
            return null;
        }
        if(accs.size()>1){
            throw new RuntimeException("结果不唯一");
        }
        return accs.get(0);
    }
    //修改操作
    public void modify(Account1 acc) {
        super.getJdbcTemplate().update("update account1 set name = ?,money = ? where id = ?;",
                acc.getName(),acc.getMoney(),acc.getId());
    }
}

5.业务逻辑层的实现类方法
在这里插入图片描述

public class Account1ServiceImpl implements IAccount1Service {
    //声明一个持久层的接口类属性,方便内部调用持久层方法。赋值采用xml IOC 配置set方法注入
    private IAccount1Dao account1Dao;
    public void setAccount1Dao(IAccount1Dao account1Dao) {
        this.account1Dao = account1Dao;
    }

    //id查询账户方法,业务层传给持久层。
    public Account1 findById(int id) {
        return account1Dao.findById(id);
    }
    
    /***转账接口方法
     * @param sourceName    转出账户名称
     * @param targetName    转入账户名称
     * @param money         转账金钱
     */
    public void transfer(String sourceName, String targetName, double money) {
        System.out.println("transfter.转账事务开始......");
        //1.根据名称查询转出账户
        Account1 source = account1Dao.findByName(sourceName);
        //2.根据名称查询转入账户
        Account1 target = account1Dao.findByName(targetName);
        //3.转出账户减钱
        source.setMoney(source.getMoney()-money);
        //4.转入账户加钱
        target.setMoney(target.getMoney()+money);
        //5.更新转出账户
        account1Dao.modify(source);
        //更新转入账户
        account1Dao.modify(target);
    }
}

6.配置 bean.xml 文件
a.将持久层实现类,业务层实现类,添加进IOC容器,并为持久层实现类,数据库操作类,注入数据源
在这里插入图片描述b.spring基于xml的声明式事物控制配置步骤
在这里插入图片描述

<!--spring基于xml的声明式事物控制配置步骤
        1.配置事物管理器
        2.配置事物通知,注意,此时需要导入事物的约束,也就是顶上固定的头文件内容
        tx名称空间和约束&&需要aop的约束,使用tx:advice 标签配置事务通知
            属性:
            id:给事务通知起一个唯一标识
            transaction-manager:给事务通知提供一个事务管理器引用。
            也就是引用第一步,配置的事务管理器。
        3.配置AOP中的通用切入点表达式
        4.建立切入点表达式和事物通知的对应关系
        5.配置事物的属性
             isolation=""   用于指定事务的隔离级别。
                默认值是DEFAULT,表示使用数据库的默认隔离级别
             propagation="" 用于指定事务的传播行为。默认值时REQUIRED,表示一定会有事务。
                增删改的选择。查询方法可以选择SUPPORTS
             read-only=""   用于指定事务是否只读。只有查询方法才能设置为true.
                默认值时false,表示读写。
             timeout=""     用于指定事务的超时时间,默认值是-1,表示永不超时。
                如果指定了数值,以秒为单位
             ollback-for=""  用于指定一个异常,当产生该异常时,事务回滚。
                产生其他异常时,事务不回滚。没有默认值,表示产生异常就回滚。
              no-rollback-for=""    用于指定一个异常,当产生该异常时,事务不回滚。
                产生其他异常时事务回滚。没有默认值。表示产生异常就回滚。-->

c.spring基于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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置业务层IOC,业务层里面包含,Dao层实现类属性-->
    <bean id="account1Service"  class="wuwei.service.impl.Account1ServiceImpl">
        <property name="account1Dao" ref="account1Dao"></property>
    </bean>

    <!--配置Dao实现类中的 jdbcTemplate 做(增删改查) 操作对象属性 添加进IOC容器-->
    <bean id = "account1Dao" class="wuwei.dao.impl.Account1DaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置spring内置数据源-->
    <bean id ="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 配置spring内置连接数据库的4个基本信息 ,注意spring内置的 name 名是固定的字符。-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
    </bean>

    <!--spring基于xml的声明式事物控制配置步骤
        1.配置事物管理器
        2.配置事物通知,注意,此时需要导入事物的约束,也就是顶上固定的头文件内容
        tx名称空间和约束&&需要aop的约束,使用tx:advice 标签配置事务通知
            属性:
            id:给事务通知起一个唯一标识
            transaction-manager:给事务通知提供一个事务管理器引用。
            也就是引用第一步,配置的事务管理器。
        3.配置AOP中的通用切入点表达式
        4.建立切入点表达式和事物通知的对应关系
        5.配置事物的属性
             isolation=""   用于指定事务的隔离级别。
                默认值是DEFAULT,表示使用数据库的默认隔离级别
             propagation="" 用于指定事务的传播行为。默认值时REQUIRED,表示一定会有事务。
                增删改的选择。查询方法可以选择SUPPORTS
             read-only=""   用于指定事务是否只读。只有查询方法才能设置为true.
                默认值时false,表示读写。
             timeout=""     用于指定事务的超时时间,默认值是-1,表示永不超时。
                如果指定了数值,以秒为单位
             ollback-for=""  用于指定一个异常,当产生该异常时,事务回滚。
                产生其他异常时,事务不回滚。没有默认值,表示产生异常就回滚。
              no-rollback-for=""    用于指定一个异常,当产生该异常时,事务不回滚。
                产生其他异常时事务回滚。没有默认值。表示产生异常就回滚。-->

    <!--1.配置事物管理器,引用的是spring内部提供的事务管理器-->
    <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--为事务管理器提供数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--2.配置事物通知,注意,此时需要导入事物的约束,也就是顶上固定的头文件内容
        tx名称空间和约束 并且 需要aop的约束,事务通知 引用,spring 提供的事务管理器,-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <!--5.配置事物的属性-->
            <tx:attributes>
                <!--把所有方法,都设置为有事务,指定事务为读写,注意单个标签-->
                <tx:method name="*"  propagation="REQUIRED" read-only="false"/>
                <!--把查询方法,设置为SUPPORTS查询事务,指定事务为只读,注意成对标签-->
                <tx:method name="find*"  propagation="SUPPORTS" read-only="true"></tx:method>
            </tx:attributes>
        </tx:advice>
       
    <!--3.配置AOP中的通用切入点表达式,-->
    <!--需要先配置AOP外框-->
    <aop:config>
        <!--配置AOP中的通用切入点表达式,-->
        <aop:pointcut id="pt1" expression="execution(* wuwei.service.impl.*.*(..))"></aop:pointcut>
        <!--4.建立切入点表达式和事物通知的对应关系-->
        <!--advice-ref="txAdvice" 释义:引用的是第2步,配置的通知。
        也就是对pt1路径下,的所有方法进行 配置通知-->
        <aop:advisor advice-ref="txAdvice"  pointcut-ref="pt1"></aop:advisor>
    </aop:config>

</beans>

7.写测试转账方法
在这里插入图片描述

//单元测试
@RunWith(SpringJUnit4ClassRunner.class)
//注解配置xml 进单元测试类
@ContextConfiguration(locations = "classpath:bean.xml")
public class Test {
    //让业务层接口属性,按从IOC 按类型,自动注入赋值
    @Autowired
    private IAccount1Service as;

    @org.junit.Test
    public  void testTransfer(){//调用业务层的转账操作方法
        as.transfer("孜然","吴伟",150);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值