[Payment System] How to generate an order number

        Usually we use the primary key in the database as the order number, and the id generation methods are auto-increment, uuid, and snowflake id. However, these are not suitable as order numbers. The order number generally reflects the year, month, day and other information. It is convenient to quickly locate the order at which time. If we directly use time as the ID, there will be the possibility of duplication. Here is the simplest way to introduce to you.

        Time + auto-increment id, here the auto-increment id of the database is borrowed to achieve the effect of not being repeated. It can also reflect the time 

 The id in our database is still an auto-incremented id, but a prefix is ​​added here, and the splicing method is used when using it. When adding an order, the time of the day will be inserted into it at the same time.

order.setIdPrefix(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));

We can set the auto-increment initial value of the id to a larger value. The page is shown as follows

 For convenience, we can define a method in the entity class to easily obtain the spliced ​​id.

Guess you like

Origin blog.csdn.net/qq_39078783/article/details/131321892