START TRANSACTION;
select last_time into @time from create_orderid;
select num into @num from create_orderid;
select date_format(NOW(),'%Y%m%d') into @nowtime;
if STRCMP(@nowtime,@time)=0 THEN
update create_orderid set num=num+1,id=concat(@time,num);
ELSE
update create_orderid set num=1,id=concat(@nowtime,1),last_time=@nowtime;
end if;
select id into ids from create_orderid;
select ids from create_orderid;
COMMIT;
end
-----------------------------------------------------------------
java调用
// 获得订单id
@SuppressWarnings("deprecation")
public String getOrderId() {
java.sql.Connection con = getSession().connection();
String sql = "{call createOrId(?)}";// 调用存储过程
CallableStatement cs;
String name = "";
try {
cs = con.prepareCall(sql);
cs.registerOutParameter(1, java.sql.Types.VARCHAR);
cs.execute();
name = cs.getString(1);
} catch (SQLException e) {
e.printStackTrace();
}
return name;
}