您现在的位置:首页 >> 前端 >> 内容

mybatis一个业务多次提交事务commit造成主键不顺序递增问题

时间:2018/3/2 13:42:37 点击:

  核心提示:mybatis一个业务多次提交事务commit造成主键不顺序递增问题package com.dangdang.service;import java.text.DateFormat;import ja...

mybatis一个业务多次提交事务commit造成主键不顺序递增问题

package com.dangdang.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

import org.apache.ibatis.session.SqlSession;

import com.dangdang.dao.AddressDAO;
import com.dangdang.dao.OrderDAO;
import com.dangdang.dao.OrderDetallDAO;
import com.dangdang.entity.BuyAddress;
import com.dangdang.entity.Order;
import com.dangdang.entity.OrderDetall;
import com.dangdang.entity.User;
import com.dangdang.util.MyBatisUtil;
import com.dangdang.vo.Cart;
import com.dangdang.vo.OrderItem;

public class OrderServiceImpl implements OrderService{
	
	//收货地址入库、订单入库、订单项表入库
	@Override
	public void orderOrder(BuyAddress address, User user, Cart cart) {
		try {
			//收货地址入库
			address.setuId(user.getId());
			address.setAddrstatus(0);
			address.setIsDefault(0);
			//把User对象存放在Buyaddress
			address.setUser(user);
			
			AddressDAO ad = (AddressDAO) MyBatisUtil.getMapper(AddressDAO.class);
			ad.save(address);
			
			MyBatisUtil.commitForce();
			MyBatisUtil.closeSqlSession();
		} catch (Exception e) {
			MyBatisUtil.rollback();
			e.printStackTrace();
		}
		
		//订单入库
		Order order = null;
		try {
			//生成订单号onumber
			Date date=new Date();
			String createTime=new SimpleDateFormat("yyyyMMddHH:mm:ss").format(date);
			String onumber = createTime + UUID.randomUUID().toString();
			//把User对象、Buyaddress对象存放在Order对象里
			order = new Order(null, cart.getTotalprice(), user.getId(), onumber, user, 1, address.getAddrId()+1, address);
			//			  new Order(orderId, totalp00rice, uid, onumber, user, orderstatus, addrId, address)
			OrderDAO od = (OrderDAO) MyBatisUtil.getMapper(OrderDAO.class);
			od.save(order);
			
			MyBatisUtil.commitForce();
			MyBatisUtil.closeSqlSession();
		} catch (Exception e) {
			MyBatisUtil.rollback();
			e.printStackTrace();
		}
		
		//订单项表入库
		OrderDetallDAO odd = (OrderDetallDAO) MyBatisUtil.getMapper(OrderDetallDAO.class);
		try {
			Set keySet = cart.getCartMap().keySet();
			//遍历cartMap
			for (Integer key:keySet) {
//				System.out.println(key);  
				OrderItem orderItem = cart.getCartMap().get(key);
				
				OrderDetall orderDetall = new OrderDetall();
				
				orderDetall.setNumber(orderItem.getAmount());
				orderDetall.setTotal(orderItem.getSmallprice());
				orderDetall.setOrderid(order.getOrderId()+1);
				orderDetall.setGid(orderItem.getGoods().getGID());
				//调用DAO层
				odd.save(orderDetall);
			} 	
			MyBatisUtil.commit();
			MyBatisUtil.closeSqlSession();
		} catch (Exception e) {
			MyBatisUtil.rollback();
			e.printStackTrace();
		}
		
	}
	
}

几点说明目的:希望先后地址入库、订单入库、订单项表入库,并且主键是连续的现状:可以入库,但主键不连续三张表

收货地址表d_buyaddress(主键D_ADDRID)、

订单表d_buyorder(主键d_orderid,外键D_ADDRID)、

订单项(详情条目)表d_buyorderdetall(外键d_orderid)

mybatis一个业务多次提交事务commit造成主键不顺序递增问题

mybatis一个业务多次提交事务commit造成主键不顺序递增问题

mybatis一个业务多次提交事务commit造成主键不顺序递增问题

MyBatisUtil.commitForce();是sqlSession.commit(true);order.getOrderId()+1是不得已 将错就错 之举:

debug时发现order.getOrderId()的值 比 数据库order表的orderId的值小1(数据库里的值比预料的自然递增数多1,如d_orderid本该到100,实际却是101);

如果不加1,会报错:违反完整约束条件,未找到父项关键字。

Tags:MY YB BA AT 
作者:网络 来源:yunzhongxi