Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of code generator?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the principle of code generator". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "what is the principle of code generator"!

The principle of the code generator is simple, summed up in one sentence: convert database fields to Java fields and output the content.

As shown in the following figure:

Therefore, the first thing we need to do is to obtain the table information and the field information of the table. Generally, there are two ways. The first is to connect to the database, execute the relevant SQL, and query the table structure information. The second kind: parses the DDL, obtains the related information, code-gen adopts the first method.

Get table information

Including table names and table comments, Mysql can use the following SQL. (corresponding to code-gen source code: com.gitee.gen.gen.mysql.MySqlTableSelector)

SHOW TABLE STATUS FROM table_name

If you need to specify a table, you can add where conditions directly.

SHOW TABLE STATUS FROM table_name where name = 'user_info'

The results of the query are as follows:

NameEngineVersionRow_formatRowsAvg_row_lengthData_lengthMax_data_lengthIndex_lengthData_freeAuto_incrementCreate_timeUpdate_timeCheck_timeCollationChecksumCreate_optionsComment "user_info", "InnoDB", "10", "Dynamic", "0", "16384", "0", "0", "0", "1"2020-12-22 15:16:40"

"utf8_general_ci"

User Information Table

Get table field information

Mysql can use the following SQL (code-gen corresponds to source code: com.gitee.gen.gen.mysql.MySqlColumnSelector):

SHOW FULL COLUMNS FROM user_info

The results of the query are as follows:

FieldTypeCollationNullKeyDefaultExtraPrivilegesComment "id"int (11)"

"NO"PRI"

"auto_increment"select,insert,update,references"self-adding primary key"name"int (11)"

"NO"

"select,insert,update,references"name"create_time"timestamp"

"NO"

"CURRENT_TIMESTAMP"

"select,insert,update,references"add time"

From the table, you can get the field name, type, remarks, primary key, and self-increment.

After querying the basic information of the database, put this information into the object to facilitate subsequent processing.

/ * Database table definition, from which you can get table name, field information * / public class TableDefinition {/ * * table name * / private String tableName; / * table comment * / private String comment; / * field * / private List columnDefinitions. Omit getter setter}

TableDefinition class stores table information, and ColumnDefinition class stores field information.

The next step is to bind the variables in the TableDefinition object to the Velocity template.

VelocityContext context = new VelocityContext (); TableDefinition tableDefinition =... context.put ("table", tableDefinition); context.put ("columns", tableDefinition.getColumnDefinitions ()); return VelocityUtil.generate (context, template)

VelocityContext stores Velocity variables, and puts two variables in the object: table and columns,template are template contents

Then the corresponding variables can be used in the template, and the corresponding template can be written as follows:

/ * {table.comment} * / public class ${context.javaBeanName} {/ / Java Field Information # foreach ($column in $columns) / * * ${column.comment} * / private ${column.javaTypeBox} ${column.javaFieldName} # end// getter,setter part # foreach (${column} in ${columns}) public void set$ {column.javaFieldNameUF} (${column.javaTypeBox} ${column.javaFieldName}) {this.$ {column.javaFieldName} = ${column.javaFieldName};} public ${column.javaTypeBox} get$ {column.javaFieldNameUF} () {return this.$ {column.javaFieldName} } # end}

After getting the generated results, return the content to the front-end page.

Dealing with multiple databases

How to deal with a variety of databases, the key point here is to obtain the table structure and field information of the database. The acquisition SQL of each database is different, so it is necessary to abstract the table information and field information, and then let the actual database take the abstract part.

Code-gen corresponds to abstract classes:

Com.gitee.gen.gen.TableSelector abstract class, which is used to obtain the subclass of table information:

Com.gitee.gen.gen.mysql.MySqlTableSelector

Com.gitee.gen.gen.oracle.OracleTableSelector

Com.gitee.gen.gen.postgresql.PostgreSqlTableSelector

Com.gitee.gen.gen.sqlserver.SqlServerTableSelector

Com.gitee.gen.gen.ColumnSelector abstract class, which is used to obtain the subclass of table field information:

Com.gitee.gen.gen.mysql.MySqlColumnSelector

Com.gitee.gen.gen.oracle.OracleColumnSelector

Com.gitee.gen.gen.postgresql.PostgreSqlColumnSelector

Com.gitee.gen.gen.sqlserver.SqlServerColumnSelector

Thank you for your reading. the above is the content of "what is the principle of code generator". After the study of this article, I believe you have a deeper understanding of what the principle of code generator is. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report