使用很简单,只需要在service层对应方法上注解@Transactional即可。
package com.wxl.go.service.impl;
import com.wxl.go.dao.PersonDao;
import com.wxl.go.model.Person;
import com.wxl.go.service.PersonService;
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 PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Override
public List<Person> selectAll() {
return personDao.selectAll();
}
@Override
@Transactional
public Integer updatePerson(Person person) {
person.setAge(100);
personDao.updatePerson(person);
int k = 1 / 0;
person.setId(1);
personDao.updatePerson(person);
return 1;
}
}