欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-06 09:32:47
mybatis一个业务多次提交事务commit造成主键不顺序递增问题 package com.dangdang.service; import java.text.da...

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,会报错:违反完整约束条件,未找到父项关键字。