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

How to use HouTu to generate an open source library of Java classes according to MySQL

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

Share

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

This article mainly introduces "how to use HouTu to generate open source library of Java class according to MySQL". In daily operation, I believe many people have doubts about how to use HouTu to generate open source library of Java class according to MySQL. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to use HouTu to generate open source library of Java class according to MySQL". Next, please follow the editor to study!

HouTu

HouTu, an easy-to-use and powerful code generation tool based on MySQL. The name Houtu, also known as Houyu, comes from the worship of land and women in the matrilineal society. It holds yin and yang and nurtures all things, so it is called the mother of the earth. This project is named HouTu, meaning "education of all things". By customizing the Freemarker template, you can generate any structured code, such as DO, Mapper, XML, Service, Controller, SQL, Html and so on. As long as you can write the template, HouTu can deliver the finished code to you.

Characteristics

The principle is simple: HouTu is based on native MySQL statements, obtains information such as database lists, table details and column details, and generates Java code combined with Freemarker templates.

Customizable templates: HouTu integrates Freemarker templates, as long as the templates that follow Freemarker development can automatically generate the corresponding Java code.

Powerful: support single table generation, multi-table generation, theoretically support the generation of arbitrary structured code.

Quick start

Declare the entry class

Generator g = new Generator (Config.getInstance ())

.setDriver ("com.mysql.cj.jdbc.Driver")

.setUrl ("jdbc:mysql://127.0.0.1:3306/houtu?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&useLegacyDatetimeCode=false")

.setUsername ("root")

.setPassword ("root")

.setPrimaryKeyType ("Integer")

.setClassPrefix ("HouTu")

.setClearClassPrefix ("HouTu")

.setOutRootDir (System.getProperty ("user.dir") + File.separator + "generator-output"))

Generator.java provides only one constructor, which is passed into the system configuration class Config.java

Use API

Generator.java provides four interfaces:

Generator#deleteOutRootDir (): delete the files in the output directory, usually called before generate, to empty the old output files

Generator#printAllTableInfo (): print all table information

Generator#generateAll (): generates all tables into a java file

Generator#generate (String []): the java file is generated by specifying the table name. The input parameter is a variable parameter. If it is empty, the Generator#generateAll () method is called.

Specific use

/ / delete the output directory of the generator

G.deleteOutRootDir ()

/ / print all tables

G.printAllTableInfo ()

/ / generate a Java file for a single table

G.generate ("sys_user")

Output content

00 project 53 java 57.945 [main] INFO me.zhyd.houtu.Generator-[delete dir] D:\ project\ java\ HouTu\ generator-output

00RV 53 main 57.955 [main] INFO me.zhyd.houtu.Generator-All Table Names Begin

00RV 53 sys_resources 57.990 [main] INFO me.zhyd.houtu.Generator-InnoDB-"sys_resources"-utf8_general_ci

00RV 53 sys_role 57.992 [main] INFO me.zhyd.houtu.Generator-InnoDB-"sys_role"-utf8_general_ci

00RV 53 sys_role_resources 57.992 [main] INFO me.zhyd.houtu.Generator-InnoDB-"sys_role_resources"-utf8_general_ci

00RV 53 sys_user 57.992 [main] INFO me.zhyd.houtu.Generator-InnoDB-"sys_user"-utf8_general_ci

00RV 53 sys_user_role 57.992 [main] INFO me.zhyd.houtu.Generator-InnoDB-"sys_user_role"-utf8_general_ci

0015 / 53 / 57.992 [main] INFO me.zhyd.houtu.Generator-All Table Names END

00RV 53 57.992 [main] INFO me.zhyd.houtu.Generator

00RV 53 main 57.992 [main] INFO me.zhyd.houtu.Generator-- BEGIN generate by sys_user

00RV 53 57.992 [main] INFO me.zhyd.houtu.Generator

00RV 53 58.077 [main] INFO me.zhyd.houtu.Generator

00RV 53 main 58.078 [main] INFO me.zhyd.houtu.Generator-- BEGIN generate by sys_user

00RV 53 58.078 [main] INFO me.zhyd.houtu.Generator

00 project 53 HouTu 58.471 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ biz\ dto\ HouTuSysUserDto.java

00 project 53 HouTu 58.476 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ biz\ form\ HouTuSysUserForm.java

00 project 53 HouTu 58.478 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ biz\ service\ HouTuSysUserService.java

00 project 53 HouTu 58.479 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ biz\ service\ impl\ HouTuSysUserServiceImpl.java

00 project 53 HouTu 58.481 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ biz\ vo\ HouTuSysUserVO.java

00 project 53 HouTu 58.482 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ controller\ render\ RenderHouTuSysUserController.java

00 project 53 HouTu 58.485 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ controller\ RestHouTuSysUserController.java

00 project 53 HouTu 58.487 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ persistence\ beans\ HouTuSysUser.java

00 project 53 HouTu 58.490 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ persistence\ mapper\ HouTuSysUserMapper.java

00 project 53 HouTu 58.491 [main] INFO me.zhyd.houtu.Generator-D:\ project\ java\ HouTu\ generator-output\ persistence\\ xml\ HouTuSysUserMapper.xml

00RV 53 58.493 [main] INFO me.zhyd.houtu.core.GeneratorHelper-Program execution is complete, release resources.

Built-in template

Lightweight code generation tool. Generate according to the template, which theoretically supports the generation of arbitrary template code.

|-- biz

| |-- dto |

| | `--${table.className} Dto.java |

| |-- service |

|-- ${table.className} Service.java |

| | `--impl |

| | `--${table.className} ServiceImpl.java |

| | `--vo |

| | `--${table.className} ConditionVO.java |

|-- controller

| |-- Rest$ {table.className} Controller.java |

| | `--render |

| | `--Render$ {table.className} Controller.java |

|-- persistence

| |-- beans |

| | `--${table.className} .java |

| |-- mapper |

| | `--${table.className} Mapper.java |

| | `--xml |

| | `--${table.className} Mapper.xml |

|-- sql

|-- view

| | `--${table.className} |

|-- annotation.include

|-- java_copyright.include

`--macro.include

Custom template

Template configuration item

| | configuration item | Type | example | explanation | remarks | |:-| |:-| | classPrefix | String | Light | generated class prefix. Remove clearClassPrefix before adding classPrefix | clearClassPrefix | String | Light | Class prefix to be removed | Remove clearClassPrefix before adding classPrefix | | | driver | String | com.mysql.cj.jdbc.Driver | | url | String | jdbc:mysql://127.0.0.1:3306/light | | db | String | light | selected database (optional) | username | String | root | Database username | password | String | root | Database password | basePackage | String | me.zhyd.xx.core | Root package name | beansPackage | String | me.zhyd.light.core.db.persistence.beans | beans package name | | mapperPackage | String | me.zhyd.xx.core.db.persistence.mapper | mapper API package name | | outRootDir | String | D:\ project\ generator\ generator-output | text | Part output path | templatePath | String | template/ | template path | Based on the current project path | table | JSONObject | object | Table information | |

Table configuration item

| | configuration item | Type | example | explanation | remarks | |:-| |: | | type | String | BASE TABLE | data table type | db | String | light | Database name | | tableName | String | | Links | Datasheet name | coding | String | utf8mb4_general_ci | Datasheet Encoding | createTime | String | 2020-04-26 13:25:37 | Datasheet creation time | engine | String | InnoDB | Datasheet engine | remark | String | | className | String | LightLinks | Class name to be generated | classNameFirstLower | String | lightLinks | lowercase class name | | classNameFirstUpper | String | LightLinks | uppercase class name | | columns | JSONArray | array | column Information |

Table.columns configuration item

| | configuration item | Type | example | explanation | remarks |:-|: | | tableName | String | links | Table name | keyType | String | PRI | key type (PRI primary key) | | columnName | String | | Id | column name | columnNameFirstLower | String | id | column name with lowercase | columnNameFirstUpper | String | Id | column name with uppercase | notNull | Boolean | true | allowed to be empty | length | Integer | 0 | length | | scale | Integer | 0 | Precision | Available when it is a floating-point number | | | remark | String | | column comment | | | columnType | String | bigint | Database data type corresponding to the column | | | jdbcType | String | BIGINT | JDBC data type corresponding to the column | | | javaType | String | java.lang.String | Java data type corresponding to the column | | possibleShortJavaType | String | String | extra | String | auto_increment | additional parameters. Auto_increment table increases itself | | |

Functions to be completed

Support referencing environment variables: ${env.JAVA_HOME} or System.properties: ${user.home}

Custom template configuration path (currently based on the project root)

At this point, the study on "how to use HouTu to generate an open source library of Java classes based on MySQL" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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