一、配置啓動器
package com.mingde;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication //啓動Spring應用
@EnableTransactionManagement // 啓註解事務管理,等同於xml配置方式的 <tx:annotation-driven />
public class SpringBootDemo {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo.class, args);
}
}
二、在servlet類中配置事務管理器
package com.mingde.services.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.mingde.dao.StudentDao;
import com.mingde.po.Student;
import com.mingde.services.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
//@Transactional 只這樣寫默認是隻讀,下面的寫法是用於增刪改
@Transactional(propagation=Propagation.REQUIRED) //必須事務,錯誤時事務回滾
public List<Student> findAll() throws Exception {
return (List<Student>) studentDao.findAll();
}
}