In the actual project development, will encounter such a problem, the database table structure is well designed, but the entity class has not been developed accordingly. The property naming method of entity class is usually hump method, and the method of naming table field in database is using underscore method. If the table has a lot of fields, it is a bit time-consuming to write the properties of the hump again manually, based on the database fields that were designed. How to quickly change the table fields in the database into the camel-like properties we need?
The solution has two, one is through the text editing tools, such as editplus,notepad++, and so on, using the regular replacement function they carry to quickly achieve, and the second is to write their own tool class to achieve. As for the first method of operation skill, do not repeat here. The second method is as follows:
Here is the code for the tool class you wrote:
Packageday0704;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/*** Hump Method-Underline cross-turn *@authorCshaper *@since2015.07.04 *@version1.0.0*/ Public classUnderline2camel {/*** Underline Turn Hump method *@paramLine Source String *@paramSmallcamel Size Hump, whether for small hump *@returnconverted string*/ Public StaticString Underline2camel (String line,BooleanSmallcamel) { if(line==NULL||"". Equals (line)) { return""; } stringbuffer SB=NewStringBuffer (); Pattern Pattern=pattern.compile ("([a-za-z\\d]+) (_)?"); Matcher Matcher=Pattern.matcher (line); while(Matcher.find ()) {String Word=Matcher.group (); Sb.append (Smallcamel&&matcher.start () ==0? Character.tolowercase (Word.charat (0)): Character.touppercase (Word.charat (0))); intIndex=word.lastindexof (' _ ')); if(index>0) {sb.append (word.substring (1, index). toLowerCase ()); }Else{sb.append (word.substring (1). toLowerCase ()); } } returnsb.tostring (); } /*** Hump method turns underline *@paramLine Source String *@returnconverted string*/ Public Staticstring Camel2underline (string line) {if(line==NULL||"". Equals (line)) { return""; } Line=string.valueof (Line.charat (0)). toUpperCase (). Concat (line.substring (1)); StringBuffer SB=NewStringBuffer (); Pattern Pattern=pattern.compile ("[A-z] ([a-z\\d]+)?"); Matcher Matcher=Pattern.matcher (line); while(Matcher.find ()) {String Word=Matcher.group (); Sb.append (Word.touppercase ()); Sb.append (Matcher.end ()==line.length ()? "": "_"); } returnsb.tostring (); } Public Static voidMain (string[] args) {String line= "I_have_an_ipang3_pig"; String Camel=underline2camel (Line,true); System.out.println (camel); System.out.println (Camel2underline (camel)); }}
The results of the operation are as follows:
Ihaveanipang3pig
I_have_an_ipang3_pig
"Original" String tool class--hump method and underline method of mutual transfer