The data record must be in the following format (and the right side of the last comma delimiter cannot be empty)
Oub_shipment_header_id,shipment_no,status,status_ext,priority
12313412416491,gf1712262040050,100,0
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.lang.reflect.Field;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportCom.test.data_provide.entity.OubShipmentHeader; Public classsql2entity {List<String> list=NewArraylist<string>(); List<String> listone=NewArraylist<string>(); List<String> listtwo=NewArraylist<string>(); Public voidToentity (File rfile,file wfile,class<?> obj,string entityinstance)throwsioexception{//read each line to listList<string> rlist=Readtxt (Rfile); intCount=0; //iterate through the value of list for(String l:rlist) {//"," split off, ordered theString[] Larray=l.split (","); //iterate over each value of the string array for(inti=0;i<larray.length;i++) {StringBuilder sb=NewStringBuilder (); //processing the first line if(count==0){ //then "_" split off, orderlyString[] tarray= Larray[i].split ("_"); //cycle through each value of the string, capitalize the first letter, and then Stitch for(intj=0;j<tarray.length;j++) {sb.append (Prewordcapital (tarray[j)); } listone.add (Sb.tostring ()); } //Process the second row of value values if(count==1) {Listtwo.add (larray[i]); } } Count++; } //Stitching Set Methodwritetxt (Wfile,concatlist (listone,listtwo,obj,entityinstance)); //writetxt (Wfile,listone); //writetxt (wfile,listtwo); } Public voidWritetxt (File file,list<string> List)throwsioexception{FileWriter FW=NewFileWriter (file,false); BufferedWriter BW=NewBufferedWriter (FW); for(String l:list) {bw.write (L); Bw.write ("\ r"); } bw.close (); Fw.close (); } Public Static voidMain (string[] args)throwsIOException {File rfile=NewFile ("D:/mytest1.txt"); File Wfile=NewFile ("D:/entity.txt"); Sql2entity SE=Newsql2entity (); Se.toentity (Rfile, Wfile, Oubshipmentheader.class, "Oubshipmentheader"); /*Se.readtxt (Rfile); list<string> list = new arraylist<string> (); List.add ("Wode"); List.add ("Ren"); Se.writetxt (wfile,list);*/ } PublicList<string>readtxt (file file) {List<String> list=NewArraylist<string>(); StringBuilder result=NewStringBuilder (); Try{bufferedreader br=NewBufferedReader (NewFileReader (file));//constructs a BufferedReader class to read the fileString s =NULL; while((s = br.readline ())! =NULL){//use the ReadLine method to read one line at a time//Result.append (System.lineseparator () +s); //System.out.println (s+ "\ n");List.add (s); } br.close (); }Catch(Exception e) {e.printstacktrace (); } returnlist; } Publicstring Prewordcapital (string word) {string Lowerword=word.tolowercase (); returnLowerword.substring (0, 1). toUpperCase () + lowerword.substring (1); } PublicList<string> concatlist (list<string> listone,list<string> listtwo,class<?>obj,string objinstance) {field[] Field=Obj.getdeclaredfields (); System.out.println ("Listone.size ()" +listone.size () + "listtwo.size ():" +listtwo.size ()); if(Listone.size ()! =listtwo.size ()) {System.err.println ("Field does not correspond to the number of values, please check whether the last one, the right side of the separator is empty, must guarantee the last, the right of the separator is not empty ..."); } for(intI=0;i<listone.size (); i++){ intJ=0; for(j = 0; J < Field.length; J + +) {field[j].setaccessible (true); //System.out.println (Field[j].getname ()); if(Field[j].getname (). toString (). Equalsignorecase (Listone.get (i))) {//System.out.println (Listone.get (i)); //System.out.println (Field[j].getname (). toString ()); Break; } } //gets the type of the propertyString type =Field[j].getgenerictype (). toString (); //concatenation of string types if(Type.equals ("Class java.lang.String")) {list.add (objinstance+ ". Set" +listone.get (i) + "(" + "\" "+listtwo.get (i) +" \ "+") "); } //int type splicing if(Type.equals ("Class Java.lang.Integer")) {list.add (objinstance+ ". Set" +listone.get (i) + "(" +listtwo.get (i) + ")"); } //Long type splicing if(Type.equals ("Class Java.lang.Long")) {list.add (objinstance+ ". Set" +listone.get (i) + "(" +listtwo.get (i) + "L" + ")"); } //BigDecimal Type splicing if(Type.equals ("Class Java.math.BigDecimal")) {list.add (objinstance+ ". Set" +listone.get (i) + "(" + "Parames.getbigdecimalparams (" +listtwo.get (i) + ")" + ")"); } //Data type splicing if(Type.equals ("Class Java.util.data")) {list.add (objinstance+ ". Set" +listone.get (i) + "(" + "parames.getdateparams ()" + ")"); } //byte type stitching if(Type.equals ("Class Java.lang.Byte")) {list.add (objinstance+ ". Set" +listone.get (i) + "(" + "Parames.getbyteparams (" +listtwo.get (i) + ")" + ")"); } } returnlist; }}
Generate initialization for the corresponding entity class based on one of the MySQL records