在业务层使用@Transactional //开启事务
@Service
@Transactional
public class StuServiceImpl implements StuService {
@Autowired
private StudentMapper studentMapper;
@Override
public int update(Student student) {
int i = studentMapper.updateByPrimaryKey(student);
return i;
}
@Override
public Student queryIdStu(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
}
在启动类添加,@EnableTransactionManagement,开启事务支持
package com.czh.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@MapperScan("com.czh.springboot.mapper")
@EnableTransactionManagement
public class Springboot07AcidApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot07AcidApplication.class, args);
}
}