2 Star 14 Fork 1

alansun2 / mapper-generator-javafx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

mybatis-friend

这是一个由 JavaFX, Spring, FreeMarker开发的小工具, 可以快速生成数据库对应的实体类Mybatis Mapper模板( Controller, Service, DTO等), 使您有更多的时间去摸鱼🤩。

如果在使用中发现问题或觉得有优化的地方, 欢迎大家提 issue, Thanks!😄

如果觉得还错的话, 欢迎大家点个 star, 👍!

1. 主要功能概览

  1. 可视化生成数据库相应的实体类, 不需要再写繁琐的配置文件
  2. 支持自定义模板, 快速生成重复代码, 例如: Controller, Service, DTO
  3. 支持包路径占位符替换
  4. 支持领域模型(Domain)的占位符替换
  5. 支持 java 文件合并, 再也不用担心文件被覆盖
  6. 支持外部 mybatis-generator 插件导入
  7. 记录您每一次的变动
    • 这里讲一下自身使用官方的 mybatis-generator 感受, 当我有多个数据源时, 刚开始我在数据源1工作, 生成OrderInfo , OrderInfoMapper, OrderInfoMapper.xml三个文件(忽略了一些字段, 去除了delete update sql), 接着我又在数据源2 工作, 此时我已经把数据源1的配置删除, 后来我对数据源1order_info表回复一些之前忽略的字段, 或者我要去除一个 count sql方法, 这时我又要重新配置数据源1, 并且要比对之前生成的文件, 很是麻烦。也许你会说, 使用多个配置文件来回切换就可以。 当然这也是一种方法。不过我相信当你用了mybatis-friend, 你应该会抛弃这种想法。

2. 版本日志

  • v4.0.0
    1. 重构代码, 优化UI
    2. 新增了模板功能
    3. 新增导入导出配置的功能
    4. 包名支持占位符替换
    5. 新增 java 文件 merge 功能, 暂不支持 kotlin 的 merge
    6. 新增领域包功能
    7. 新增外部 mybatis-generator 插件导入功能
    8. 删除了 tk.mybatis 的生成策略
  • v2.1.0
    1. 增加 tk.mybatis 的生成策略
    2. 增加 mybatis 生成策略(MyBatis3Simple, MyBatis3DynamicSql)
    3. 优化代码
  • v2.0.0
    1. 增加可选择的实现 Serializable 接口的功能
    2. 重构代码
    3. 优化搜索
    4. 修改一些bug
  • v1.1.0
    1. 增加表搜索功能, 左边列表区域使用键盘输入就可以使用

3. 使用

3.1. 添加数据源

  1. 右上角菜单栏点击文件 -> 添加数据源 add-datasource.jpg datasource-config.jpg

说明:

  1. 理论上可以支持所有数据库, 但经过本人测试的只有 mysql。如果您使用时发现有问题的话, 可以在 githubgitee 提 issue
  2. 新增时默认选择驱动类型为 mysql, 请自行修改配置, 驱动类型下拉框只是为了方便您的填写, 并不会影响配置

3.2. 选择需要导出的表

有两种方式:

  1. 点击数据源导出会导出数据源下所有表, 另外数据源的刷新只是对表重新加载, 并不会对字段重新加载。对字段的重新加载请看3.3. 刷新字段open-mybatis-config-datasource.jpg

  2. 点击表导出(可多选), 或者双击单表导出 open-mybatis-config-table.jpg

3.3. 对需要导出的表进行配置

table-detail.jpg column-refresh.jpg

说明:

  1. 表配置, 对想要导出的 sql 语句打上勾
  2. 表字段配置, 可以忽略导出字段, 指定导出的属性名(property), 对属性名的类型进行重写(java type), 对属性配置类型处理器(type handler)

3.4. 配置导出配置

export.jpg

说明:

  1. 这里配置基本都是基于 mybatis-generator 的配置, 配置详情请见官网
  2. 这里 Bean 包名, Mapper 包名, Xml 地址, 可以使用占位符, 具体请见 占位符

3.4.1. 菜单按钮说明:

  1. 添加额外属性: 可以添加额外的属性, 在 Bean 包名, Mapper 包名, Xml 地址 中使用 ${} 占位符来引用, 具体请见 占位符
  2. 保存配置: 当添加新配置后, 配置不会立刻保存至文件, 点击此按钮可以保存配置(另外点击导出也会立刻保存)
  3. 导出: 导出配置
  4. 下一步: 当需要使用模板来快速生成代码时, 可以点击此按钮进行进一步的配置

3.4.2. 高级配置

目前高级配置只支持 mybatis-generator 插件配置, 支持 .java 后缀文件和 jar 包导入。

插件配置 编辑插件

3.5. 配置额外模板(可选)

  1. 额外模板可以用来快速生成重复代码, 例如: Controller, Service, DTO 等。 如果您不需要该功能, 可以在上一步点击导出即可。 template1.jpg template2.jpg

3.5.1. 示例模板说明:

  1. colo test: 表示 cola 架构, 如果你的项目使用的是 cola 架构, 可以使用该模板
  2. usual test: 通用的分层架构

3.5.2. 按钮说明:

  1. 添加额外属性保存配置同上
  2. 导入额外模板: 从模板库中导入到当前配置中
  3. 导出: 导出配置(包括模板文件和 mybatis 文件)
  4. 返回: 返回上一步

3.5.3. 导入额外模板说明

模板是从模板库中导入的, 如何导入请见模板库

extra-template.jpg

3.5.4. 配置模板

template-config

配置说明:

  1. 文件输出地址: 这里只需要填写相对位置即可, 例如: src/main/java, XXXX/src/main/java, 前缀路径由上一步中的项目地址/项目名称/组成
  2. 包名: 例如: com.example.demo, 这里包名支持使用 ${} 占位符, 例如: com.example.{a}, 另外也支持DOMAIN, 具体请见 占位符

3.5.5. 配置模板库

  1. 新增 model 模板, model 模板表示 java bean 的模板, 例如: DTOextra-template-add-model.jpg

配置属性说明:

  1. 后缀: 会添加到数据表表名后面, 例如: order_info 表, 后缀为 Model, 则生成的 bean 名称为 OrderInfoModel
  2. Validate 注解: 表示生成 model 时会根据数据库字段的类型添加相应的注解, 例如: varchar 类型会添加 @Length 注解
  3. 忽略字段: 表示生成 model 时会忽略的字段, 例如: id 字段
  4. 父类: 表示生成 model 时 model 继承父类, 例如: BaseModel
  1. 新增自定义模板(CUSTOM TEMPLATE), 自定义模板表示类似 Service 层的模板, 例如: OrderService, OrderServiceImpl, 需要您手写 FreeMarker 文件并导入 extra-template-add-custom-template.jpg

自定义模板使用了 FreeMarker 模板引擎

配置属性说明:

  1. 后缀 父类 同上
  2. 模板文件: 表示模板文件的路径, 例如: D:/template/OrderService.java.ftl
3.5.5.1. 按钮说明:
  1. 保存配置: 保存当前配置, 如果添加后不保存直接关闭会丢失配置
  2. 导入: 导入勾选后的模板至当前配置

4. 配置存储目录

  • 当前用户下 /AppData/Local/MapperGeneratorV2/config
    • config 目录下主要存放的是数据源配置, 导出配置, 插件配置, 数据表和字段的缓存

如果工具发生错误, 可以试一下清空以上目录。如果还不行, 欢迎提 issue。😡


5. 日志文件位置

  • windows-exe 版本: 安装目录下 mybatis-friend.log
  • jar 包版本: jar的同级目录下 mybatis-friend.log

6. 如何自定义开发自己的功能?

该项目大概8000多行代码, 很简单的代码。但您得先有JavaFX, 的基础, 如果您不熟悉 JavaFX 这里给 B 站的可爱阿婆主《JavaFX没人看系列》 做一波广告😄。

6.1 构建

该项目使用 JavaPackager 打包, 使用 maven 构建, 使用 jdk17, 如果您想自己构建, 可以按照以下步骤:

  1. 执行 mvn clean package。目前 pom 中的配置只可以打包成 win, 如果要打包成其他平台, 请根据 JavaPackager 在 pom 中修改
  2. mapper-generator-javafx-core/target/mybatis-friend 目录下找到 mybatis-friend.exe , 双击即可运行

7. 下载

  • mybatis-friend-windows-exe.7z:可以直接运行在 windows
  • mybatis-friend-executable.jar:可执行 jar 包, 可以运行在 windows, linux, mac 等系统, 命令:java -jar mybatis-friend-executable.jar

本项目 github 源码地址:https://github.com/alansun2/mapper-generator-javafx

本项目 gitee 源码地址:https://gitee.com/alansc/mapper-generator-javafx

本项目 github 下载地址

本项目 gitee 下载地址 (不支持上传大于 100m 的附件, 所以只能上传 jar 版本)

8. 其他

8.1. 占位符

8.1.1. 哪些地方可以使用占位符?

占位符总体来说可以使用在两个地方:

  1. 配置, 具体请见如下图: place-holder-1 place-holder-2
  2. FreeMarker 模板(模板占位符), 具体请见如下图: template

8.1.2. 占位符格式

${placeHolder}

注意: 不要使用内置的占位符, 内置占位符表请见 内置占位符

8.1.3. 内置占位符

占位符 说明 类型 可使用的位置
DOMAIN 领域, 具体请见8.1.1. String 配置, 模板
DOMAIN_DESC 领域描述, 具体请见8.1.1. String 配置, 模板
PACKAGE 包名 String 模板
TYPE_NAME_UPPER_CAMEL 首字母大写驼峰格式的类名 String 模板
TYPE_NAME_LOWER_CAMEL 首字母小写驼峰格式的类名 String 模板
TYPE_NAME_LOWER_HYPHEN 中划线分割小写格式的类名 String 模板
CUR_DATE_TIME 当前时间 String 模板
FIELDS_UPPER_CAMELS 首字母大写的字段 list List <String> 模板
IGNORE_FIELDS_MAP 被忽略的字段 map Map<String, List<String>> 模板
author 作者, 来源 3.4.中的作者名称 String 模板

除此之外还包括首字母大写的驼峰格式的表名, 例如表名为 user_info, 则占位符为 UserInfo 以及加上模板后缀的占位符, 例如表名为 user_info, 则占位符为 UserInfoDO UserInfoDTO` 等等, 具体可以参考内置模板 AddCmdExe.ftl

8.2. DOMAIN 说明

8.2.1. 为什么要有 DOMAIN?

您可能会遇到这样的情况, 比如您的原始项目结构是这样的:

com
└── test
    └── dao

而您想要生成这样的项目结构:

com
└── test
    └── dao
        ├── order
        │   ├── Order.java
        │   └── OrderMapper.java
        └── user
            ├── User.java
            └── UserMapper.java

可以看到, dao 下面多了 orderuser 文件夹。原始的 mybatis-generator 是不支持一次性生成这样的项目结构的 (虽然您可以通过修改配置文件的方式一个个的生成, 但还是非常繁琐, 有时还会忘记), 所以这就是为什么要有 Domain 的原因。

8.2.2. DOMAIN 可以在哪些位置使用?

其实可以把 DOMAIN 当作普通的占位符, 所以能使用占位符的地方都可以使用 DOMAIN。具体请见占位符

8.2.3. 那么问题来了, DOMAIN 从哪里来呢?

DOMAIN 是从数据表中的备注获取的, 所以想要使用 DOMAIN, 备注需要符合一定的格式, 格式为 json, 具体如下:

{
  "d": "order",
  "dd": "订单"
}

说明:

  1. d: 领域, 使用位置请见8.2.2., 占位符名称为 DOMAIN
  2. dd: 领域描述, 使用位置为模板, 占位符名称为 DOMAIN_DESC
  3. 具体如何使用请见占位符

9. 未来开发规划

  1. 支持修改表结构和字段
  2. 支持插件属性配置

10. 请作者喝杯咖啡

如果您觉得本项目对您有帮助, 欢迎赞助作者喝杯咖啡。

       
alipaywechat
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

暂无描述 展开 收起
Java 等 3 种语言
Apache-2.0
取消

发行版 (8)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/alansc/mapper-generator-javafx.git
git@gitee.com:alansc/mapper-generator-javafx.git
alansc
mapper-generator-javafx
mapper-generator-javafx
master

搜索帮助