springboot使用事物注解方式代码实例
更新时间:2019年11月11日 09:36:58 作者:&天涯海角&
这篇文章主要介绍了springboot使用事物注解方式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了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实现一键获取Mysql所有表字段设计和建表语句的工具类
这篇文章主要为大家详细介绍了如何利用Java编写一个工具类,可以实现一键获取Mysql所有表字段设计和建表语句,感兴趣的小伙伴可以了解一下2023-05-05JSON各种转换问题(json转List,json转对象等)
这篇文章主要介绍了JSON各种转换问题(json转List,json转对象等),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-03-03
最新评论