Generation of serial Number (date + business code + auto-increment sequence)

Source: Internet
Author: User

Project development, the use of two sets of databases, development environment and on-line environment, the database tables are mostly using the self-increment primary key,
Like what:

idintunsigned primary key auto_increment,

But there are often problems, such as:
In the development environment, crawling some data using crawlers, indexing, and then migrating the data to the on-line environment will result in an ID in the index and
The on-line environment database does not have an ID pair, so it is decided to use a string as the primary key.

So here's the question, how do I generate a unique serial number?
Format by: yyyymmdd+ Two-bit business code + 10-bit self-increment sequence,

Like 20150101**99**0000000001.

Ideas:
Getting dates is simple;
The business code is the parameter that the calling service passed in;
Use Redis to save and self-increment the 10-bit self-increment sequence, using the format of the serial.number:{date} to hold the value of the self-increment sequence for a given day;

The main code is as follows:

 Public  interface serialnumberservice {    /** * serial number self-increment sequence */String Serial_number ="Serial.number:";/** * Generates a serial number according to the two-bit business code string, formatted according to the:<br/> * yyyymmdd{bizcode}{10 bit of the self-increment sequence number} * @param Bizcode * Two-bit, 00-99 * @return 20-bit serial number * @throws serviceexception */String Generate (String bizcode)throwsServiceexception;default BooleanIslegal (String bizcode) {if(Bizcode = =NULL|| Bizcode.length ()! =2) {Throw NewRuntimeException ("Bizcode:"+ Bizcode +"Exception"); }if(Character.isdigit (Bizcode.charat (0)) && Character.isdigit (Bizcode.charat (1)))return true;return false; }}@Service Public  class Serialnumberserviceimpl implements serialnumberservice {    @Resource    PrivateRedisdao Redisdao;@Override     PublicStringGenerate(String Bizcode)throwsserviceexception {/** Check the business code * *        BooleanIslegal = Islegal (Bizcode);if(!islegal) {Throw NewServiceexception ("Bizcode parameter is not valid"); }/** Get today's date: YYYYMMDD */String date = Timeutil.gettoday ();/** to construct a redis key * /String key = Serial_number + date;/** Self-increment * /        Longsequence = REDISDAO.INCR (key);        String seq = stringutil.getsequence (sequence); StringBuilder SB =NewStringBuilder ();        Sb.append (date). Append (Bizcode). append (seq); String serial = Sb.tostring ();returnSerial }} Public  class timeutil {    Private Timeutil() {    }/** * Get today's date * * @return  * *     Public StaticStringGettoday() {return "20150101"; }} Public  class stringutil {    Private Stringutil() {    }Static Final intDefault_length =Ten;/** * Get 10-bit serial number, length less than 10 bits, front 0 * * @param seq * @return * *     Public StaticStringgetsequence(LongSEQ) {String str = string.valueof (seq);intLen = Str.length ();if(Len >= default_length) {//Depending on the size of the business, it should not reach ten            returnStr }intrest = Default_length-len; StringBuilder SB =NewStringBuilder (); for(inti =0; I < rest; i++) {Sb.append (' 0 '); } sb.append (str);returnSb.tostring (); }}

Only the Redisdao interface is declared and can be implemented using the Jedis client.

publicinterface RedisDao {    String get(String key);    /**     * 自增,+1,返回增加后的值     *      * @param key     * @return     */    long incr(String key);}

Generation of serial Number (date + business code + auto-increment sequence)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.