欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot使用事物注解方式代码实例

时间:2021-03-28 09:15:27|栏目:JAVA代码|点击:

这篇文章主要介绍了springboot使用事物注解方式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考

1.在启动类Application中添加注解@EnableTransactionManagement

import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //开启书屋管理注解模式 最新的版本可以省略
@MapperScan("com.xz.springboot.mapper") //扫描该包下所有的接口并为该接口生成实现类
public class Springboot01Application {
  public static void main(String[] args) {
    SpringApplication.run(Springboot01Application.class, args);
  }
}

2.在业务层添加@Transactional

import com.xz.springboot.bean.User;
import com.xz.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> queryAll(){
      System.out.println("热部署");
      return userMapper.findAll();
    }
  @Transactional
  public void deleteById(Integer id) {
      userMapper.deleteById(id);
     // int c=10/0;
  }
}

上一篇:Java中==运算符与equals方法的区别及intern方法详解

栏    目:JAVA代码

下一篇:java设计优化之代理模式

本文标题:springboot使用事物注解方式代码实例

本文地址:http://www.codeinn.net/misctech/89462.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有