完整例子:spring整合mybatis,并使用spring事务

0 建表

create table student(id bigint primary key, name varchar(30), age tinyint unsigned);
insert into student values(12014052074, '小黄', 21);
insert into student values(12014052075, '小红' , 18);

1 项目目录结构

目录结构.PNG

2 maven依赖



    4.0.0

    demo
    demo
    1.0-SNAPSHOT

    
            4.2.6.RELEASE
    

    
        
            org.projectlombok
            lombok
            1.16.18
            provided
        
        
            junit
            junit
            4.10
            test
        
        
            org.springframework
            spring-test
            ${spring.version}
            test
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        

        
            org.mybatis
            mybatis-spring
            2.0.6
        
        
            org.mybatis
            mybatis
            3.5.0
        
        
            mysql
            mysql-connector-java
            5.1.10
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

3 创建po对象

@Data
@AllArgsConstructor
public class Student {
    private long id;
    private String name;
    private int age;
}

4 创建dao接口

public interface StudentDao {
    int updateStudent(Student s); // 更新记录
    List getAllStudent(); // 获取所有记录
}

5 创建StudentDao的mapper配置(studentDao.xml)




    
        update student set name=#{name}, age=#{age} where id=#{id}
    
    
        
        
        
    
    

6 mybatis.xml配置文件




    
        
    

用于引入所有的mapper配置,本例中只有一个studentDao.xml。

7 spring的配置文件(applicationContext.xml)



    
    

    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
        
    

    
    
        
    
    
    


8 两个service类以及测试类

@Service
public class ServiceB {
    @Autowired
    private StudentDao dao;

    @Transactional(propagation = Propagation.NESTED)
    public void updateStudentB(long id) {
        Student studentB = new Student(id, "B修改", 20);
        dao.updateStudent(studentB);
        if (studentB.getAge() < 15) {
            System.out.println("B抛了一个异常");
            throw new RuntimeException("B嫌年龄太小了");
        }
    }
}
@Service
public class ServiceA {
    @Autowired
    private StudentDao dao;
    @Autowired
    private ServiceB serviceB;

    @Transactional()
    public void updateStudentA(long id) {
        try {
            serviceB.updateStudentB(id);
        } catch (RuntimeException e) {
            System.out.println("A捕获了B的运行时异常");
        }
        Student studentA = new Student(id, "A修改", 18);
        dao.updateStudent(studentA);
        if (studentA.getAge() < 20) {
            System.out.println("A抛了一个异常");
            throw new RuntimeException("A嫌弃年龄太小了");
        }
    }
}

用这两个service可以测试spring事务的各种传播机制。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/applicationContext.xml"})
public class SpringTransactionTest {
    @Autowired
    private ServiceA serviceA;
    @Autowired
    private StudentDao dao;

    @Test
    public void transTest() {
        try {
            serviceA.updateStudentA(12014052074L);
        } catch (RuntimeException e) {
            System.out.println("test捕获了A一个运行时异常:"+e.getClass());
        }
        System.out.println(dao.getAllStudent());
    }
}
运行结果.PNG

你可能感兴趣的:(完整例子:spring整合mybatis,并使用spring事务)