mybatis集成spring的事务管理

第一 创建一个测试实体

public class Order {

private int id;
private String orderName;

public Order(String orderName) {
this.orderName = orderName;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
}


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

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

public interface OrderMapper {

void insertOrder(Order order);
}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sagaware.mapper.OrderMapper">
<!-- 开启缓存 -->
<cache />
<insert id="insertOrder" parameterType="Order" keyProperty="id" useGeneratedKeys="true">
insert into tb_order(name) values (#{orderName})
</insert>
</mapper>



第三步 写一个service类


@Service("orderService")
public class OrderService {

@Autowired
private OrderMapper mapper;

/**
* 事务处理必需抛出异常 spring 才会帮事务回滚
* @param orders
*/
@Transactional
public void insertOrder(List<Order> orders) {
for(int i = 0 ; i < orders.size() ; i++) {
if(i < 2) {
mapper.insertOrder(orders.get(i));
} else {
throw new RuntimeException();
[code="java"]
}
}
}
[/code]

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


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>

<!-- 开启注解配置 -->
<context:annotation-config />

<!-- 扫描service层 -->
<context:component-scan base-package="com.sagaware.service" />

<!-- 开启事务注解驱动 -->
<tx:annotation-driven />

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 创建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.sagaware.entity" />
</bean>

<!-- 自动扫描映射器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sagaware.mapper" />
</bean>
</beans>


最后 写一个测试类


public class Main {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
OrderService service = (OrderService) context.getBean("orderService");
System.out.println(service);
List<Order> orders = new ArrayList<Order>();
for(int i = 0 ; i < 5 ; i++) {
Order order = new Order("订单" + i);
orders.add(order);
}
service.insertOrder(orders);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值