1 Star 0 Fork 107

weir / mybatis-plus-code-generator

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

MyBatis-Plus 代码生成器

Current Version

3.3.2

概述

代码生成器,又被叫做逆向工程,MyBatis官方为了推广,自己也写了一个,我之前也使用这个,功能也是非常强大,强大以为支持自定义配置,那么问题来了,我该怎么配置才合理呢,所以,有人把所有的配置项都弄成中文的,还有人开发了生成插件,这些在我以往的博文中都看看到。MyBatis-Plus的代码生成器到底怎么样,这我就不评判了,我就这样说,用用看吧。

功能列表:

  • 自动生成model类

  • 自动生成dao接口

  • 自动生成xml文件

  • 自动生成service接口

  • 自动生成service实现类

  • model支持Builder模式

  • 支持swagger2

  • 支持生成数据库字段常量

  • 支持生成Kotlin代码

  • ……more……

目录结构

.
├── README.md
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── fengwenyi
    │               └── code_generator
    │                   ├── Config.java                      // 自定义配置
    │                   ├── MySQL8CodeGenerator.java         // MySQL代码生成器
    │                   ├── OracleCodeGenerator.java         // Oracle代码生成器
    │                   └── util
    │                       └── CommonUtils.java
    └── test
        └── java
            └── com
                └── fengwenyi
                    └── code_generator
                        └── CodeGeneratorTests                // 代码生成器测试

支持的数据库

  • MySQL

  • Oracle

快速开始

你只需要三步,就能生成代码

第一步:Git克隆

git clone git@github.com:fengwenyi/mybatis-plus-code-generator.git

第二步:IDE导入

第三步:简单且必应的配置即可运行

MySQL:MySQL8CodeGenerator

Oracle:OracleCodeGenerator

简单且必要的配置

String dbUrl = "jdbc:mysql://localhost:3306/dbName";
String username = "dbUsername";
String password = "dbPassword";
String driver = "com.mysql.cj.jdbc.Driver";
// 表前缀,生成的实体类,不含前缀
String [] tablePrefixes = {};
// 表名,为空,生成所有的表
String [] tableNames = {};
// 字段前缀
String [] fieldPrefixes = {};
// 基础包名
String packageName = "com.example.module.db";

更多配置

package com.fengwenyi.code_generator;

/**
 * @author Erwin Feng
 * @since 2019-04-17 12:04
 */
public class Config {

    /** 包名:controller */
    public static final String PACKAGE_NAME_CONTROLLER = "controller";

    /** 包名:service */
    public static final String PACKAGE_NAME_SERVICE = "service";

    /** 包名:service.impl */
    public static final String PACKAGE_NAME_SERVICE_IMPL = "service.impl";

    /** 包名:model */
    public static final String PACKAGE_NAME_MODEL = "model";

    /** 包名:dao */
    public static final String PACKAGE_NAME_DAO = "dao";

    /** 包名:xml */
    public static final String PACKAGE_NAME_XML = "mapper";

    /** 文件名后缀:Model */
    public static final String FILE_NAME_MODEL = "%sModel";

    /** 文件名后缀:Dao */
    public static final String FILE_NAME_DAO = "%sDao";

    /** 文件名后缀:Mapper */
    public static final String FILE_NAME_XML = "%sMapper";

    /** MP开头,Service结尾 */
    public static final String FILE_NAME_SERVICE = "MP%sService";

    /** 文件名后缀:ServiceImpl */
    public static final String FILE_NAME_SERVICE_IMPL = "%sServiceImpl";

    /** 文件名后缀:Controller */
    public static final String FILE_NAME_CONTROLLER = "%sController";


    /** 作者 */
    public static final String AUTHOR = "Erwin Feng";

    /** 生成文件的输出目录 */
    public static String projectPath = System.getProperty("user.dir");

    /** 输出目录 */
//    public static final String outputDir = projectPath + "/src/main/java";
    public static final String outputDir = "/Users/code-generator";

}

注意事项

1、关于指定生成的表名

// 表名,为空,生成所有的表
String [] tableNames = {};

// 表名,具体表名
String [] tableNames = {"table"};

// 表名,多张表
String [] tableNames = {"table1", "table1"};

// 注意:生成多张表时,不能写成
String [] tableNames = {""};

2、包名

// 基础包名
// 这是基础包名,会在后面加dao/model/service等包名
String packageName = "com.example.module.db";

3、数据库操作服务接口默认以 MP 为前缀,如:MPUserService

4、报错 Error java: 错误;不支持发行版5

解决办法:

Java Compilermodule 对应的 java 版本改成 8+

MIT License Copyright (c) 2020 Erwin Feng Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

MyBatis Plus 代码生成器 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/weber/mybatis-plus-code-generator.git
git@gitee.com:weber/mybatis-plus-code-generator.git
weber
mybatis-plus-code-generator
mybatis-plus-code-generator
master

搜索帮助