springboot代码生成器基于mybatis-plus-generator

springboot mybatis-plus 代码生成。

添加pom

 		<dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.1.2version>
        dependency>
        <dependency>
            <groupId>org.freemarkergroupId>
            <artifactId>freemarkerartifactId>
            <version>2.3.28version>
        dependency>

新建代码生成配置

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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;

/**
 * @author fujie
 * @ 日期 2019-08-01 10:35
 */
public class Generator {
    /**
     * 数据源配置
     *
     * @return DataSourceConfig配置信息
     */
    private DataSourceConfig dataSourceConfig() {
        return new DataSourceConfig()
                .setUrl("jdbc:mysql://localhost:3306/web-admin?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true")
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUsername("root")
                .setPassword("root");
    }

    /**
     * 全局配置
     *
     * @return GlobalConfig
     */

    private GlobalConfig globalConfig() {
        String projectPath = System.getProperty("user.dir");
        return new GlobalConfig().setOutputDir(projectPath + "/src/main/java")
                .setAuthor("felix")
                //实体属性 Swagger2 注解
                .setSwagger2(true)
                .setOpen(false)
                //是否覆盖
                .setFileOverride(true)
                //是否kotlin
                .setKotlin(false)
                .setActiveRecord(false)
                .setBaseColumnList(true)
                .setBaseResultMap(true)
                .setServiceName("%sService");
        // .setEntityName("%s")
        //   .setMapperName("%sMapper")
        //  .setXmlName(null)
        //  .setServiceImplName("%sServiceImpl")
        // .setControllerName("%sController");
    }

    /**
     * 包名设置
     *
     * @param moduleName 模块名称 web
     */
    private PackageConfig packageConfig(String moduleName) {
        return new PackageConfig().setModuleName(moduleName).setParent("com.fecred.traffic.module");
    }

    /**
     * 自定义模板配置
     *
     * @return 模板配置
     */
    private TemplateConfig templateConfig() {
        return new TemplateConfig()
                .setEntity("templates/entity")
                .setXml(null)
                .setMapper("templates/mapper.java")
                .setService("templates/service")
                .setServiceImpl("templates/service.impl")
                .setController("templates/controller");
    }

    private StrategyConfig strategyConfig(String tablePrefix, String... include) {
        return new StrategyConfig().setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setTablePrefix(tablePrefix)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .setInclude(include)
                .setCapitalMode(true)
                .setControllerMappingHyphenStyle(true);
    }

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

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return System.getProperty("user.dir") + "/src/main/resources/mapper/" + moduleName
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    /**
     * @param moduleName  模块名称
     * @param tablePrefix 数据库表前缀
     * @param include     表,
     * @return
     */

    public AutoGenerator autoGenerator(String moduleName, String tablePrefix, String... include) {
        return new AutoGenerator()
                .setDataSource(dataSourceConfig())
                .setGlobalConfig(globalConfig())
                .setPackageInfo(packageConfig(moduleName))
                .setTemplate(templateConfig())
                .setStrategy(strategyConfig(tablePrefix, include))
                .setCfg(init(moduleName)).setTemplateEngine(new FreemarkerTemplateEngine());
    }

    public 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) {
        Generator genetator = new Generator();
        genetator.autoGenerator(genetator.scanner("模块名称"), genetator.scanner("表前缀"), genetator.scanner("表")).execute();
    }
}

自定义模板

你可能感兴趣的:(java)