24.spring事务控制(XML方式)

 

 

 

 代码实现:

导入坐标:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.4</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

新建类Account

public class Account {

  private String name;
  private String money;


  public String getName() {
    return name;
  }

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


  public String getMoney() {
    return money;
  }

  public void setMoney(String money) {
    this.money = money;
  }

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

controller:

public class AccountController
{
    public static void main(String[] args)
    {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) app.getBean("accountService");
        accountService.transfer("suqi","hah",2000);
    }
}

AccountService:

package Service.impl;

import Dao.AccountDao;
import Service.AccountService;

public class AccountServiceImpl implements AccountService
{
    private AccountDao accountDao;

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

    @Override
    public void transfer(String outMan, String inMan, double money)
    {
        accountDao.out(outMan,money);/*转出*/
        accountDao.in(inMan,money);/*转入*/
    }
}
AccountDao:
package Dao.impl;

import Dao.AccountDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao
{

    private JdbcTemplate jdbcTemplate;

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

    @Override
    public void out(String outMan, double money)
    {
        int update = jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
        double money2 = (double) jdbcTemplate.queryForObject("select money from account where name=?",Double.class,outMan);
        System.out.println("姓名:"+outMan+",余额:"+money2);
    }

    @Override
    public void in(String inMan, double money)
    {
        int update = jdbcTemplate.update("update account set money=money+? where name=?", money, inMan);
        double money2 = (double) jdbcTemplate.queryForObject("select money from account where name=?",Double.class,inMan);
        System.out.println("姓名:"+inMan+",余额:"+money2);
    }
}

applicationContext.xml

 配置数据模板对象:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=18081736467xr
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

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

    <bean id="accountDao" class="Dao.impl.AccountDaoImpl"></bean>
    <bean id="accountService" class="Service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <bean id="accountDao" class="Dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="accountService" class="Service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

原始数据:

 运作结果:

如果我们在service修改逻辑,使得转出人的钱,并没有到转入人的账户

public class AccountServiceImpl implements AccountService
{
    private AccountDao accountDao;

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

    @Override
    public void transfer(String outMan, String inMan, double money)
    {
        accountDao.out(outMan,money);/*转出*/
        int i =1/0;
        accountDao.in(inMan,money);/*转入*/
    }
}

所以 我们要在service进行事务提交
 

需要修改applicationContext.xml

    <bean id="accountDao" class="Dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--目标对象 内部方法就是切点-->
    <bean id="accountService" class="Service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

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

    <!--通知 事务的增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务的aop织入-->
    <aop:config>
       <aop:advisor advice-ref="txAdvice" pointcut="execution(* Service.impl.AccountServiceImpl.*(..))"></aop:advisor>
    </aop:config>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值