Spring声明式事务

先把所有代码都放上来:

1,实体类:User.java

 1 package com.maolei.pojo;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 //使用lombok
 8 @Data
 9 @AllArgsConstructor
10 @NoArgsConstructor
11 public class User {
12     private int id;
13     private String name;
14     private String pwd;
15 
16 }

2:接口:UserMapper.java

 1 package com.maolei.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.maolei.pojo.User;
 6 
 7 public interface UserMapper {
 8     public List<User> selectUser();
 9     public int addUser(User user);
10     public int deleteUser(int id);
11 
12 }

3:接口实现类:UserMapperImpl.java

 1 package com.maolei.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.mybatis.spring.support.SqlSessionDaoSupport;
 6 
 7 import com.maolei.pojo.User;
 8 
 9 public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{
10 
11     public int addUser(User user) {
12         return getSqlSession().getMapper(UserMapper.class).addUser(user);
13     }
14 
15     public int deleteUser(int id) {
16         return getSqlSession().getMapper(UserMapper.class).deleteUser(id);
17     }
18 
19     public List<User> selectUser() {
20         User user = new User(5,"王力宏","857857");
21         addUser(user);
22         deleteUser(1);
23         return getSqlSession().getMapper(UserMapper.class).selectUser();
24     }
25 
26 }

4:接口的映射文件:UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 <mapper namespace="com.maolei.mapper.UserMapper">
 5     <select id="selectUser" resultType="user">
 6         select * from user;
 7     </select>
 8     <insert id="addUser" parameterType="user">
 9         insert into user(id,name,pwd) value(#{id},#{name},#{pwd})
10     </insert>
11     <delete id="deleteUser" parameterType="int">
12         deletes from user where id =#{id}
13     </delete>
14 </mapper>

注意这里故意把delete写成deletes,让sql执行出错

5:mybatis-config.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
3         "http://mybatis.org/dtd/mybatis-3-config.dtd">
4 <configuration>
5     <typeAliases>
6         <!-- <typeAlias alias="User" type="com.maolei.pojo.User"/> -->
7         <package name="com.maolei.pojo"/>
8     </typeAliases>
9 </configuration>

6:spring-dao.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         https://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop
 9         https://www.springframework.org/schema/aop/spring-aop.xsd
10         http://www.springframework.org/schema/tx
11         https://www.springframework.org/schema/tx/spring-tx.xsd">
12     
13     <!-- dataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid 
14     我们这里使用的Spring提供的JDBC:org.springframework.jdbc.datesource -->
15     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
17         <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=GMT"/>
18         <property name="username" value="root"/>
19         <property name="password" value="123456"/>
20     </bean>
21     <!-- sqlsessionFactory -->
22     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
23           <property name="dataSource" ref="dataSource" />
24           <!-- 绑定Mybatis配置文件 -->
25           <property name="configLocation" value="classpath:mybatis-config.xml"/>
26           <property name="mapperLocations" value="classpath:com/maolei/mapper/*.xml"/>
27     </bean>
28     <!-- sqlsessionTemplate:就是我们使用的sqlsession-->
29     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
30         <constructor-arg index="0" ref="sqlSessionFactory"/>
31     </bean>
32     
33     <!-- 配置声明式事务 -->
34     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
35           <constructor-arg ref="dataSource" />
36     </bean>
37     <!-- 结合AOP实现事务的织入 -->
38     <!-- 配置事务通知 -->
39     <tx:advice id="txAdvice" transaction-manager="transactionManager">
40         <!-- 给哪些方法配置事务 -->
41         <!-- 配置事务的传播特性:new propagation -->
42         <tx:attributes>
43             <tx:method name="addUser" propagation="REQUIRED"/>
44             <tx:method name="delete*" propagation="REQUIRED"/>
45             <tx:method name="*" propagation="REQUIRED"/>
46         </tx:attributes>
47     </tx:advice>
48     <!-- 配置事务切入 -->
49     <aop:config>
50         <aop:pointcut expression="execution(* com.maolei.mapper.*.*(..))" id="txPointCut"/>
51         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
52     </aop:config>
53 
54 
55 </beans>

 

这个xml里加粗的部分们就是spring声明式事务的体现。

这里注意下Spring中propagation的7种事务配置

  • Spring中七种Propagation类的事务属性详解:

REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。

MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。

REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。

NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。

NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

另外,<tx:method name="..."/>里的name定义是根据正则表达式匹配接口中的方法名,上面的xml中的三种name定义,分别能匹配到addUser,deleteUser,所有方法。

7.applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop
 8         https://www.springframework.org/schema/aop/spring-aop.xsd">
 9     
10     <import resource="spring-dao.xml"/>
11     
12     <bean id="userMapper" class="com.maolei.mapper.UserMapperImpl">
13         <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
14     </bean>
15 
16 
17 </beans>

8:测试类:MyTest.java

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Test;
10 import org.mybatis.spring.SqlSessionTemplate;
11 import org.springframework.context.ApplicationContext;
12 import org.springframework.context.support.ClassPathXmlApplicationContext;
13 
14 import com.maolei.mapper.UserMapper;
15 import com.maolei.pojo.User;
16 
17 public class MyTest {
18     @Test
19     public void getUser() throws IOException {
20         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
21         UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
22         List<User> userList = userMapper.getUser();
23         for (User user : userList) {
24             System.out.println(user);
25         }
26     }
27 
28 }

测试结果:

### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'deletes from user where id =1' at line 1

先执行的addUser方法插入的数据也没有提交在数据库中


posted @ 2021-12-21 15:49  Rookie12138  阅读(29)  评论(0编辑  收藏  举报