Spring 事务控制 -- 基于XML的声明式事务控制:环境搭建

Posted CodeJiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 事务控制 -- 基于XML的声明式事务控制:环境搭建相关的知识,希望对你有一定的参考价值。

01:Spring 事务控制 – 编程式事务控制相关对象
02:Spring 事务控制 – 基于XML的声明式事务控制:环境搭建
03:Spring 事务控制 – 基于XML的声明式事务控制:详细配置
04:Spring 事务控制 – 基于注解的声明式事务控制

1. 基于XML的声明式事务控制


1.1 什么是声明式事务控制


1.2 转账业务环境搭建


1.2.1 执行下面的sql语句 搭建mysql数据库环境

CREATE DATABASE test;
use test;


DROP TABLE IF EXISTS `account`;
CREATE TABLE `account`  (
  `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '姓名',
  `money` double(10, 2) NOT NULL COMMENT '余额'
);


INSERT INTO `account` VALUES ('tom', 5000.00);
INSERT INTO `account` VALUES ('lucy', 5000.00);

1.2.2 导入相关依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>itheima_spring_tx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!--        spring依赖 包括aop包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--        aop织入包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>
        <!--        spring-jdbc 里面包含spring-tx-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--        spring-test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--        c3p0连接池-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.1</version>
        </dependency>
        <!--        mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <!--        Junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>
</project>

1.2.3 相关的代码

pojo层:

Account.java

package com.tian.domain;

public class Account {

    private String name;
    private double money;

    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

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

Dao层:

AccountDao.java

package com.tian.dao;

public interface AccountDao {
    // 取钱
    public void out(String outMan, double money);

    //存钱
    public void in(String inMan, double money);
}

AccountDaoImpl.java

package com.tian.dao.impl;

import com.tian.dao.AccountDao;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

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

    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?", money, outMan);
    }

    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?", money, inMan);
    }
}

Service层:

AccountService.java

package com.tian.service;

public interface AccountService {
    /**
     * 转账的方法
     *
     * @param outMan 转方
     * @param inMan  收款方
     * @param money 交易的金额
     */
    public void transfer(String outMan, String inMan, double money);
}

AccountServiceImpl.java

package com.tian.service.impl;

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

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

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

    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan, money);
        accountDao.in(inMan, money);
    }
}

Controller层(模拟):

AccountController.java

package com.tian.controller;

import com.tian.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

1.2.4 配置applicationContext.xml

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--    配置c3p0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="317525"/>
    </bean>

    <!--    配置jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <bean id="accountDao" class="com.tian.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    
    <!--目标对象 -->
    <bean id="accountService" class="com.tian.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
</beans>

1.2.5 测试环境搭建是否成功

现在的accout表:

运行controller方法:

运行成功:



以上是关于Spring 事务控制 -- 基于XML的声明式事务控制:环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

Spring 事务控制 -- 基于XML的声明式事务控制:详细配置

Spring 事务控制 -- 基于注解的声明式事务控制

Spring_事务

spring基于xml的声明式事务控制

spring基于xml的声明式事务控制

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