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/

 

posted @ 2018-02-09 11:18  ---江北  阅读(606)  评论(0编辑  收藏  举报
TOP