- 事务ACID原则
- 原子性
- 一致性
- 隔离性
- 多个业务可能操作同一个资源,防止数据损坏
- 持久性
- 事务一旦提交,无论系统发生什么问题,结果都不会在被影响,被持久化的写到存储器中。
使用声明式事务需要用到Mybatis—Spring中的DataSourceTransactionManager对象
配置声明式事务 使用Mybatis-Spring创建一个DataSourceTransactionManager对象-->
使用AOP完成事务的织入,增删改使用事务,查询只用于只读状态
配置事务的切入点,切入在Mapper文件下所有类和方法
实现类,继承SqlSessionDaoSupport 创建SqlSession
public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{
public List selectStudent() {
//获取SqlSession,不在用SqlSessionTemplate来创建SQLSession了
//创建一个student用户
StudentMapper studentMapper2 = getSqlSession().getMapper(StudentMapper.class);
student student = new student(1007, "图图", 1, "幼儿园班");
int i = studentMapper2.InsertStudent(student);
System.out.println("插入结果为:"+i);
int r = studentMapper2.DelStudent(1007); //此时在这会报错,而且切入点实在mapper路径下,所以要写在查询方法中
System.out.println("删除结果为:"+r);
return getSqlSession().getMapper(StudentMapper.class).selectStudent();
}
public int InsertStudent(student student) {
return getSqlSession().getMapper(StudentMapper.class).InsertStudent(student);
}
public int DelStudent(int id) {
return getSqlSession().getMapper(StudentMapper.class).DelStudent(id);
}
}
测试类
//测试一个事务
@Test
public void Test(){
//读取Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentMapper studentMapper2 = context.getBean("StudentMapper2", StudentMapper.class);
List students = studentMapper2.selectStudent();
//输出内容
for (student s : students){
System.out.println(s);
}
}



