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

2021年09月15日 阅读数:1
这篇文章主要向大家介绍MyBatic与Spring的整合,事务的管理,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

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

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

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

4、事务的管理

一、建立服务接口

package com.itheima.service;

import com.itheima.po.Customer;

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

二、建立服务接口实现类

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; // 模拟添加操做后系统忽然出现的异常问题
    }
}

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

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

四、建立测试类

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);
    }
}

五、运行程序

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

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

六、修改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; // 模拟添加操做后系统忽然出现的异常问题
    }
}

七、再次运行程序

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)

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