idea中代码生成插件及逆向工程

mabatis-plus的逆向工程

1、使用mybatis-plus的插件

1.1、mybatis-plus插件的安装

mybatis-plus插件下载:https://zhile.io/2019/04/23/mybatis-code-helper-pro-crack.html

首先下载好mybatis-plus的插件,然后选择从磁盘中进行安装插件。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UWf48t33-1585209646155)(assets/1580533656861.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xu3Ita2Y-1585209646156)(assets/1580533702170.png)]

1.2、连接数据源

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g7rKOLNg-1585209646158)(assets/1580534661986.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HfcPifrM-1585209646159)(assets/1580534833212.png)]

连接成功后,如图所示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w3pbfBpX-1585209646160)(assets/1580534857322.png)]

1.3、使用插件单个生成单个表

使用安装好的mybatis插件进行自动生成类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8IUZaK3S-1585209646161)(assets/1580534471847.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IqOcw720-1585209646163)(assets/1580534123137.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UllGNZEZ-1585209646164)(assets/1580534333402.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xgfOkfBF-1585209646165)(assets/1580534355518.png)]

生成完成,如图所示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wuqldLNG-1585209646168)(assets/1580534431936.png)]

1.4、使用插件生成多个表(建议使用)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b7sOp3Jn-1585209646168)(assets/1585019967241.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OhHo3iuL-1585209646169)(assets/1580534333402.png)]

2、mybatis-plus自动生成类

mybatis-plus官网可以找到
引入依赖

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.1.tmp</version>
    </dependency>
    <!--模板引擎-->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.29</version>
    </dependency>
    <!--mysql 的驱动,版本来自spring boot 里面-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

启动下面这个程序也会自动生成表

package com.zxm;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class GenApp {
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 把生成的代码放在当前的项目里面
        String projectPath = System.getProperty("user.dir");
        // 生成代码所在的具体目录
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("zhaoxiaomeng");
        // 代码生成后是否自动打开文件夹
        gc.setOpen(false);
        // 实体属性 Swagger2 注解
        gc.setSwagger2(true);
        // 去掉接口名称前面的字母I   %s 代表是实体的名称
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.94.225.69:3306/ego-shop?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.zxm");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        /*
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录");
                return false;
            }
        });
        */
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 设置实体类的命名规则
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 设置属性的命名规则
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // lombok配置
        strategy.setEntityLombokModel(true);
        // RestController
//        strategy.setRestControllerStyle(true);
//        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
发布了29 篇原创文章 · 获赞 0 · 访问量 2249

猜你喜欢

转载自blog.csdn.net/rootDream/article/details/105120896