关于后端:SSM系列教材-六-中进行注解式和XML配置式事务管理

7次阅读

共计 9424 个字符,预计需要花费 24 分钟才能阅读完成。


步骤 1: 先运行,看到成果,再学习
步骤 2: 模拟和排错
步骤 3: 本知识点成果
步骤 4:MYSQL 表的类型必须是 INNODB 才反对事务
步骤 5: 导入 JAR 包
步骤 6:CategoryService
步骤 7:CategoryServiceImpl
步骤 8: 批改测试类,察看成果
步骤 9: 进行事务配置
步骤 10: 应用注解形式
步骤 11: 运行测试类,察看成果
步骤 12: 应用 XML 配置形式
步骤 13: 运行测试类,察看成果
步骤 14: 为什么不启动 Web 利用进行测试?

步骤 1 : 先运行,看到成果,再学习

老规矩,先下载下载区 (点击进入) 的可运行我的项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的成果。

成果: 运行 MybatisTest 成心制作谬误,会察看到被事务管理的办法:addTwo,因为某一条 sql 语句失败了,另一个 sql 语句也没有胜利失效(要么都胜利,要么都失败)

步骤 2 : 模拟和排错

在确保可运行我的项目可能正确无误地运行之后,再严格照着教程的步骤,对代码模拟一遍。
模拟过程不免代码有出入,导致无奈失去冀望的运行后果,此时此刻通过比拟 正确答案 (可运行我的项目) 和本人的代码,来定位问题所在。
采纳这种形式,学习有成果,排错有效率,能够较为显著地晋升学习速度,跨过学习路上的各个槛。

举荐应用 diffmerge 软件,进行文件夹比拟。把你本人做的我的项目文件夹,和我的可运行我的项目文件夹进行比拟。
这个软件很牛逼的,能够晓得文件夹里哪两个文件不对,并且很显著地标记进去
这里提供了绿色装置和应用教程:diffmerge 下载和应用教程

步骤 3 : 本知识点成果

本知识点将演示,应用事务之前和应用事务之后的成果区别,以及别离应用注解和 XML 形式在 SSM 中配置事务。

本知识点基于 SSM 整合的根底上进行

步骤 4 : MYSQL 表的类型必须是 INNODB 才反对事务

在 Mysql 中,只有当表的类型是 INNODB 的时候,才反对事务,所以须要把表的类型设置为 INNODB, 否则无奈察看到事务.

批改表的类型为 INNODB 的 SQL:

alter table category_ ENGINE  = innodb;

查看表的类型的 SQL

show table status from how2java;

不过有个前提,就是以后的 MYSQL 服务器自身要反对 INNODB, 如果不反对,请看 开启 MYSQL INNODB 的方法

步骤 5 : 导入 JAR 包

做事务管理须要额定的 jar 才行,所以先在下载区 (点击进入) 下载 aspectjweaver.jar,
把 jar 包导入到我的项目中,导包方法:右键 project->properties->java build path->libaries->add external jars

步骤 6 : CategoryService

为接口 CategoryService 减少 addTwo 和 deleteAll 办法

package com.how2java.service;

import java.util.List;
import com.how2java.pojo.Category;
public interface CategoryService {List<Category> list();
    void addTwo();
    void deleteAll();}

步骤 7 : CategoryServiceImpl

在 CategoryServiceImpl 中实现 deleteAll 和 addTwo 办法。
别离做如下事件:
1. 删除所有 Category
2. 减少 2 个 Category 对象,第一个能够失常减少,第二个因为名字太长,退出会失败。

package com.how2java.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
@Service
public class CategoryServiceImpl  implements CategoryService{
    @Autowired
    CategoryMapper categoryMapper;
    public List<Category> list(){return categoryMapper.list();
    }
    public void deleteAll() {List<Category> cs = list();
        for (Category c : cs) {categoryMapper.delete(c.getId());
        }
    }
    @Override
    public void addTwo() {Category c1 = new Category();
        c1.setName("短的名字");
        categoryMapper.add(c1);
        Category c2 = new Category();
        c2.setName("名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下,");
        categoryMapper.add(c2);
    };

}

步骤 8 : 批改测试类,察看成果

批改测试类 MybatisTest:注入 CategoryService 对象,并调用 deleteAll 和 addTwo 办法,运行后在数据库中发现只有只有一条数据插入胜利了,阐明 addTwo 办法不是事务管理的。如果是事务管理的,那么就应该都胜利,或者都失败。

package com.how2java.test;

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 com.how2java.service.CategoryService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
    @Autowired
    private CategoryService categoryService;
    @Test
    public void testAddTwo() {categoryService.deleteAll();
        categoryService.addTwo();}

}

步骤 9 : 进行事务配置

批改 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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.how2java.service" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>admin</value>
        </property>
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.how2java.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.how2java.mapper"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

步骤 10 : 应用注解形式

为 addTwo 办法加上事务注解

package com.how2java.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
@Service
public class CategoryServiceImpl  implements CategoryService{
    @Autowired
    CategoryMapper categoryMapper;
    public List<Category> list(){return categoryMapper.list();
    }
    public void deleteAll() {List<Category> cs = list();
        for (Category c : cs) {categoryMapper.delete(c.getId());
        }
    }
    @Override
    @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception")
    public void addTwo() {Category c1 = new Category();
        c1.setName("短的名字");
        categoryMapper.add(c1);
        Category c2 = new Category();
        c2.setName("名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下, 名字长对应字段放不下,");
        categoryMapper.add(c2);
    };

}

步骤 11 : 运行测试类,察看成果

运行测试类 MybatisTest 发现数据库里是空的。因为第二个数据插入不进去,所以第一个数据的插入也回滚了。

步骤 12 : 应用 XML 配置形式

批改 applicationContext,以反对 AOP 形式

<?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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.how2java.service" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>admin</value>
        </property>
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.how2java.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.how2java.mapper"/>
    </bean>
    <!--     <tx:annotation-driven transaction-manager="transactionManager"/> -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="list*" propagation="REQUIRED" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="serviceMethod" expression="execution(* com.how2java.service.*.*(..))"/>
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txadvice"/>
    </aop:config>
</beans>

步骤 13 : 运行测试类,察看成果

运行 MybatisTest 能够看到一样的事务成果,即都没有插入胜利

步骤 14 : 为什么不启动 Web 利用进行测试?

为什么不启动 Web 利用进行测试?而是要用一个 MybatisTest 这种形式?

因为依据后面的教材,都是基于一个实体类做治理的 ssm 利用,不不便重现事务出问题的景象。

通过本教材的形式,也是批改 Service 以提供事务管理的反对,其成果也是可能使用在 web 利用的。

更多内容,点击理解:https://how2j.cn/k/ssm/ssm-transaction/1150.html

正文完
 0