Neil的備忘錄

          just do it
          posts - 66, comments - 8, trackbacks - 0, articles - 0
              最近項(xiàng)目中使用到的一個(gè)主鍵自動(dòng)生成策略,持久層框架為Hibernate,數(shù)據(jù)庫為Oracle9i.
              生成方式是:當(dāng)天時(shí)間(8位)+流水號(hào)(8位),以下是代碼
             
           1 import java.io.Serializable;
           2 import java.text.SimpleDateFormat;
           3 import java.util.Date;
           4 import java.util.Properties;
           5 
           6 import org.hibernate.HibernateException;
           7 import org.hibernate.dialect.Dialect;
           8 import org.hibernate.engine.SessionImplementor;
           9 import org.hibernate.id.TableGenerator;
          10 import org.hibernate.type.Type;
          11 
          12 import com.seektax.tams.util.StringUtil;
          13 
          14 public class MyCustomizedIdGenerator extends TableGenerator {
          15     private String rulesize;
          16     private String curDate;
          17     
          18     public void configure(Type type, Properties params, Dialect d) { 
          19         super.configure(type, params, d);
          20         curDate = params.getProperty("curDate"== null ? "dddddddd" : getCurDate();
          21         rulesize = params.getProperty("rulesize"== null ? "0" :
          22         params.getProperty("rulesize"); 
          23         } 
          24 
          25         public synchronized Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
          26             StringBuffer Iddm = new StringBuffer(512);
          27             String tempid = String.valueOf(super.generate(session, obj)); 
          28             tempid = StringUtil.LPAD(tempid, Integer.valueOf(rulesize).intValue(), "0");
          29             if(tempid.length() > Integer.parseInt(rulesize))
          30             {
          31                 tempid = tempid.substring(tempid.length()-Integer.parseInt(rulesize), tempid.length());
          32             }
          33             Iddm.append(curDate);
          34             Iddm.append(tempid);
          35             return new Long(Long.parseLong((Iddm.toString())));
          36         } 
          37         
          38         /**
          39          * 獲取當(dāng)天日期
          40          */
          41         private String getCurDate()
          42         {
          43             Date curDate = new Date();
          44             SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
          45             String curDay = sdf.format(curDate);
          46             return curDay;
          47         }
          48         
          49 }
          50 
              String tempid = String.valueOf(super.generate(session, obj)); 
              tempid = StringUtil.LPAD(tempid, Integer.valueOf(rulesize).intValue(), "0");
              以上兩句話是當(dāng)自動(dòng)生成的ID位數(shù)不夠時(shí)自動(dòng)補(bǔ)零.方法代碼為:
           1 /**
           2      * 類似于oralce LPAD 從左到右
           3      * @param source
           4      * @param newSize
           5      * @param replaceString
           6      * @return
           7      */
           8     public static String LPAD(String source, int newSize, String replaceString) {
           9         String result = "";
          10         if (source.length() < newSize) {
          11             for (int i = 0; i < newSize - source.length(); i++) {
          12                 result += replaceString;
          13             }
          14             result = result + source;
          15 
          16         } else {
          17             result = source;
          18         }
          19         return result;
          20     }
              然后,需要在配置文件中配置該主鍵生成策略,代碼如下:
          1        <id name="id" type="long">
          2             <column name="ID" precision="16" scale="0" />
          3             <generator class="com.seektax.tams.modules.xxcj.MyCustomizedIdGenerator"> 
          4               <param name="table">SERIAL_NO</param>    //數(shù)據(jù)庫表名
          5               <param name="column">DAJBXXID</param>    //表中字段名
          6               <param name="curDate">curDate</param>   
          7               <param name="rulesize">8</param>         //定義位數(shù)
          8             </generator> 
          9         </id>
              配置好這些就OK了!
              注:由于項(xiàng)目中使用的是雙主鍵,在插入一方的同時(shí),多方也要重新獲得主鍵,如:
          1       DaPageId id = new DaPageId();
          2       id.setXh(i+1);
          3       id.setDaId(jbxx.getId());
              (完)

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 嘉义县| 南宫市| 来凤县| 宜都市| 冷水江市| 盘山县| 德江县| 阿拉尔市| 桑植县| 依安县| 南乐县| 贵定县| 塔河县| 峡江县| 荆门市| 安龙县| 申扎县| 台东县| 井陉县| 油尖旺区| 长乐市| 三河市| 巴马| 龙门县| 平顺县| 萨迦县| 六安市| 永济市| 惠来县| 玛纳斯县| 博白县| 彰化县| 平江县| 阳曲县| 普陀区| 宣汉县| 合作市| 稷山县| 承德市| 桦甸市| 昌乐县|