1、如果执行到订单明细插入有问题,那么订单的信息也不会在mysql插入
DEBUG [http-bio-8085-exec-1] - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@39e4f242] after transaction
DEBUG [http-bio-8085-exec-1] - Returning JDBC Connection to DataSource
org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'order_id' cannot be null
### The error may involve com.taotao.mapper.TbOrderItemMapper.insert-Inline
### The error occurred while setting parameters
### SQL: insert into tb_order_item (id, item_id, order_id, num, title, price, total_fee, pic_path) values (?, ?, ?, ?, ?, ?, ?, ?)
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'order_id' cannot be null
; SQL []; Column 'order_id' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'order_id' cannot be null
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:85)
2、定义事务:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.taotao.order.service.*.*(..))" />
</aop:config>
</beans>
3、 测试的代码:
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbOrderItemMapper;
import com.taotao.mapper.TbOrderMapper;
import com.taotao.mapper.TbOrderShippingMapper;
import com.taotao.order.dao.JedisClient;
import com.taotao.order.service.OrderService1;
import com.taotao.pojo.TbOrder;
import com.taotao.pojo.TbOrderItem;
import com.taotao.pojo.TbOrderShipping;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* @创建人 steffens
* @创建时间 2019/6/16
* @描述 文件创建
*/
@Service
public class OrderService1Impl implements OrderService1 {
@Autowired
private TbOrderMapper orderMapper;
@Autowired
private TbOrderItemMapper orderItemMapper;
@Autowired
private TbOrderShippingMapper orderShippingMapper;
@Autowired
private JedisClient jedisClient;
@Value("${ORDER_GEN_KEY}")
private String ORDER_GEN_KEY;
@Value("${ORDER_INIT_ID}")
private String ORDER_INIT_ID;
@Value("${ORDER_DETAIL_GEN_KEY}")
private String ORDER_DETAIL_GEN_KEY;
@Override
public TaotaoResult createOrder(TbOrder order, List<TbOrderItem> itemList, TbOrderShipping orderShipping) {
//向订单表中插入记录
//#获取订单编号
String string = jedisClient.get(ORDER_GEN_KEY);
if (StringUtils.isBlank(string)) {
jedisClient.set("ORDER_GEN_KEY", ORDER_INIT_ID);
}
long orderId = jedisClient.incr(ORDER_GEN_KEY);
//#补全POJO的属性
order.setOrderId(orderId + "");
order.setStatus(1);
Date date = new Date();
order.setCreateTime(date);
order.setUpdateTime(date);
//#0:未评价; 1:已评价
order.setBuyerRate(0);
//向订单表插入数据
orderMapper.insert(order);
//插入订单明细
for (TbOrderItem tbOrderItem : itemList) {
//补全订单明细
//取订单明细id
long orderDetailId = jedisClient.incr(ORDER_DETAIL_GEN_KEY);
tbOrderItem.setId(orderDetailId + "");
//向订单明细插入记录
orderItemMapper.insert(tbOrderItem);
}
//插入物流表
//补全物流信息
orderShipping.setOrderId(order + "");
orderShipping.setCreated(date);
orderShipping.setUpdated(date);
orderShippingMapper.insert(orderShipping);
return TaotaoResult.ok();
}
}