MyBatic与Spring的整合,事务的管理

本文摘自:Java EE企业级应用开发教程,有部分修改

本文假定你的MyBatis与Spring环境已经搭建完毕,数据结构及测试数据也已经导入数据库,实体类(Customer.java)也已经创建,接口类与接口映射文件都已经建立,参考:MyBatic与Spring的整合,Mapper接口方式的开发

本文的重点在于测试数据库事务。

四、事务的管理

1、创建服务接口

package com.itheima.service;

import com.itheima.po.Customer;

public interface CustomerService {
    public void addCustomer(Customer customer);
}

2、创建服务接口实现类

package com.itheima.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.mapper.CustomerMapper;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    // 注解注入CustomerMapper
    @Autowired
    private CustomerMapper customerMapper;

    // 添加客户
    public void addCustomer(Customer customer) {
        this.customerMapper.addCustomer(customer);
        int i = 1 / 0; // 模拟添加操作后系统突然出现的异常问题
    }
}

3、修改applicationContext-mybatis.xml配置文件

    <!-- 开启扫描 --> 
    <context:component-scan base-package="com.itheima.service" />

4、创建测试类

package com.itheima.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;

/**
 * 测试事务
 */
public class TransactionTest {
    public static void main(String[] args) {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        CustomerService customerService = act.getBean(CustomerService.class);
        Customer customer = new Customer();
        customer.setUsername("赵明星");
        customer.setJobs("演员");
        customer.setPhone("13809090099");
        customerService.addCustomer(customer);
    }
}

5、运行程序

DEBUG [main] - ==>  Preparing: insert into t_customer(username,jobs,phone) values(?,?,?) 
DEBUG [main] - ==> Parameters: 赵明星(String), 演员(String), 13809090099(String)
DEBUG [main] - <==    Updates: 1
Exception in thread "main" java.lang.ArithmeticException: / by zero
    ......

检查数据库,会发现数据没有任何变化,即数据没有正常插入。

6、修改CustomerServiceImpl.java,注释事务部分

package com.itheima.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.mapper.CustomerMapper;
import com.itheima.po.Customer;
import com.itheima.service.CustomerService;

@Service
//@Transactional
public class CustomerServiceImpl implements CustomerService {
    // 注解注入CustomerMapper
    @Autowired
    private CustomerMapper customerMapper;

    // 添加客户
    public void addCustomer(Customer customer) {
        this.customerMapper.addCustomer(customer);
        int i = 1 / 0; // 模拟添加操作后系统突然出现的异常问题
    }
}

7、再次运行程序

DEBUG [main] - ==>  Preparing: insert into t_customer(username,jobs,phone) values(?,?,?) 
DEBUG [main] - ==> Parameters: 赵明星(String), 演员(String), 13809090099(String)
DEBUG [main] - <==    Updates: 1
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.itheima.service.impl.CustomerServiceImpl.addCustomer(CustomerServiceImpl.java:20)
    at com.itheima.test.TransactionTest.main(TransactionTest.java:19)

检查数据库,会发现系统扔出异常,但数据仍然正常插入后台。这就说明事务确实起到应该起的作用。

原文地址:https://www.cnblogs.com/nayitian/p/15267296.html