springboot快速入门(五)——事务管理

一、入门

  概念就不再赘述了,由于一般我们是通过service控制事务,这里给出注解式的示例:

package com.example.demo;

import com.example.demo.bean.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; /**
* service
*
* @author zcc ON 2018/2/9
**/
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional
public void insertTwo() {
Girl girl1 = new Girl("Nana", 18);
Girl girl2 = new Girl("Lala", 19);
girlRepository.save(girl1);
girlRepository.save(girl2);
}
}

  controller层就可以调用了:

 @PostMapping(value = "/girls/two")
public void insertTwo() {
girlService.insertTwo();
}

  还有更多关于隔离级别等的控制:

@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)

  参考http://blog.didispace.com/springboottransactional/

上一篇:C# 将 Stream 写入文件


下一篇:装饰者模式在JDK和Mybatis中是怎么应用的? java io包