Based on struts2 + hibernate + spring, database fields are automatically increased in the format of time + self-increasing data.
(1) query the database table. The maximum value of the attribute is Oracle10g)
/**
* The largest auto-increment number in the query (Format: Time + auto-increment)
* Used as an account card or other auto-increment operations (Format: Time + auto-increment)
* @ Param STR table Field
* @ Param tables Table Name
* @ Return refers to the largest number.
*/
Private string getmaxzhkbh (final string STR, final string tables ){
String maxcompute (stringcontactgethibernatetemplate(cmd.exe cute (New hibernatecallback (){
Public object doinhibernate (session) throws hibernateexception, sqlexception {
String hql = "select max (" + STR + ") from" + tables + "as w where substr (" + STR + ", 0,8) =" + timestamp. gettime ();
Query q = session. createquery (hql );
List all = Q. List ();
If (all! = NULL & all. Size ()> 0 ){
Return all. Get (0 );
}
Return NULL;
}
});
Return Max;
}
(2) The number of auto-increment operations is generally less than 001,002,003, that is, when the number of digits increases is not enough to add zero. Of course, you have defined the number of digits.
/**
* Zero-padding is not enough.
* @ Param a number
* @ Param LEN Length
* @ Return
*/
Public string format (int A, int Len ){
String temp = string. valueof ();
While (temp. Length () <Len ){
Temp = "0" + temp;
}
Return temp;
}
(3) The following are the methods to be called in the action. When the maximum value is queried for the first operation of the database, it does not exist. Therefore, you must specify a value 001. Later, you can query the maximum value, and then increase the maximum value by one.
/**
* Generate auto-increment number
* @ Param STR table Field
* @ Param tables Table Name
* @ Return the new number
*/
Public String gettimestamp (string STR, string tables ){
String max = getmaxzhkbh (STR, tables );
String stamp = NULL;
If (max = NULL ){
Stamp = timestamp. gettime () + "-" + "001 ";
} Else {
String sub = max. substring (max. lastindexof ("-") + 1 );
Int I = integer. parseint (sub) + 1;
String temp = This. Format (I, 3 );
Stamp = timestamp. gettime () + "-" + temp;
}
Return stamp;
}
(4) timestamp. gettime () is to get the time format content, the Code is as follows.
Public class timestamp extends hibernatedaosupport {
Public timestamp (){
}
/**
* Gets the time.
*
* @ Return the time
*/
Public static string gettime (){
String temp = NULL;
Simpledateformat SDF = new simpledateformat ("yyyymmdd ");
Temp = SDF. Format (new date ());
Return temp;
}
}