Reduce workload, simple code generator

zhaozj2021-02-16  104

Cause: I am developing a simple MVC architecture system (no struts and ejb) ,. Discover that the simple table implementation is relatively simple, nothing more than a few methods, several SQL, form submit some data, to complete However, if there are too many fields, such as hundreds, even more. Treatment is very cumbersome, mainly cumbersome is:

1. Learn the SQL, INSERT statement inserted into the statement, is not handwritten.

2, the updated SQL is also a long story

3. Data query processing, give the data structure class (entity bean in the actionform, EJB in Struts) assigning a statement. Can't handw it.

4. Add or modify the page submission page in the most ordered page (if you do not use the parameters of UseBean, all)

Request.getParameter ("name"); to handle it, it is a dead.

Solution: Let the code generate these code,

Implementation: We all know that each table in our database will need a data structure class to save the data, the structure of the data structure class and the table is basically one, which is generally produced, in JB or other IDE. China provides production methods. When we produce, we pay attention to the principle of policy names and fields in the table. We use this struct class to produce the code we need.

process:

1. If the content of the structural class is as follows (must be made in the following format, this is also in this form, the other IDE is similar):

Private long ID; private string COM_CODE = "111"; private float e7; private float e8; private float e9; private float e10;

...........

2. We have a consideration of this data. In fact, we need, type and name. We only need to resolve this file, ask the Type and Name in a row, so you can generate the code we need.

3. It should be clear. As for the following code, you don't have to look at it. Write it, it is very messy, but it works. It is still lazy and rebuild. AA.TXT is the above data structure Content. The output structure is the re-console print. Bb.txt is not used. :)

To put it bluntly, it is also a bunch of garbage. There is no technical content, which cannot be an operation of a string. :) :)

Combine reference reference: http://blog.9cbs.net/shaokun305/Archive/2004/08/01/57794.aspx

/ ** *

Title: *

Description: *

Copyright: CopyRight (C) 2004 *

Company: Le Open Studio * @author Chen Shaokun * @version 1.0 * /

Import java.io. *;

Public class codemaker {

Public codemaker () {} public static void main (string [] args) {codeMaker code (); system.out.println ("//"); codeMaker1.get (); codeMaker1.set (); codeMAker1 . GetInsertsql (); codeMaker1.getupdatesql (); codeMAKER1.GETSUMSQL ();

/ ** * The code generation of the acquisition data of a page plane. * / Public void get () {string path = "c: //aa.txt"; string path1 = "c: //bb.txt";

Java.io.bufferedreader bin = null; java.io.bufferedwriter bout = null; try {bin = new java.io.bufferedreader (new file (path)); bout = new java.io .BUFFEREDWRITER (New filewriter (new file (ooException ex));} catch (ioException ex) {ex.printStackTrace ();} if (bin! = Null) {string aa = null; try {while AA = bin.readline ())! = null) {//system.out.println ("Aa===" aa); int based = aa.indexof ("private"); int end = aa.indexof ";"); If (begin == - 1) Break; string bb = aa.substring (begin 8, end); string [] cc = bb.split ("); if (cc! = Null) { String C1 = CC [0]; String C2 = CC [1]; if (c2.indexof ("=")! = - 1) {C2 = C2.SUBString (0, c2.indexof ("=")); } //System.out.println ("c1=" c1); // system.out.println ("C2 =" C2); string aa1 = c2.substring (0, 1) .touppercase (); string C22 = aa1 c2.substring (1);

IF (c1.equals ("string")) {system.out.println ("if (Request.GetParameter (/" C2 "/"); = null) "); system.out.println (" xxx. Set " C22 " (Request.GetParameter (/ " C2 " / "));");

IF (c1.equals ("long") {system.out.println ("if (Request.GetParameter); = null); system.out.println (" xxx. Set " C22 " (Request.GetParameter (/ " ")); ");} if (c1.equals (" float ")) {system.out.println (" if (Request.getParameter (/ " C2 " / ")! = null)"); system.out.println ("xxx.set" c22 "(float.parsefloat (Request.GetParameter (/" " C2 " / ")));");

IF (c1.equals ("int")) {system.out.println ("if (Request.GetParameter (/" C2 "/"); = null) "); system.out.println (" xxx. Set " C22 " (Integer.Parseint (Request.GetParameter (/ " C2 " / ")))));");

IF (C1.Equals ("Double")) {system.out.println ("if (Request.getParameter); = null); system.out.println (" xxx. SET " C22 " (Request.Parsedouble (/ " C2 ")))); ");}} aa = null;} system.out.println (" // "); bin. Close (); bout.close ();} catch (ooexception ex) {ex.printstacktrace ();}}}

/ ** * Assign a function to the data structure class. * / Public void set () {string path = "c: //aa.txt"; string path1 = "c: //bb.txt";

Java.io.bufferedreader bin = null; java.io.bufferedwriter bout = null; try {bin = new java.io.bufferedreader (new file (path)); bout = new java.io .BUFFEREDWRITER (New filewriter (new file (ooException ex));} catch (ioException ex) {ex.printStackTrace ();} if (bin! = Null) {string aa = null; try {while AA = bin.readline ())! = null) {//system.out.println ("Aa===" aa); int based = aa.indexof ("private"); int end = aa.indexof ";"); If (begin == - 1) Break; string bb = aa.substring (begin 8, end); string [] cc = bb.split ("); if (cc! = Null) { String C1 = CC [0]; String C2 = CC [1]; if (c2.indexof ("=")! = - 1) {C2 = C2.SUBString (0, c2.indexof ("=")); } //System.out.println ("c1=" c1); // system.out.println ("C2 =" C2); string aa1 = c2.substring (0, 1) .touppercase (); string C22 = aa1 c2.substring (1);

IF (C1.Equals ("String")) {system.out.println ("xxx.set" "(rs.getstring (/" " C2 " / "));");

IF (C1.Equals ("long")) {system.out.println ("xxx.set" "(rgetlong (/" C2 "/")); ");}

IF (c1.equals ("float")) {system.out.println ("xxx.set" "(rGetfloat (/" C2 "/")); ");

IF (c1.equals ("int") {system.out.println ("xxx.set" C22 "(rGetint (/" C2 "/")); ");} if (c1. Equals ("Double")) {system.out.println ("xxx.set" "(rgetdouble (/" c2 "/")); ");}} aa = null;} system. Out.Println ("//"); bin.close (); bout.close ();} catch (ioException ex) {ex.printstacktrace ();}}}

Public void getInsertsql () {string path = "c: //aa.txt"; string path1 = "c: //bb.txt";

INT count = 0; string SQL = "INSERT INTO ***** ("; string para = "{";

Java.io.bufferedreader bin = null; java.io.bufferedwriter bout = null; try {bin = new java.io.bufferedreader (new file (path)); bout = new java.io .BUFFEREDWRITER (New filewriter);} catch (ioException ex) {EX.PrintStackTrace ();

IF (bin! = null) {string aa = null; try {while ((aa = bin.readline ())! = null) {//system.out.println ("aa===" Aa); int Begin = aa.indexof ("private"); int end = aa.indexof (";"); if (begin == - 1) Break; count ; // count, see how many? To supplement SQL string string bb = aa.substring; string [] cc = bb.split ("); if (cc! = Null) {string C1 = CC [0]; string C2 = CC [1]; if (c2.indexof ("=")! = - 1) {C2 = c2.substring (0, c2.indexof ("="));} //system.out.println (" C1 = " C1); // system.out.println (" C2 = " C2); string aa1 = c2.substring (0, 1) .touppercase (); string c22 = aa1 c2.substring (1) ; // SQL = C2 ","; if (c1.equals ("string")) {para = "/" string / ", xxx.get" C22 "(),";

IF (c1.equals ("long") {para = "/" long / ", /" / " xxx.get" C22 "(),";

IF (c1.equals ("float") {para = "/" float / ", /" / " xxx.get" C22 "(),";}

IF (c1.equals ("int")) {para = "/" int / ", /" / " xxx.get" C22 "(),";

IF (C1.Equals ("Double") {para = "/" double / ", /" / " xxx.get" C22 "(),";}} aa = null;} // while loop Para = Para.Substring (0, Para.Length () - 1) "};"; SQL = SQL.SUBSTRING (0, SQL.Length () - 1); SQL = ") VALUES ("; for (INT i) = 0; I

SQL = "?,"

}

SQL = SQL.SUBString (0, SQL.LENGTH () - 1) ")";

///

System.out.println ("String SQL = /" " SQL " / ";");

System.out.println ("Object [] Para =" Para);

System.out.println ("//"); bin.close (); bout.close ();} catch (ioException ex) {ex.printstacktrace ();}}

}

/ ** * Get updage SQL statement * / public void getupdatesql () {string path = "c: //aa.txt"; string path1 = "c: //bb.txt";

INT count = 0; string SQL = "Update ***** set"; string para = "{";

Java.io.bufferedreader bin = null; java.io.bufferedwriter bout = null; try {bin = new java.io.bufferedreader (new file (path)); bout = new java.io .BUFFEREDWRITER (New filewriter);} catch (ioException ex) {EX.PrintStackTrace ();

IF (bin! = null) {string aa = null; try {while ((aa = bin.readline ())! = null) {//system.out.println ("aa===" Aa); int Begin = aa.indexof ("private"); int end = aa.indexof (";"); if (begin == - 1) Break; count ; // count, see how many? To supplement SQL string string bb = aa.substring; string [] cc = bb.split ("); if (cc! = Null) {string C1 = CC [0]; string C2 = CC [1]; if (c2.indexof ("=")! = - 1) {C2 = c2.substring (0, c2.indexof ("="));} //system.out.println (" C1 = " C1); // system.out.println (" C2 = " C2); string aa1 = c2.substring (0, 1) .touppercase (); string c22 = aa1 c2.substring (1) ; // SQL = C2 "= ?,"; if (C1.Equals ("String")) {Para = "/" String / ", XXX.GET" C22 "(),";

IF (c1.equals ("long") {para = "/" long / ", /" / " xxx.get" C22 "(),";

IF (c1.equals ("float") {para = "/" float / ", /" / " xxx.get" C22 "(),";}

IF (c1.equals ("int")) {para = "/" int / ", /" / " xxx.get" C22 "(),";

IF (C1.Equals ("Double") {para = "/" double / ", /" / " xxx.get" C22 "(),";}} aa = null;} // while loop Para = Para.Substring (0, Para.Length () - 1) "};"; SQL = SQL;

SQL = SQL.SUBSTRING (0, SQL.LENGTH () - 1) "WHERE ID =?"; /// system.out.println ("String SQL = /" SQL "/"; "); System .out.println ("Object [] para =" para);

System.out.println ("//");

Bin.close (); bout.close ();} catch (ioException ex) {ex.printstacktrace ();}}

}

/ ** * Get the function of all fields * / public void getsumsql () {string path = "c: //aa.txt"; string path1 = "c: //bb.txt";

INT count = 0; string SQL = "SELECT";

Java.io.bufferedreader bin = null; java.io.bufferedwriter bout = null; try {bin = new java.io.bufferedreader (new file (path)); bout = new java.io .BUFFEREDWRITER (New filewriter);} catch (ioException ex) {EX.PrintStackTrace ();

IF (bin! = null) {string aa = null; try {while ((aa = bin.readline ())! = null) {//system.out.println ("aa===" Aa); int Begin = aa.indexof ("private"); int end = aa.indexof (";"); if (begin == - 1) Break; count ; // count, see how many? To supplement SQL string string bb = aa.substring; string [] cc = bb.split ("); if (cc! = Null) {string C1 = CC [0]; string C2 = CC [1]; if (c2.indexof ("=")! = - 1) {C2 = c2.substring (0, c2.indexof ("="));} //system.out.println (" C1 = " C1); // system.out.println (" C2 = " C2); string aa1 = c2.substring (0, 1) .touppercase (); string c22 = aa1 c2.substring (1) ; // SQL = "SUM (" C2 ") AS" C2 ",";} aa = null;} // while loop

SQL = SQL.SUBSTRING (0, SQL.LENGTH () - 1); SQL = "from **** Where com_code <> '/" COM_CODE / "' and SUM_YEAR = /" Year / "and IS_SUBMIT = 1 "

/// system.out.println ("String SQL = /" " SQL " / ";");

System.out.println ("//");

Bin.close (); bout.close ();} catch (ioexception ex) {ex.printstacktrace ();}}}

}

转载请注明原文地址:https://www.9cbs.com/read-10212.html

New Post(0)