MyBatis Plus 代码生成器配置

package com.jiuxiao;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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 org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 代码生成器
 * @Author: 悟道九霄
 * @Date: 2022年07月20日 15:58
 * @Version: 1.0.0
 */
public class CodeGenerator {

    /**
     * @param tip
     * @return: java.lang.String
     * @decription 读取控制台内容
     * @date 2022/7/20 16:05
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + " : ");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNoneBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

    public static void main(String[] args) {

        final String url = "jdbc:mysql://localhost:3306/jxc?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai";
        final String username = "root";
        final String password = "0531";
        final String driverName = "com.mysql.cj.jdbc.Driver";
        //模板引擎为 freemarker
        final String templatePath = "/templates/mapper.xml.ftl";

        AutoGenerator mpg = new AutoGenerator();

        //--------------- 全局配置 -----------------
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //设置输出目录
        gc.setOutputDir(projectPath + "/jxc-admin/src/main/java");
        //设置作者
        gc.setAuthor("悟道九霄");
        //打开输出目录
        gc.setOpen(false);
        //xml 开启 BaseResultMap
        gc.setBaseResultMap(true);
        //xml 开启 BaseColumnList
        gc.setBaseColumnList(true);
        //实体属性使用 Swagger2 注解
        gc.setSwagger2(true);

        //--------------- 数据源配置 -----------------
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);

        //--------------- 包配置 -----------------
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.jiuxiao")
                .setEntity("pojo")
                .setMapper("mapper")
                .setService("service")
                .setServiceImpl("service.impl")
                .setController("controller");

        //--------------- 自定义配置 -----------------
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        //自定义输出配置
        ArrayList<FileOutConfig> focList = new ArrayList<>();
        //自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //自定义输出文件名
                return projectPath + "/jxc-admin/src/main/resources/mapper/" + tableInfo.getEntityName()
                        + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);

        //--------------- 模板配置 -----------------
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);

        //--------------- 策略配置 -----------------
        StrategyConfig sc = new StrategyConfig();
        //数据库表映射到实体的命名策略
        sc.setNaming(NamingStrategy.underline_to_camel);
        //数据库表字段映射到实体的命名策略
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        //lombok 模型
        sc.setEntityLombokModel(true);
        //生成 @Controller 控制器
        sc.setRestControllerStyle(false);
        sc.setInclude(scanner("表名,使用多个英文逗号分隔").split(","));
        sc.setControllerMappingHyphenStyle(true);
        //数据库表前缀
        sc.setTablePrefix("t_");

        //--------------- 所有配置项汇总 -----------------
        mpg.setGlobalConfig(gc);
        mpg.setDataSource(dsc);
        mpg.setPackageInfo(pc);
        mpg.setCfg(cfg);
        mpg.setTemplate(tc);
        mpg.setStrategy(sc);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
posted @ 2022-07-20 16:47  悟道九霄  阅读(126)  评论(0编辑  收藏  举报