Java code generator implementation

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.