基于xml的事务加载

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

一、知识点总结:
springAop:基于注解的
@Component
@Aspect 修饰 切面类

@Before() 前置通知
@AfterReturning 后置通知
@After 最终通知
@Around 环绕通知
@AfterThrowing 异常通知
@PointCut 切入点

使用注解扫描器 完成对 注解的扫描
<context:component-scan base-package=“com.oracle”>

spring操作 持久层框架:

使用 spring 对数据库进行连接

数据源:
数据库连接池:存储 Connection
1.dbcp database connectivity pool(数据库连接池)
2.c3p0 必须有 依赖包 common-logging
3.alibaba 德鲁伊 druid 第三方jar

Connection conn = DriverMananger.getConnection(URL,USERNAME,PASSWORD);

2.spring的 持久层 框架
JdbcTemplate

前提 必须 导入 spring 的 jar包
     spring-jdbc , spring-orm , spring-tx

=============================================================
spring + 持久层框架
JdbcTemplate

1.数据库源 数据库连接池:
1.dbcp
2.c3p0 ComboPooledDataSource
3.druid DruidDataSource

spring ioc

JdbcTemplate

事务: spring 的事务管理

回滚

1.mysql 的事务:

事务 是 一件 或 多件 事件的机会 ,这些事 要么 全做 ,要没 全不做

2.事务的特性:
ACID:
原子性:事务是最小的执行 单位 , 不可再分
一致性:事务的状态 ,要保持一致
隔离性:事务和 事务之间 相互 不干扰
持久性:事务中的数据 一旦 提交 不可 改变

3.事务并发问题:
1.脏读
2.幻读
3.不可重复读

4.事务隔离级别:
1.读 未提交 数据 read-uncomitted
2.读 已提交数据 read-committed
3.可以重复读 repeatable 这个是 mysql 的默认隔离级别
4.串行化 serializable

5.spring的事务管理器 来管理事务

mybatis: 用的是 jdbc的事务 管理
getSession(true); 自动 提交
getSession(); 手动提交

<environments>
    <environment>
     <transactionManager type="JDBC">

6.spring框架 不直接管理事务 它 交由 它的事务管理平台管理:

以下:三个包相互依存
spring-tx 是 spring 的事务管理jar包 , spring-orm.jar(映射包) , spring-jdbc.jar

spring事务管理的三个 顶级接口:
 PlatformTransactionManager: 平台事务管理器
TransactionDefinition: 事务的定义
TransactionStatus:事务的状态

7.开发步骤:
在spring的配置文件中 :
1.实例化 DataSourceTransactionManager(原始的jdbc事务管理器)
并注入 数据源
2.配置 aop 思想
编写 切入点表达式
配置增强
3.使用tx:advice id=“myAdvice” transaction-Manager=“transactionManager”
tx:attributes
<tx:method name=“transfer”>
在这里插入图片描述
二、工程架构
(1)工程架构图
在这里插入图片描述
在这里插入图片描述
(2)案例代码

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.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {
    //在applicationContext.xml里面IOC和DI
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

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

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

    }

    @Override
    public void inner(String inner, double money) {
        String sql="update account set balance = balance + ? where name = ?";
        int update = 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,double money);
}
4. com.oracle.serviceImpl包:
package com.oracle.serviceImpl;

import com.oracle.dao.AccountDao;
import com.oracle.service.AccountService;


public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    public AccountDao getAccountDao() {
        return accountDao;
    }

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

    /**
     *
     * @param outer 汇款人
     * @param inner 收款人
     * @param money 金额
     */
    @Override
    public void transfer(String outer, String inner, double money) {
        //汇款
        accountDao.outer(outer,money);
        int a=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("root");
        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("root");
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);

    }
    @Test
    public void Testtansfer(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        AccountService accountService = (AccountService) c.getBean("accountServiceId");
        accountService.transfer("张三","王五",100);
        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">

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

    <!--德鲁伊 数据库 连接池: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>

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

    <!--IOC DI-->
    <bean id="accountDaoId" class="com.oracle.daoImpl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--IOC DI-->
    <bean id="accountServiceId" class="com.oracle.serviceImpl.AccountServiceImpl">
        <property name="accountDao" ref="accountDaoId"></property>
    </bean>

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

    <!--配置通知
               propagation:  事务的传播特性
               isolation:    事务的隔离级别
       -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>

    <!--aop配置-->
    <aop:config>
        <aop:pointcut id="myPointCut" expression="execution(* com.oracle.serviceImpl.AccountServiceImpl.* (..))"/>
        <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"></aop:advisor>
    </aop:config>

</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)结果图
在这里插入图片描述
说明钱没丢

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值