【Spring Boot学习之四】Spring Boot事务管理

环境
  eclipse 4.7
  jdk 1.8
  Spring Boot 1.5.2

一、springboot整合事务
事务分类:编程事务、声明事务(XML、注解),推荐使用注解方式,springboot默认集成事物,只主要在方法上加上@Transactional即可
1、controller

package com.wjy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wjy.test1.service.UserServiceTest1;

@RestController
public class UserController {

    @Autowired
    public UserServiceTest1 userServiceTest1;

    @RequestMapping("/insertTest1ByService")
    public String insertTest1ByService(String name,Integer age) {
        userServiceTest1.insertuser1(name, age);
        return "success";
    }

}

2、service

/**
 *
 */
package com.wjy.test1.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wjy.test1.dao.UserMapperTest1;

/**
 * @Desc
 * @author wangjy15
 */
@Service
public class UserServiceTest1 {

    @Autowired
    private UserMapperTest1 userMapperTest1;

    /**
     * @Description: 如果没有事务控制 那么报错之后  插入到库里的数据不会回滚  加上 注解@Transactional  就可以回滚
     */
    @Transactional
    public String insertuser1(String name,Integer age) {
        userMapperTest1.insert(name, age);
        int i =1/0;
        return "success";
    }

}

3、mapper

/**
 *
 */
package com.wjy.test1.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.wjy.entity.User;

/**
 * @Desc
 * @author wangjy15
 */
public interface UserMapperTest1 {

    @Select("SELECT * FROM users WHERE NAME = #{name}")
    User findByName(@Param("name") String name);

    @Insert("insert into users (name,age) values(#{name},#{age})")
    int insert(@Param("name") String name,@Param("age") Integer age);
}

4、APP

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class APP {

    public static void main(String[] args) {
        SpringApplication.run(APP.class, args);
    }

}

5、测试验证

http://localhost:8080/insertTest1ByService?name=wangsan0010&age=1000

二、SpringBoot分布式事务管理
传统项目:jta+automatic

原文地址:https://www.cnblogs.com/cac2020/p/11230967.html

时间: 2024-08-29 21:30:10

【Spring Boot学习之四】Spring Boot事务管理的相关文章

spring框架学习笔记7:事务管理及案例

Spring提供了一套管理项目中的事务的机制 以前写过一篇简单的介绍事务的随笔:http://www.cnblogs.com/xuyiqing/p/8430214.html 还有一篇Hibernate的事务管理:http://www.cnblogs.com/xuyiqing/p/8449167.html 可以做个对比 Spring管理事务特有的属性: 事务传播行为:事务传播行为(propagation behavior)指的就是当一个事务方法被另一个事务方法调用时,这个事务方法应该如何进行. 例

Spring+Mybatis+MySql+Maven 简单的事务管理案例

利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` (   `id` 

Spring整合JMS(四)——事务管理

原文链接:http://haohaoxuexi.iteye.com/blog/1983532 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.JmsTransactionManager在执行本地资源事务管理时将从指定的ConnectionFactory绑定一个ConnectionFactory/Session这样的配对到线程中.JmsTemplate会自动检测这样的事务资

mybatis、Spring整合(eclipse)以及事务管理

1.项目目录 2.jar包 dbcp:连接池 pool:连接池 logging:日志 log4j:日志 mybatis-spring:用于SqlSession等相关操作 spring相关包 mybatis 3.web.xml配置 可以删除本配置文件,本次测试用的是JUnit,不涉及网络访问,所有该配置文件并不需要 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http

spring事物配置,声明式事务管理和基于@Transactional注解的使用

参考来源:http://blog.csdn.net/bao19901210/article/details/41724355 事物管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的一致性. spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager.对于编程式事务管理,spring推荐使用TransactionTemplate. 声明式事务管理建立在A

Spring使用注解方式就行事务管理

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation

8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)

声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-driven 1 <tx:annotation-driven transaction-manager="txManager"/> 2 <bean id="txManager" class="org.springframework.orm.hiberna

9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)

使用xml的方式进行声明式的事务管理 推荐使用xml的方式,因为可以同时为多个方法进行声明 1 <!-- 开启Spring中的事务管理(声明式的事务管理) xml--> 2 3 <!-- 不管是通过 xml 还是注解的方式 来进行声明式的事务管理,都需要 加载TransactionManager或 DataSouce 4 因为 事务是与是与数据库相关的,需要数据库一些配置信息 5 --> 6 <bean id="txManager" class="

spring的学习____12 声明式事务(AoP的横向织入)

1.事务的介绍: 事务涉及到数据的一致性问题. 事务:要么都成功,要么都不成功! 事务的四大特性: ACID :原子性:一致性:隔离性:持久性. 编程中遇到的实际问题: 在如下的实现类(UserDaoImpl)中,执行了:先添加一个user,再删除一个user的操作,最后打印出所有的用户列表. 当我们人为的在删除代码写错时(即就不能成功执行删除操作),发现程序还是执行了添加和打印用户列表的其余两步,这就不符合事物的一致性. public class UserDaoImpl implements