你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】

 很多时候,会想手动创建controller、service及impl、mapper、entity等很繁琐,特别是新项目起始阶段,随着数据库的设计,然后业务接口、mapper接口、实体类等都要一一创建好,大家会不会有这样的烦恼,想着是否有能代替手动创建对应的类实体接口,岂不美哉。

于是我要送给大家的是,一个超级好用的代码生成工具类,支持手动定义某些表创建对应的控制器等而且还对生成的文件归类到自定义好的文件夹中,灵活轻巧,操作只需启动该类的main方法,就会自动生成对应的类接口实体,这你真值得拥有,不信你试试。

如果最后觉得生成器对你有所帮助,请不要吝啬你的赞,直接pia的点亮就完了啦,up up up!!!

现在我把它分享出来,如下就是全码,拿走!不谢!

package com.system.xiaoma.utils;


import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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;
import java.util.Scanner;

public class MyGenerator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    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("luoyong");
        gc.setOpen(false);
        gc.setFileOverride(true);// 是否覆盖同名文件,默认是false
        gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap 生成基本的resultmap
        gc.setBaseColumnList(true);// XML columList 生成基本的sql片段
        //实体属性 Swagger2 注解
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/fk_system?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName(scanner("模块名"));
        pc.setParent("com.system.xiaoma");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);

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

        // 自定义输出配置
        List focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" +".xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.system.xiaoma.base.BaseEntity");
        strategy.setEntityLombokModel(false);
        strategy.setRestControllerStyle(true);
        // 公共父类
        strategy.setSuperControllerClass("com.system.xiaoma.base.BaseController");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");

        TemplateConfig tc = new TemplateConfig();
            tc.setXml(null);
        mpg.setTemplate(tc);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

如下我给大家演示一下,确保所有小伙伴都能掌握:

1、启动main方法之后,在控制台会如下展示,按需输入要生成的数据库表名称,多表按英文逗号隔开;最后回车即可。

你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第1张图片

2、几秒就生成好了,最后一行会自动打印出文件生成完成!显示全部生成成功了。

 你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第2张图片

3、你刷新下项目,生成的文件夹及文件都在。

你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第3张图片

你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第4张图片

你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第5张图片

你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第6张图片

你一定没用过的代码生成工具,超级好用,快进你的收藏夹吃灰吧【附全码】_第7张图片


❤如果文章对您有所帮助,就在文章的右上角或者文章的末尾点个赞吧!(づ ̄ 3 ̄)づ 

❤如果喜欢大白兔分享的文章,就给大白兔点个关注吧!(๑′ᴗ‵๑)づ╭❤~

❤对文章有任何问题欢迎小伙伴们下方留言或者入群探讨【群号:708072830】

❤鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复讨论(请勿发表攻击言)

你可能感兴趣的:(工具类,代码生成类)