Search in sources :

Example 21 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project waynboot-mall by wayn111.

the class Gen method main.

public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/waynboot-common/src/main/java");
    gc.setAuthor("wayn");
    gc.setBaseColumnList(true);
    gc.setBaseResultMap(true);
    gc.setOpen(false);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://192.168.31.49:3306/wayn_shop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("admin123");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setService("service.tool");
    pc.setServiceImpl("service.tool.impl");
    pc.setMapper("mapper.tool");
    pc.setEntity("domain.tool");
    pc.setModuleName("");
    pc.setParent("com.wayn.common.core");
    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 + "/waynboot-common/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("调用默认方法创建的目录,自定义目录用");
                if (fileType == FileType.MAPPER) {
                    // 已经生成 mapper 文件判断存在,不想重新生成返回 false
                    return !new File(filePath).exists();
                }
                // 允许生成模板文件
                return true;
            }
        });*/
    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);
    // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    strategy.setSuperControllerClass("com.wayn.common.base.BaseController");
    // 写于父类中的公共字段
    strategy.setSuperEntityColumns("id");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 22 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project project by Ahaochan.

the class CodeGeneratorTest method generator.

@Test
public void generator() {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // show table status WHERE 1=1 AND NAME IN ('cl_transfer_queue_fail')
    String[] tableName = { "user" };
    // 1. 数据源配置 https://mp.baomidou.com/config/generator-config.html#%E6%95%B0%E6%8D%AE%E6%BA%90-datasourceconfig-%E9%85%8D%E7%BD%AE
    DataSourceConfig dataSourceConfig = initDataSourceConfig(properties);
    mpg.setDataSource(dataSourceConfig);
    // 2. 数据库表配置 https://mp.baomidou.com/config/generator-config.html#%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8%E9%85%8D%E7%BD%AE
    StrategyConfig strategyConfig = initStrategyConfig(tableName);
    mpg.setStrategy(strategyConfig);
    // 3. 包名配置 https://mp.baomidou.com/config/generator-config.html#%E5%8C%85%E5%90%8D%E9%85%8D%E7%BD%AE
    PackageConfig packageConfig = initPackageConfig("moe.ahao.spring.boot.mybatis.plus", "module");
    mpg.setPackageInfo(packageConfig);
    // 4. 模板配置 https://mp.baomidou.com/config/generator-config.html#%E6%A8%A1%E6%9D%BF%E9%85%8D%E7%BD%AE
    TemplateConfig templateConfig = initTemplateConfig();
    mpg.setTemplate(templateConfig);
    // 5. 全局配置 https://mp.baomidou.com/config/generator-config.html#%E5%85%A8%E5%B1%80%E7%AD%96%E7%95%A5-globalconfig-%E9%85%8D%E7%BD%AE
    GlobalConfig globalConfig = initGlobalConfig();
    mpg.setGlobalConfig(globalConfig);
    // 6. 注入配置 https://mp.baomidou.com/config/generator-config.html#%E6%B3%A8%E5%85%A5-injectionconfig-%E9%85%8D%E7%BD%AE
    InjectionConfig injectionConfig = initInjectionConfig();
    mpg.setCfg(injectionConfig);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project mybatis-plus-plugin by kana112233.

the class MybatisTests method doGen.

public void doGen() {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("jobob");
    gc.setOpen(false);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/mysql?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123456");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(null);
    pc.setMapper("dao");
    pc.setParent("com.baomidou.ant");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    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);
    strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
    // 写于父类中的公共字段
    strategy.setSuperEntityColumns("id");
    strategy.setInclude("user");
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 24 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project spring-boot-demo by liuyueyi.

the class CodeGenerator method main.

public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir") + "/spring-boot/106-mybatis-plus-generator";
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("YiHui");
    gc.setOpen(false);
    // 覆盖写
    gc.setFileOverride(true);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // 不额外指定模块,如果指定为 test,则生成的xml会在 mapper/test/ 目录下
    pc.setModuleName("");
    pc.setParent("com.git.hui.boot.mybatis.plus");
    mpg.setPackageInfo(pc);
    // 自定义配置
    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) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setController(null);
    // templateConfig.setEntityKt(null);
    // templateConfig.setService(null);
    // templateConfig.setServiceImpl(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // 所有实体类继承自 BasePo, 且id在父类中
    // strategy.setSuperEntityClass(BasePo.class);
    // strategy.setSuperEntityColumns("id");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
    // 设置需要生成的表名
    strategy.setInclude("userT0", "story_t0");
    // strategy.setInclude("story");
    strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 25 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project springboot-learning by lyb-geek.

the class CodeGeneratorUtils method execute.

/**
 * 代码生成逻辑
 * @param codeGeneratorHelper
 * @throws Exception
 */
public static void execute(CodeGeneratorHelper codeGeneratorHelper) throws Exception {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 项目路径
    String projectPath = codeGeneratorHelper.getProjectPath();
    if (StringUtils.isBlank(projectPath)) {
        projectPath = getProjectPath();
        System.out.println("-----------------项目根路径未配置将使用默认配置路径--->" + projectPath + "----------------");
    }
    // 全局配置
    GlobalConfig gc = getGlobalConfig(codeGeneratorHelper, projectPath);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = getDataSourceConfig(codeGeneratorHelper);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = getPackageConfig(codeGeneratorHelper);
    mpg.setPackageInfo(pc);
    InjectionConfig cfg = getInjectionConfig(pc, codeGeneratorHelper, projectPath);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = getStrategyConfig(codeGeneratorHelper);
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Aggregations

InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)33 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)22 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)22 ArrayList (java.util.ArrayList)20 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)13 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)6 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)4 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)4 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)4 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)4 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)4 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)4 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 MySqlTypeConvert (com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert)2 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)2 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 TableField (com.baomidou.mybatisplus.generator.config.po.TableField)1 MyFreemarkerTemplateEngine (com.baomidou.plugin.idea.mybatisx.codegenerator.MyFreemarkerTemplateEngine)1