1. Reasons for development
Recently, the company encountered a large project involving a large number of database tables. During coding, we will find that
A lot of work is boring. Every time we need to implement the corresponding model object of the table, database operation class,
Adding, deleting, and updating pages are similar.
However, it takes a lot of time to do it one by one, and it is prone to errors.CodeGenerator is
It is imperative that I start searching online, either because the function is too weak to meet our goal, or
Learning is too complicated, so I decided to do it myself.
2. Demand Analysis
We generally develop data based on databases and generally implement model classes, database operation classes, and form pages.
It is actually implemented for tables.
I analyzed:
A table has a table name and description. In addition, many columns in the table have class names, column types, column comments, and whether the column is a primary key.
Then we will naturally construct such an object.
Class tablemodel
{
// Table name
Private string tablename;
// Table comment
Private string tabcomment;
// Column list
Private list <columnmodel> columnlist = new arraylist <columnmodel> ();
}
// Column object
Class columnmodel
{
// Column name
Private string columnname;
// Column type
Private string columntype;
// Whether it is set up
Private Boolean isprimarykey = false;
// Primary key order.
Private int pkseq = 0;
}
With this tablemodel, we can use the template engine to render it into the code we need.
I found velocity is a good template engine. We can define templates and implement our code as needed.