例子:对系统数据库中的订单信息新增的时候,对一个订单号字段的值进行递增
比如订单号字段为:YY_00000001,新建订单时。要读取数据库现有最新的订单号,然后将String类型正则取出8位的流水订单号,然后转化成Long类型,然后自增1,然后将Long类型的数据转化成String类型,并且拼凑订单的固定开头即可,Long转String时还需要补0(补零)
Object order_number = orderNumber.get("order_number");
if (order_number != null) {
Long aLong = Long.valueOf(Pattern.compile("[^0-9]").matcher((CharSequence) order_number).replaceAll(""));
aLong += 1;
String head = "YY_";
societyOrder.setOrderNumber(head + String.format("%08d", aLong));
}else {
societyOrder.setOrderNumber("YY_00000001");
}
补充:
Integer或Long转String时补0(补零)
Integer num = 10;
String numString = String.format("%08d", num);
Long num = 10l;
String numString = String.format("%08d", num);