5 Star 26 Fork 16

yadong.zhang / HouTu

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MulanPSL-1.0

HouTu

HouTu,一款基于MySQL的简单易用又功能强大的代码生成工具。取名后土,后土又名後土,源于母系社会自然崇拜中的土地与女性崇拜。其掌阴阳,育万物,因此被称为大地之母。本项目取名HouTu,取义“育万物”之意,通过定制Freemarker模板,可以生成任意结构化的代码,如 DO、Mapper、XML、Service、Controller、SQL、Html等等,只要你能写出模板,HouTu就能交付给你成品代码。

star github star

特点

  1. 原理简单:HouTu以原生MySQL语句为基础,获取如数据库列表、表详情和列详情等信息,结合Freemarker模板生成Java代码。
  2. 可定制模板:HouTu集成Freemarker模板,只要是遵循Freemarker开发的模板都可以自动生成对应的Java代码。
  3. 功能强大:支持单表生成、多表生成,理论上支持生成任意结构化的代码。

快速开始

模板参考 doc/template

声明入口类

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只提供了一个构造函数,传入系统配置类Config.java

使用API

Generator.java对外提供了四个接口:

  • Generator#deleteOutRootDir(): 删除输出目录的文件,一般在generate之前调用,清空旧的输出文件
  • Generator#printAllTableInfo(): 打印所有表信息
  • Generator#generateAll(): 将所有表生成java文件
  • Generator#generate(String[]): 指定表名生成java文件,入参为可变参数,如果为空则调用Generator#generateAll()方法

具体使用

//删除生成器的输出目录
g.deleteOutRootDir();
// 打印所有表
g.printAllTableInfo();
// 生成单个表的Java文件
g.generate("sys_user");

输出内容

00:53:57.945 [main] INFO me.zhyd.houtu.Generator - [delete dir]    D:\project\java\HouTu\generator-output
00:53:57.955 [main] INFO me.zhyd.houtu.Generator - ----All Table Names Begin----
00:53:57.990 [main] INFO me.zhyd.houtu.Generator -   InnoDB - "sys_resources" - utf8_general_ci
00:53:57.992 [main] INFO me.zhyd.houtu.Generator -   InnoDB - "sys_role" - utf8_general_ci
00:53:57.992 [main] INFO me.zhyd.houtu.Generator -   InnoDB - "sys_role_resources" - utf8_general_ci
00:53:57.992 [main] INFO me.zhyd.houtu.Generator -   InnoDB - "sys_user" - utf8_general_ci
00:53:57.992 [main] INFO me.zhyd.houtu.Generator -   InnoDB - "sys_user_role" - utf8_general_ci
00:53:57.992 [main] INFO me.zhyd.houtu.Generator - ----All Table Names END----
00:53:57.992 [main] INFO me.zhyd.houtu.Generator - ---------------------------------------------------------------
00:53:57.992 [main] INFO me.zhyd.houtu.Generator - - BEGIN generate by sys_user
00:53:57.992 [main] INFO me.zhyd.houtu.Generator - ---------------------------------------------------------------
00:53:58.077 [main] INFO me.zhyd.houtu.Generator - ---------------------------------------------------------------
00:53:58.078 [main] INFO me.zhyd.houtu.Generator - - BEGIN generate by sys_user
00:53:58.078 [main] INFO me.zhyd.houtu.Generator - ---------------------------------------------------------------
00:53:58.471 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\biz\dto\HouTuSysUserDto.java
00:53:58.476 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\biz\form\HouTuSysUserForm.java
00:53:58.478 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\biz\service\HouTuSysUserService.java
00:53:58.479 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\biz\service\impl\HouTuSysUserServiceImpl.java
00:53:58.481 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\biz\vo\HouTuSysUserVO.java
00:53:58.482 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\controller\render\RenderHouTuSysUserController.java
00:53:58.485 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\controller\RestHouTuSysUserController.java
00:53:58.487 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\persistence\beans\HouTuSysUser.java
00:53:58.490 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\persistence\mapper\HouTuSysUserMapper.java
00:53:58.491 [main] INFO me.zhyd.houtu.Generator - D:\project\java\HouTu\generator-output\persistence\xml\HouTuSysUserMapper.xml
00:53:58.493 [main] INFO me.zhyd.houtu.core.GeneratorHelper - 程序执行完毕,释放资源...

内置模板

轻量级的代码生成工具。根据模板进行生成,理论上支持任意模板代码的生成。

|-- 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

定制模板

模板配置项

| 配置项 | 类型 | 示例 | 解释 | 备注 |
|:---------------------------|:---------|:---------|:---------||:---------|
| classPrefix | String | Light | 生成的类前缀,先去除 clearClassPrefix 再添加 classPrefix | |
| clearClassPrefix | String | Light | 需要去除的类前缀,先去除 clearClassPrefix 再添加 classPrefix | |
| driver | String | com.mysql.cj.jdbc.Driver | | |
| url | String | jdbc:mysql://127.0.0.1:3306/light | | |
| db | String | light | 选择的数据库,非必填,如果为空会从url中解析 | |
| username | String | root | 数据库用户名 | |
| password | String | root | 数据库密码 | |
| basePackage | String | me.zhyd.xx.core | 根包名 | |
| beansPackage | String | me.zhyd.light.core.db.persistence.beans | beans包名 | |
| mapperPackage | String | me.zhyd.xx.core.db.persistence.mapper | mapper接口包名 | |
| outRootDir | String | D:\project\generator\generator-output | 文件输出路径 | |
| templatePath | String | template/ | 模板路径,基于当前项目路径 | |
| table | JSONObject | object | 表信息 | |

table 配置项

| 配置项 | 类型 | 示例 | 解释 | 备注 |
|:---------------------------|:---------|:---------|:---------||:---------|
| type | String | BASE TABLE | 数据表类型 | |
| db | String | light | 数据库名 | |
| tableName | String | links | 数据表名 | |
| coding | String | utf8mb4_general_ci | 数据表编码 | |
| createTime | String | 2020-04-26 13:25:37 | 数据表创建时间 | |
| engine | String | InnoDB | 数据表引擎 | |
| remark | String | | 数据表注释 | |
| className | String | LightLinks | 待生成的类名 | |
| classNameFirstLower | String | lightLinks | 首字母小写的类名 | |
| classNameFirstUpper | String | LightLinks | 首字母大写的类名 | |
| columns | JSONArray | array | 列信息 | |

table.columns 配置项

| 配置项 | 类型 | 示例 | 解释 | 备注 |
|:---------------------------|:---------|:---------|:---------||:---------|
| tableName | String | links | 表名 | |
| keyType | String | PRI | 键类型(PRI 主键) | |
| columnName | String | id | 列名 | |
| columnNameFirstLower | String | id | 列名首字母小写 | |
| columnNameFirstUpper | String | Id | 列名首字母大写 | |
| notNull | Boolean | true | 是否允许为空 | |
| length | Integer | 0 | 长度 | |
| scale | Integer | 0 | 精度,当为浮点数字类型时可用 | |
| remark | String | | 列注释 | |
| columnType | String | bigint | 列对应的数据库数据类型 | |
| jdbcType | String | BIGINT | 列对应的JDBC数据类型 | |
| javaType | String | java.lang.String | 列对应的Java数据类型 | |
| possibleShortJavaType | String | String | | |
| extra | String | auto_increment | 额外参数,auto_increment表自增 | |

模板可用完整参数示例 JSON

{
	"primaryKeyType": "String",
	"classPrefix": "",
	"basePackage": "com.zyd.blog",
	"mapperPackage": "com.zyd.blog.persistence.mapper",
	"templatePath": "template/",
	"url": "jdbc:mysql://127.0.0.1:3306/dblog?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&useLegacyDatetimeCode=false",
	"clearClassPrefix": "",
	"password": "root",
	"driver": "com.mysql.cj.jdbc.Driver",
	"outRootDir": "D:\\project\\java\\gitee\\OneBlog\\blog-codegen\\generator-output",
	"db": "dblog",
	"table": {
		"className": "BizAd",
		"classNameFirstLower": "bizAd",
		"classNameFirstUpper": "BizAd",
		"coding": "utf8_general_ci",
		"columns": [{
			"columnName": "id",
			"columnNameFirstLower": "id",
			"columnNameFirstUpper": "Id",
			"columnType": "bigint",
			"extra": "auto_increment",
			"javaType": "java.lang.Long",
			"jdbcType": "BIGINT",
			"keyType": "PRI",
			"length": 0,
			"notNull": true,
			"possibleShortJavaType": "Long",
			"remark": "",
			"tableName": "biz_ad"
		}, {
			"columnName": "content",
			"columnNameFirstLower": "content",
			"columnNameFirstUpper": "Content",
			"columnType": "varchar",
			"extra": "",
			"javaType": "java.lang.String",
			"jdbcType": "VARCHAR",
			"keyType": "",
			"length": 300,
			"notNull": false,
			"possibleShortJavaType": "String",
			"remark": "广告内容(简介)",
			"tableName": "biz_ad"
		}],
		"createTime": "2021-10-27 16:56:13",
		"db": "dblog",
		"engine": "InnoDB",
		"remark": "广告",
		"tableName": "biz_ad",
		"type": "BASE TABLE"
	},
	"beansPackage": "com.zyd.blog.persistence.beans",
	"username": "root"
}

待完成的功能

  • 支持引用环境变量: ${env.JAVA_HOME} or System.properties: ${user.home}
  • 自定义模板配置路径(当前是基于项目根目录的)
  • 支持其他数据库

开源推荐

关于 HouTu

其实本项目是重构于我15年开源的一款代码生成工具:https://github.com/zhangyd-c/generation,当时还是在用的SpringMVC。开源后就一直没怎么维护,最近要开新项目,正好抽时间就重写了一遍。

当时写了两篇关于实现generation原理的文章:

参与贡献

  1. fork本项目到自己的repo
  2. 把fork过去的项目也就是你仓库中的项目clone到你的本地
  3. 修改代码
  4. commit后push到自己的库
  5. 发起PR(pull request) 请求,提交到dev分支
  6. 等待作者合并

关注&交流

公众号 微信(备注:HouTu)

请喝咖啡

支付宝 微信
木兰宽松许可证, 第1版 木兰宽松许可证, 第1版 2019年8月 http://license.coscl.org.cn/MulanPSL 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第1版(“本许可证”)的如下条款的约束: 0. 定义 “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 “法人实体”是指提交贡献的机构及其“关联实体”。 “关联实体”是指,对“本许可证”下的一方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 1. 授予版权许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 2. 授予专利许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括仅因您或他人修改“贡献”或其他结合而将必然会侵犯到的专利权利要求。如您或您的“关联实体”直接或间接地(包括通过代理、专利被许可人或受让人),就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 3. 无商标许可 “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 4. 分发限制 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 5. 免责声明与责任限制 “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 条款结束。 如何将木兰宽松许可证,第1版,应用到您的软件 如果您希望将木兰宽松许可证,第1版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; 3, 请将如下声明文本放入每个源文件的头部注释中。 Copyright (c) [2019] [name of copyright holder] [Software Name] is licensed under the Mulan PSL v1. You can use this software according to the terms and conditions of the Mulan PSL v1. You may obtain a copy of Mulan PSL v1 at: http://license.coscl.org.cn/MulanPSL THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v1 for more details. Mulan Permissive Software License,Version 1 Mulan Permissive Software License,Version 1 (Mulan PSL v1) August 2019 http://license.coscl.org.cn/MulanPSL Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v1 (this License) with following terms and conditions: 0. Definition Software means the program and related documents which are comprised of those Contribution and licensed under this License. Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. Legal Entity means the entity making a Contribution and all its Affiliates. Affiliates means entities that control, or are controlled by, or are under common control with a party to this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. Contribution means the copyrightable work licensed by a particular Contributor under this License. 1. Grant of Copyright License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. 2. Grant of Patent License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed, excluding of any patent claims solely be infringed by your or others’ modification or other combinations. If you or your Affiliates directly or indirectly (including through an agent, patent licensee or assignee), institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. 3. No Trademark License No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in section 4. 4. Distribution Restriction You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. 5. Disclaimer of Warranty and Limitation of Liability The Software and Contribution in it are provided without warranties of any kind, either express or implied. In no event shall any Contributor or copyright holder be liable to you for any damages, including, but not limited to any direct, or indirect, special or consequential damages arising from your use or inability to use the Software or the Contribution in it, no matter how it’s caused or based on which legal theory, even if advised of the possibility of such damages. End of the Terms and Conditions How to apply the Mulan Permissive Software License,Version 1 (Mulan PSL v1) to your software To apply the Mulan PSL v1 to your work, for easy identification by recipients, you are suggested to complete following three steps: i. Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; ii. Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; iii. Attach the statement to the appropriate annotated syntax at the beginning of each source file. Copyright (c) [2019] [name of copyright holder] [Software Name] is licensed under the Mulan PSL v1. You can use this software according to the terms and conditions of the Mulan PSL v1. You may obtain a copy of Mulan PSL v1 at: http://license.coscl.org.cn/MulanPSL THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v1 for more details.

About

HouTu,一款基于MySQL简单易用的代码生成工具,取名后土。后土又名後土,源于母系社会自然崇拜中的土地与女性崇拜。其掌阴阳,育万物,因此被称为大地之母。本项目取名HouTu,取义“育万物”之意,通过定制Freemarker模板,可能生成任意结构化的代码,如 DO、Mapper、XML、Service、Controller、SQL、Html等等,只要你能写出模板,HouTu就能交付给你成品代码。 expand collapse
Java
MulanPSL-1.0
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/yadong.zhang/HouTu.git
git@gitee.com:yadong.zhang/HouTu.git
yadong.zhang
HouTu
HouTu
master

Search

344bd9b3 5694891 D2dac590 5694891