Spring_2023_11_24_1 Spring整合mybatis--Spring中的事务管理(xml形式)

发布时间 2023-11-24 11:46:22作者: Kbaor

Spring整合mybatis--Spring中的事务管理(xml形式)

引入依赖

<properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <spring.version>5.3.20</spring.version>
        <mysql.version>8.0.31</mysql.version>
        <mybatis.version>3.5.4</mybatis.version>
        <mybatis.spring.version>1.3.2</mybatis.spring.version>
        <lombok.version>1.18.30</lombok.version>
        <commons-dbcp2.version>2.11.0</commons-dbcp2.version>
        <aspectjweaver.version>1.9.7</aspectjweaver.version>
    </properties>

    <dependencies>
        <!--spring基础依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring连接数据库-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--添加mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!--mybatis与spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <!--事务的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>${commons-dbcp2.version}</version>
        </dependency>
        <!--切面编程框架-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectjweaver.version}</version>
        </dependency>

    </dependencies>

引入事务的命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

事务的相关配置

<!-- 事务管理数据源的配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--配置事务的属性
            1. name: 指定的哪些方式进行事务管理(这些方式指的事service中的方法)
                1.1 * 表示所有的方法都需要进行事务管理
                1.2 save* 表示save开头的方式需要进行事务管理
            2. propagation: 传播级别
                2.1 REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,就加入到这个事务中。这是最常见的选择。
            3. isolation: 隔离级别
        -->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--使用AOP编程进行事务的配置-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.bboy.service.impl.*.*(..))"/>
    </aop:config>

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">


    
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
        <property name="username" value="root"/>
        <property name="password" value="127003"/>
     </bean>
    <!-- 事务管理数据源的配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--配置事务的属性
            1. name: 指定的哪些方式进行事务管理(这些方式指的事service中的方法)
                1.1 * 表示所有的方法都需要进行事务管理
                1.2 save* 表示save开头的方式需要进行事务管理
            2. propagation: 传播级别
                2.1 REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,就加入到这个事务中。这是最常见的选择。
            3. isolation: 隔离级别
        -->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <!--使用AOP编程进行事务的配置-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.bboy.service.impl.*.*(..))"/>
    </aop:config>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.bboy.pojo"/>
        <property name="mapperLocations" value="mapper/*.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.bboy.mapper.AccountMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

    <bean id="accountService" class="com.bboy.service.impl.AccountServiceImpl">
    <property name="accountMapper" ref="accountMapper"/>
    </bean>
</beans>

AccountMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper  namespace="com.bboy.mapper.AccountMapper">
    <update id="updateId1">
        update t_account set money=money-${money} where id=${id}
    </update>
    <update id="updateId2">
        update t_account set money=money+${money} where id=${id}
    </update>

</mapper>

service

AccountService

package com.bboy.service;

public interface AccountService {
    public int pay(int id_1,int id_2,int money);

}

AccountServiceImpl

package com.bboy.service.impl;

import com.bboy.mapper.AccountMapper;
import com.bboy.service.AccountService;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/24 0024 10:48:11
 */
public class AccountServiceImpl implements AccountService {
    private AccountMapper accountMapper;

    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public int pay(int id_1, int id_2, int money) {

        //-:转出 id_1
        accountMapper.updateId1(id_1, money);
        //-停电了
        //  int res = 1/0 ;
        //-:转入 id_2
        accountMapper.updateId2(id_2, money);
        return 0;
    }
}

mapper-(AccountMapper)

package com.bboy.mapper;

import org.apache.ibatis.annotations.Param;

public interface AccountMapper {
    //- 转出
    public int updateId1(@Param("id") int id, @Param("money") int money);
    //- 转入
    public int updateId2(@Param("id") int id,@Param("money") int money);
}

pojo-(Account)

package com.bboy.pojo;

import lombok.Data;

@Data
/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/24 0024 10:45:51
 */
public class Account {
    private int id ;
    private String name ;
    private  int money ;
}

demo

package com.bboy.demo;

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

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/24 0024 10:43:19
 */
public class Demo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) context.getBean("accountService");
        accountService.pay(1,2,10000);
    }
}