zoukankan      html  css  js  c++  java
  • springboot mybatis 事务管理

    本文主要讲述springboot提供的声明式的事务管理机制。

    一、一些概念

    声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优点是:

    1)非侵入式,业务逻辑不受事务管理代码的污染。

    2)方法级别的事务回滚,合理划分方法的粒度可以做到符合各种业务场景的事务管理。

    本文使用目前最常用的mybatis框架来配置springboot的事务管理机制。下面进入配置方法介绍。

    二、springboot mybatis事务配置

    1、看一下pom依赖

    其中:

    1)<parent></parent>标签引入springboot父依赖

    2)使用了spring和mybatis集成包,整合spring和mybatis

    3)mysql数据库驱动包

    4)序列化支持fastjson

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.3.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.19</version>
            </dependency>
        </dependencies>

    2、application.properties

    # 主数据源 moonlight
    spring.datasource.moonlight.driverClassName = com.mysql.jdbc.Driver
    spring.datasource.moonlight.url=jdbc:mysql://10.93.84.53:3306/moonlight?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
    spring.datasource.moonlight.username = root
    spring.datasource.moonlight.password = 1234
    spring.datasource.moonlight.poolMaximumActiveConnections=400
    spring.datasource.moonlight.poolMaximumIdleConnections=200
    spring.datasource.moonlight.poolMaximumCheckoutTime=20000

    之后你可以根据添加的数据源,写好mapper也就是dao层代码。

    DAO层代码是使用XML配置方式,还是使用注解实现方式,对事务管理都是没有影响的。

    3、Service层

    在设计service层的时候,应该合理的抽象出方法包含的内容。

    然后将方法用@Trasactional注解注释,默认的话在抛出Exception.class异常的时候,就会触发方法中所有数据库操作回滚,当然这指的是增、删、改。

    当然,@Transational方法是可以带参数的,具体的参数解释如下:

     
    属性类型描述
    value String 可选的限定描述符,指定使用的事务管理器
    propagation enum: Propagation 可选的事务传播行为设置
    isolation enum: Isolation 可选的事务隔离级别设置
    readOnly boolean 读写或只读事务,默认读写
    timeout int (in seconds granularity) 事务超时时间设置
    rollbackFor Class对象数组,必须继承自Throwable 导致事务回滚的异常类数组
    rollbackForClassName 类名数组,必须继承自Throwable 导致事务回滚的异常类名字数组
    noRollbackFor Class对象数组,必须继承自Throwable 不会导致事务回滚的异常类数组
    noRollbackForClassName 类名数组,必须继承自Throwable 不会导致事务回滚的异常类名字数组

    给出一些示例代码

    @Service
    public class GeoFenceService {
    
        @Autowired
        private MoonlightMapper moonlightMapper;
    
        @Transactional
        public int addGeoFence(GeoFence geoFence) {
            String formatTime = TimeFunction.transTimeToFormatPerfect(System.currentTimeMillis());
            geoFence.setCreateTime(formatTime);
            geoFence.setUpdateTime(formatTime);
            return moonlightMapper.insertOne(geoFence);
        }
    
        @Transactional
        public int batchGeoFence(List<GeoFence> geoFenceList) {
            String formatTime = TimeFunction.transTimeToFormatPerfect(System.currentTimeMillis());
            for (GeoFence geoFence : geoFenceList) {
                geoFence.setCreateTime(formatTime);
                geoFence.setUpdateTime(formatTime);
            }
            return moonlightMapper.insertBatch(geoFenceList);
        }
    }

    4、开启事务

    最后你要在Application类中开启事务管理,开启事务管理很简单,只需要@EnableTransactionManagement注解就行

    @EnableTransactionManagement
    @SpringBootApplication
    public class WebApplication {
        public static void main(String[] args) {
            SpringApplication.run(WebApplication.class, args);
        }
    }

    三、测试一下

    可以做一个简单的测试,主动抛出异常,测试一下是否真的能保证事务性。

    在执行完插入之后,手动抛出一个空指针异常,可以发现数据真的回滚了。

    @Service
    public class GeoFenceService {
    
        @Autowired
        private MoonlightMapper moonlightMapper;
    
        @Transactional
        public int addGeoFence(GeoFence geoFence) {
            String formatTime = TimeFunction.transTimeToFormatPerfect(System.currentTimeMillis());
            geoFence.setCreateTime(formatTime);
            geoFence.setUpdateTime(formatTime);
            int count = moonlightMapper.insertOne(geoFence);
            String a = null;
            a.indexOf('c');
            return count;
        }
    }

     参考 http://www.cnblogs.com/kangoroo/p/7133543.html

  • 相关阅读:
    小结 编程练习 制作新按钮,“新窗口打开网站” ,新窗口打开时弹出确认框,是否打开,通过输入对话框,打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。
    关闭窗口(window.close)
    打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口。 语法: window.open([URL], [窗口名称], [参数字符串])
    提问(prompt 消息对话框)用于询问一些需要与用户交互的信息。弹出消息对话框(包含一个确定按钮、取消按钮与一个文本输入框)
    确认(confirm 消息对话框)语法:confirm(str); 消息对话框通常用于允许用户做选择的动作,如:“你对吗?”等。弹出对话框(包括一个确定按钮和一个取消按钮)
    警告(alert 消息对话框) 如果你不点击“确定”,就不能对网页做任何操作,这个小窗口就是使用alert实现的
    Socket的3次握手链接与4次断开握手
    大型电子商务网站架构之--分布式可扩展数据库架构
    程序员成长历程的四个阶段
    盘点 Github 所用到的开源项目
  • 原文地址:https://www.cnblogs.com/zhaoyan001/p/10769538.html
Copyright © 2011-2022 走看看