In the project, a lot of Meta, Dao, and Service codes need to be written based on database tables, and many of them are repetitive and cumbersome. Therefore, if there is a generator for template code, development efficiency can be improved to a certain extent.
Configurable generator that generates Dao, Meta, and Service layer template codes in Java Web projects.
1 2 3 4 5 6 |
|
The project refers to the process of generating code with Mybatis generator. The specific steps are divided into the following 5 steps.
Logical steps
Parsing the command line
ParsingConfiguration file
Get data table information
Generate configuration information
Generate file
Command parsing class ShellRunner
This class is responsible for parsing command line commands, parsing configuration files and encapsulating the required data to Code generation class.
The parsable commands include -config<a href="http://www.php.cn/wiki/1313.html" target="_blank">file</a>
: specify the path where the configuration file is located and -overwrite
: whether to rewrite the target file.
The configuration items of the configuration file are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Code generation class CodeGenerator
This class is responsible for connecting to the database, querying the table information of the data table, and SQL types are mapped to Java types and encapsulate the required data to file generation classes.
1 2 3 4 5 6 7 8 |
|
Through the above lines of code, the table information of the target data table has been obtained in the rs variable.
databaseMetaData.getColumns
The essence of the method is to execute the SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME="tableName"
statement.
In the result set, subsequent processing generally requires the following table information columns.
##DATA_TYPECOLUMN_SIZEData length##COLUMN_NANENULLABLEDECIMAL_DIGITSREMARKSCOLUMN_DEFFinally Through the type mapping (Field | Description |
---|---|
Data type | |
Column name | |
Whether non-null is allowed | |
The number of decimal places | |
Remarks | |
Default value |
) in JavaTypeResolver
and the camel case naming conversion (getCamelCaseString
) in StringUtils
) SQL information is converted into Java information.
This class combines data into a target code file through
FreeMarkertemplate engine. The main logic is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
In the test,
Class.class.getClass().getResource("/" ).getPath();This method can obtain the project root directory, but when testing the generated Jar package, this method is timely. Therefore, before generating the Jar package, you need to modify this line to new File("").getAbsolutePath();
Get the generation path. Project structure
Configuration file example
1 2 3 4 5 6 7 8 9 10 |
|
Running command example
1 |
|
The above is the detailed content of Detailed explanation of the design and implementation of Java Web template code generator. For more information, please follow other related articles on the PHP Chinese website!