SpringBoot事务管理器

一、配置启动器

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();
	}
	
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章