除了查询,都要加事务;
可防止有的数据库操作成功,有的不成功;
//controller
@Autowired
private GirlService girlService;
@PostMapping(value = "/girls/two")
public void girlTwo(){
girlService.insertTwo();
}
//service
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional //事务处理的关键!!!
public void insertTwo(){
Girl girlA = new Girl();
girlA.setAge(12);
girlA.setCupSize("F");
girlRepository.save(girlA);
Girl girlB = new Girl();
girlB.setAge(18);
girlB.setCupSize("yy");
girlRepository.save(girlB);
}
}
//Repository