首页 > 代码库 > mybatis集成spring的事务管理

mybatis集成spring的事务管理

第一 创建一个测试实体

 1 public class Order { 2  3     private int id; 4     private String orderName; 5      6     public Order(String orderName) { 7         this.orderName = orderName; 8     } 9     10     public int getId() {11         return id;12     }13     public void setId(int id) {14         this.id = id;15     }16     public String getOrderName() {17         return orderName;18     }19     public void setOrderName(String orderName) {20         this.orderName = orderName;21     }22 }

第二 创建映射器以及对应的xml

只是做了一个简单的订单映射 

1 public interface OrderMapper {2 3     void insertOrder(Order order);4 }
 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  5 <mapper namespace="com.sagaware.mapper.OrderMapper"> 6 <!-- 开启缓存 --> 7   <cache /> 8   <insert id="insertOrder" parameterType="Order" keyProperty="id"        useGeneratedKeys="true"> 9       insert into tb_order(name) values (#{orderName}) 10   </insert>11 </mapper>

第三步 写一个service类 

 

 1 @Service("orderService") 2 public class OrderService { 3  4     @Autowired 5     private OrderMapper mapper; 6      7     /** 8      * 事务处理必需抛出异常 spring 才会帮事务回滚 9      * @param orders10      */11     @Transactional12     public void insertOrder(List<Order> orders) {13         for(int i = 0 ; i < orders.size() ; i++) {14             if(i < 2) {15                 mapper.insertOrder(orders.get(i));16             } else {17                 throw new RuntimeException();18         <pre name="code" class="java"></pre>    }19 <br>        }20 <br>    }21 <br>

第四部 也就是重点,配置spring配置文件 

 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" xmlns:aop="http://www.springframework.org/schema/aop" 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 5     xmlns:context="http://www.springframework.org/schema/context" 6     xsi:schemaLocation=" 7      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 8      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd10      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd11      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">12 13     <!-- 数据源 -->14     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">15         <property name="driverClass" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property>16         <property name="jdbcUrl" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/test"></property>17         <property name="user" value="http://www.mamicode.com/root"></property>18         <property name="password" value="http://www.mamicode.com/root"></property>19     </bean>20 21     <!-- 开启注解配置 -->22     <context:annotation-config />23 24     <!-- 扫描service层 -->25     <context:component-scan base-package="com.sagaware.service" />26 27     <!-- 开启事务注解驱动 -->28     <tx:annotation-driven />29 30     <!-- 事务管理器 -->31     <bean id="transactionManager"32         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">33         <property name="dataSource" ref="dataSource" />34     </bean>35 36     <!-- 创建SqlSessionFactory -->37     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">38         <property name="dataSource" ref="dataSource" />39         <property name="typeAliasesPackage" value="http://www.mamicode.com/com.sagaware.entity" />40     </bean>41 42     <!-- 自动扫描映射器 -->43     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">44         <property name="basePackage" value="http://www.mamicode.com/com.sagaware.mapper" />45     </bean>46 </beans>

最后 写一个测试类

 1 public class Main { 2  3     public static void main(String[] args) { 4         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 5         OrderService service = (OrderService) context.getBean("orderService"); 6         System.out.println(service); 7         List<Order> orders = new ArrayList<Order>(); 8         for(int i = 0 ; i < 5 ; i++) { 9             Order order = new Order("订单" + i);10             orders.add(order);11         }12         service.insertOrder(orders);13     }14 }