• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            Hibernate中UUID的生成算法

            /***********本人原創(chuàng),歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)保留本人信息*************/
            作者:王力猛 (wallimn)
            電郵:wallimn@sohu.com
            博客:http://wallimn.bokee.com
               http://blog.csdn.net/wallimn
            時(shí)間:2006-11-15
            /***********本人原創(chuàng),歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)保留本人信息*************/

              不知大家在寫數(shù)據(jù)庫(kù)程序的時(shí)候使用什么做主鍵,使用無(wú)意義的東東做主鍵是個(gè)很好的做法,很多人用SEQUENCE,可我覺(jué)得Hibernate中的UUID是個(gè)不錯(cuò)的選擇,我把他剝出來(lái),喜歡的人可以拿去用。
              算法如下:


            package com.xxh.hz.util;

            import java.io.Serializable;
            import java.net.InetAddress;
            /**
             * 唯一主鍵生成辦法。從Hibernate中提取出來(lái)。我一直覺(jué)得這不錯(cuò),不用建什么Sequence
             * @version      :  V1.0
             * @author       : 王力猛(Email: wallimn@sohu.com  QQ: 54871876)
             * @date           : 2006-12-8 下午11:51:43
             
            */

            public class UUIDGenerator {

             
            private static final int IP;
             
            public static int IptoInt( byte[] bytes ) {
              
            int result = 0;
              
            for (int i=0; i<4; i++{
               result 
            = ( result << 8 ) - Byte.MIN_VALUE + (int) bytes[i];
              }

              
            return result;
             }

             
            static {
              
            int ipadd;
              
            try {
               ipadd 
            = IptoInt( InetAddress.getLocalHost().getAddress() );
              }

              
            catch (Exception e) {
               ipadd 
            = 0;
              }

              IP 
            = ipadd;
             }

             
            private static short counter = (short0;
             
            private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );

             
            public UUIDGenerator() {
             }


             
            /**
              * Unique across JVMs on this machine (unless they load this class
              * in the same quater second - very unlikely)
              
            */

             
            protected int getJVM() {
              
            return JVM;
             }


             
            /**
              * Unique in a millisecond for this JVM instance (unless there
              * are > Short.MAX_VALUE instances created in a millisecond)
              
            */

             
            protected short getCount() {
              synchronized(UUIDGenerator.
            class{
               
            if (counter<0) counter=0;
               
            return counter++;
              }

             }


             
            /**
              * Unique in a local network
              
            */

             
            protected int getIP() {
              
            return IP;
             }


             
            /**
              * Unique down to millisecond
              
            */

             
            protected short getHiTime() {
              
            return (short) ( System.currentTimeMillis() >>> 32 );
             }

             
            protected int getLoTime() {
              
            return (int) System.currentTimeMillis();
             }

             
             
            private final static String sep = "";

             
            protected String format(int intval) {
              String formatted 
            = Integer.toHexString(intval);
              StringBuffer buf 
            = new StringBuffer("00000000");
              buf.replace( 
            8-formatted.length(), 8, formatted );
              
            return buf.toString();
             }


             
            protected String format(short shortval) {
              String formatted 
            = Integer.toHexString(shortval);
              StringBuffer buf 
            = new StringBuffer("0000");
              buf.replace( 
            4-formatted.length(), 4, formatted );
              
            return buf.toString();
             }


             
            public Serializable generate() {
              
            return new StringBuffer(36)
               .append( format( getIP() ) ).append(sep)
               .append( format( getJVM() ) ).append(sep)
               .append( format( getHiTime() ) ).append(sep)
               .append( format( getLoTime() ) ).append(sep)
               .append( format( getCount() ) )
               .toString();
             }


            }


            posted on 2008-05-19 10:02 楊粼波 閱讀(478) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            一本色道久久88—综合亚洲精品| 亚洲国产日韩欧美久久| 亚洲综合久久夜AV | 亚洲国产成人乱码精品女人久久久不卡 | 久久狠狠一本精品综合网| 久久成人小视频| 国产精品岛国久久久久| 久久久久亚洲精品中文字幕| 久久久av波多野一区二区| 欧美成a人片免费看久久| 日韩久久久久久中文人妻| 亚洲精品高清一二区久久| 久久天堂AV综合合色蜜桃网| 久久精品成人| 久久se精品一区精品二区| 色诱久久久久综合网ywww| 国产精品久久久久一区二区三区 | 人妻无码精品久久亚瑟影视| 久久精品国产免费| 亚洲AV无码久久精品狠狠爱浪潮 | 亚洲日本va中文字幕久久| 国内精品久久久久久麻豆 | 国产精品久久久久久吹潮| 亚洲欧美一区二区三区久久| 国产精品久久久久久搜索| 7777精品伊人久久久大香线蕉| 丰满少妇人妻久久久久久4| 国产成人久久精品一区二区三区 | 国产∨亚洲V天堂无码久久久 | 久久91综合国产91久久精品| 日韩av无码久久精品免费| 四虎国产精品成人免费久久| 亚洲国产天堂久久久久久| 久久精品国产亚洲7777| 色偷偷888欧美精品久久久| 7777久久亚洲中文字幕| 久久精品国产亚洲AV麻豆网站 | 国产精品久久久久9999高清| 久久发布国产伦子伦精品| 国产亚洲精品自在久久| 国产精品久久网|