基于注解的事务加载

24 篇文章 0 订阅
15 篇文章 0 订阅

一、知识点总结
spring基于注解的 事务配置:

@Transactional注解 可以放在 类的上面
放在类的上面 代表当前类下的所有方法 都添加了事务

@Transactional注解 可以放在 方法上面
放在方法上面 代表当前这个方法 添加了事务 ,其它方法没有事务;
二、工程架构
(1)架构图
在这里插入图片描述
在这里插入图片描述
(2)案例Code

1.com.oracle.dao包:
package com.oracle.dao;

public interface AccountDao {

    public void outer(String outer, double money);//汇款人

    public void inner(String inner, double money);//收款人


}
2.com.oracle.daoImpl包:
package com.oracle.daoImpl;

import com.oracle.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
//AccountDaoImpl IOC
@Repository("accountDaoId")
public class AccountDaoImpl implements AccountDao {

    /**
     * jdbcTemplate
     *
     * @param outer
     * @param money
     */
    //依赖注入
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void outer(String outer, double money) {
        String sql = "update account set balance = balance - ? where name = ?";
        int a = jdbcTemplate.update(sql, money, outer);
    }

    @Override
    public void inner(String inner, double money) {
        String sql = "update account set balance = balance + ? where name = ?";
        int a = jdbcTemplate.update(sql, money, inner);
    }
}
3.com.oracle.service包:
package com.oracle.service;

public interface AccountService {
    /**
     *
     * @param outer 汇款人
     * @param inner 收款人
     * @param money 转账金额
     */
    public void transfer(String outer, String inner, int money);

}
4.com.oracle.serviceImpl包:
package com.oracle.serviceImpl;

import com.oracle.dao.AccountDao;
import com.oracle.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
//AccountServiceImpl IOC
@Service
/*@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)*/
public class AccountServiceImpl implements AccountService {

    //依赖注入
    @Autowired
    @Qualifier("accountDaoId")
    private AccountDao accountDao;

    //为方法加事务,propagation传播方式,isolation隔离级别
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
    public void transfer(String outer, String inner, int money) {

        //调用 转账
        accountDao.outer(outer,money);

        //转账的时候出现了异常
        int i = 1/0;

        //收款
        accountDao.inner(inner,money);
    }

}

5. com.oracle.test包:
package com.oracle.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.oracle.service.AccountService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.SQLException;

public class Demo {
    @Test
    public void Testc3p0() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        dataSource.setUser("root");
        dataSource.setPassword("123");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
    @Test
    public void Testdruid() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("123");
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);

    }
    @Test
    public void Testtansfer(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        AccountService accountService = c.getBean(AccountService.class);
        accountService.transfer("张三","王五",200);
        System.out.println("执行完毕!");
    }
}

6.com.oracle.vo包:
package com.oracle.vo;

public class Account {

    private Integer id;
    private String name;
    private Double balance;

    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 Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", balance=" + balance +
                '}';
    }
}

7.config包:
1)applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- spring的 注解扫描器-->
    <context:component-scan base-package="com.oracle"/>

    <!--加载db.properties本地文件-->
    <context:property-placeholder local-override="true" location="classpath:config/db.properties"/>

    <!-- 德鲁伊数据源 DruidDataSource IOC 并为其属性赋值 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <!-- JdbcTemplate:IOC 和 DI-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--原生JDBC事务管理器:IOC 和 DI-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 启动事务管理器 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
=========================
2)db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username=root
password=123

(3)结果图
在这里插入图片描述
说明钱没丢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值