springboot提供的声明式的事务管理机制

一、概念
声明式的事务管理是基于AOP的,在springboot中可以通过@Transactional注解的方式获得支持,这种方式的优点是:
1)非侵入式,业务逻辑不受事务管理代码的污染。
2)方法级别的事务回滚,合理划分方法的粒度可以做到符合各种业务场景的事务管理。
二、springboot mybatis事务配置
1、pom依赖
其中:
1)标签引入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
这个配置主要是实现配置mybatis数据源

# 主数据源 moonlight
spring.datasource.moonlight.driverClassName = com.mysql.jdbc.Driver
spring.datasource.moonlight.url=jdbc:mysql://127.0.0.1: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

DAO层代码是使用XML配置方式,还是使用注解实现方式,这些对事务管理都是没有影响的。
3、Service层
在设计service层的时候,应该合理的抽象出方法包含的内容。
然后将方法用@Trasactional注解注释,默认的话在抛出Exception.class异常的时候,就会触发方法中所有数据库操作回滚,当然这指的是增、删、改。
当然,@Transational方法是可以带参数的,具体的参数解释如下:
在这里插入图片描述示例代码:

@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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值