spring中注解的事物管理---------Transactional

IDEA创建的maven的java项目


spring事务管理有两种方式:一种是xml配置文件方式管理、另一种利用Transactional注解方式管理


第一步:在pom.xml文件中添加依赖

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">


        <groupId>com.itqf</groupId>
        <artifactId>spring-tx-xml-12</artifactId>
        <version>1.0-SNAPSHOT</version>
    <properties><springversion>5.0.8.RELEASE</springversion></properties>
    <modelVersion>4.0.0</modelVersion>

<!--
    <groupId>com.qf</groupId>
    <artifactId>springTransaction</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties><springversion>5.0.8.RELEASE</springversion></properties>-->
    <dependencies>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--添加ssm整合所需依赖-->
        <!-- 加入ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- MySQL依赖 start -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>



        <!-- 引入Spring(包含SpringMVC) 依赖 start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springversion}</version>
        </dependency>


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


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


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

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

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


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>aop-4.RELEASE</artifactId>
            <version>1.6.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>




        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>

        <!--  引用c3p0 依赖 start-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.2.1</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 编码和编译和JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

步骤二:在resources文件夹下创建c3p0-config.xml配置数据库连接池

<c3p0-config>
	<!-- 默认配置,如果没有指定则使用这个配置 -->
	<default-config>
		<!-- 基本配置 -->
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///day06</property>
		<property name="user">root</property>
		<property name="password">root</property>
	
		<!--扩展配置-->
		<!-- 连接超过30秒报错-->
		<property name="checkoutTimeout">30000</property>
		<!--30秒检查空闲连接 -->
		<property name="idleConnectionTestPeriod">30</property>
		<property name="initialPoolSize">10</property>
		 <!-- 30秒不适用丢弃-->
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
		<property name="maxStatements">200</property>
	</default-config> 
	
	
	<!-- 命名的配置 -->
	<named-config name="zhaowf">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///day06</property>
		<property name="user">root</property>
		<property name="password">root</property>
		
		
		<!-- 如果池中数据连接不够时一次增长多少个 -->
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">20</property>
		<property name="minPoolSize">10</property>
		<property name="maxPoolSize">40</property>
		<property name="maxStatements">20</property>
		<property name="maxStatementsPerConnection">5</property>
	</named-config>
</c3p0-config> 


步骤三:在resources文件夹下创建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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">



       <context:component-scan base-package="com.itqf" />

       <!-- jdbctemplate ioc  datasource-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>

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


       <!--步骤1:根据持久层选择事务管理器的实现类 -->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="dataSource" />
       </bean>

    <!--开启注解形式支持-->
     <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>



-------------以上环境搭建完毕-----------------


步骤四:创建数据库表对应的实体类

本案例将该类命名为:Account

package com.itqf.entity;

/**
 * 作者:赵伟风
 * 项目名: springroot
 * 时间:2019/10/24  11:14
 * 描述:
 */
public class Account {


    private String name;
    private int money;

    public String getName() {
        return name;
    }

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

    public int getMoney() {
        return money;
    }

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

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


五: 定义dao层

该案例dao层接口命名为:AccountDao

package com.itqf.dao;


public interface AccountDao {

    void addMoney(String to, int money);

    void subMoney(String from, int money);
}

AccountDao对应的实现类命名为:AccountDaoImpl

package com.itqf.dao.impl;

import com.itqf.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 作者:赵伟风
 * 项目名: springroot
 * 时间:2019/10/24  15:51
 * 描述:
 */
@Repository
public class AccountDaoImpl  implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addMoney(String to, int money) {
        jdbcTemplate.update("update account set money = money + ? where name = ?", money,to);
    }

    @Override
    public void subMoney(String from, int money) {
        jdbcTemplate.update("update account set money = money - ? where name = ?", money,from);
    }
}


六:定义service层

该案例将service层接口命名为:AccountService

package com.itqf.service;

/**
 * 作者:赵伟风
 * 项目名:springroot
 * 时间:2019/10/24  15:49
 * 描述:
 */
public interface AccountService {


    void  transfer(String from, String to, int money);

}

AccountService对应的实现类:

package com.itqf.service.impl;

import com.itqf.dao.AccountDao;
import com.itqf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作者:赵伟风
 * 项目名: springroot
 * 时间:2019/10/24  15:50
 * 描述:
 */
@Service
public class AccountServiceImpl  implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Transactional
    @Override
    public void transfer(String from, String to, int money) {

        System.out.println("业务层执行了");

        accountDao.addMoney(to, money);

        accountDao.subMoney(from, money);

    }
}


七:controller层

该案例将controller命名为:AccountController

package com.itqf.controller;

import com.itqf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 作者:赵伟风
 * 项目名: springroot
 * 时间:2019/10/24  15:47
 * 描述:
 */
@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     *
     * @param from 转出人
     * @param to  转入人
     * @param money  转账金额
     */
    public void  transfer(String from,String to, int money){

        System.out.println("contoller执行了");

        accountService.transfer(from, to, money);

    }

}


步骤七:定义启动类

该案例将该启动类命名为:TestTransfer

package com.itqf.test;

import com.itqf.controller.AccountController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作者:赵伟风
 * 项目名: springroot
 * 时间:2019/10/24  16:02
 * 描述:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.SUPPORTS,readOnly = true,timeout = 5)//似乎也可以不设置属性,自带默认属性
public class TestTransfer {

    @Autowired
    private AccountController accountController;

    @Test
    public void test1(){

        accountController.transfer("zhangsan", "lisi", 5000000);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值