zoukankan      html  css  js  c++  java
  • Spring Boot项目中开启事务支持及使用

    本博文主要讲Spring Boot中事务的使用

    第一步:

    在Spring Boot项目的入口类(启动类)中添加

    @EnableTransactionManagement //注解为作用开启事务支持

    使用@Mapper注解要定义成一个接口interface

    作用:

    1.使用@Mapper将NewsDAO接口交给Spring进行管理

    2.不用写Mapper映射文件(XML)

    3.为这个NewsDAO接口生成一个实现类,让别的类进行引用

    如果有多个类的话,可以使用@MapperScan进行注解,一次性注解多个包 

    @SpringBootApplication  
    @MapperScan({"com.tao.demo","com.tao.user"})  
    public class App {  
        public static void main(String[] args) {  
           SpringApplication.run(App.class, args);  
        }  
    }

    使用注意事项:

    1.接口不可以定义重名方法,即不支持方法重载

    2.方法参数有多个的时候,需要使用@Param,单参数方法不需要

    第二步:

    在service的层你要使用的事物的方法上添加    @Transactional    注解

    
    

    @Transactional 
    @Override
    public Integer upd(Map map) {
    int updateRow = loginDao.upd(map);
    //a = 0 自定义一个异常,发生异常则会回滚 。事务输出1不回滚 ,输出0则回滚
    int a = 10 /updateRow ;
    return updateRow;
    }

  • 相关阅读:
    hlgoj 1766 Cubing
    Reverse Linked List
    String to Integer
    Bitwise AND of Numbers Range
    Best Time to Buy and Sell Stock III
    First Missing Positive
    Permutation Sequence
    Next Permutation
    Gray Code
    Number of Islands
  • 原文地址:https://www.cnblogs.com/wdyjt/p/14253176.html
Copyright © 2011-2022 走看看