Search in sources :

Example 1 with InjectionConfig

use of com.baomidou.mybatisplus.generator.InjectionConfig in project java-example by saxingz.

the class GenCode method gen.

// /**
// * <p>
// * 读取控制台内容
// * </p>
// */
// 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.isNotBlank(ipt)) {
// return ipt;
// }
// }
// throw new MybatisPlusException("请输入正确的" + tip + "!");
// }
@Test
public void gen() {
    String[] tables = new String[] { "channel" };
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("saxing");
    gc.setOpen(false);
    gc.setFileOverride(false);
    gc.setIdType(IdType.AUTO);
    // 实体属性 Swagger2 注解
    gc.setSwagger2(true);
    gc.setDateType(DateType.ONLY_DATE);
    // gc.setEntityName("%sDO");
    // gc.setXmlName("%sMapper");
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(jdbcUrl);
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername(username);
    dsc.setPassword(password);
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName("");
    pc.setParent("org.saxing.a0041_wemedia");
    pc.setEntity("domain.entity");
    pc.setService("logic");
    pc.setServiceImpl("logic.impl");
    pc.setController("controller");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            // to do nothing
            this.getConfig().getTableInfoList().forEach(tableInfo -> {
                String entityName = tableInfo.getEntityName();
                if (!entityName.endsWith("DO")) {
                    // 实体类和表名不一致
                    tableInfo.setConvert(true);
                    tableInfo.setEntityName(entityName + "DO");
                    tableInfo.setServiceName("I" + entityName + "Logic");
                    tableInfo.setServiceImplName(entityName + "Logic");
                }
            });
        }
    };
    // 自定义输出配置
    // 如果模板引擎是 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;
            String entityName = tableInfo.getEntityName();
            entityName = entityName.endsWith("DO") ? entityName.substring(0, entityName.length() - 2) : entityName;
            return projectPath + "/src/main/resources/mapper/" + /*+ pc.getModuleName()*/
            "/" + entityName + "Mapper" + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // 此处设置为null,就不会再java下创建xml的文件夹了
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    strategy.setLogicDeleteFieldName("is_deleted");
    // 用数据库自带的时间更新
    // TableFill createdTime = new TableFill("created_time", FieldFill.INSERT);
    // TableFill updatedTime = new TableFill("updated_time", FieldFill.INSERT_UPDATE);
    // strategy.setTableFillList(Arrays.asList(createdTime, updatedTime));
    // 写于父类中的公共字段
    strategy.setInclude(tables);
    strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.execute();
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with InjectionConfig

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

the class CodeGeneratorUtils method getInjectionConfig.

/**
 * InjectionConfig配置
 * @param codeGeneratorHelper
 * @return
 */
private static InjectionConfig getInjectionConfig(PackageConfig pc, CodeGeneratorHelper codeGeneratorHelper, String projectPath) {
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    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/" + codeGeneratorHelper.getMapperXmlLoction() + "/" + pc.getModuleName().toLowerCase() + "/" + tableInfo.getEntityName() + codeGeneratorHelper.getMapperNameSuffix() + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    return cfg;
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo)

Example 3 with InjectionConfig

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

the class CodeGenerator method main.

public static void main(String[] args) throws Exception {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String basePath = CodeGenerator.class.getResource("").getPath();
    String projectPath = basePath.substring(0, basePath.indexOf("/target"));
    // String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("lyb-geek");
    gc.setOpen(false);
    gc.setBaseColumnList(true);
    gc.setBaseResultMap(true);
    gc.setServiceName("%sService");
    // gc.setSwagger2(true);// 实体属性 Swagger2 注解
    gc.setDateType(DateType.ONLY_DATE);
    mpg.setGlobalConfig(gc);
    String url = YmlUtil.getValue("spring.datasource.druid.url").toString();
    String username = YmlUtil.getValue("spring.datasource.druid.username").toString();
    String pwd = PropertiesUtil.INSTANCE.getProperty("password");
    String publicKey = PropertiesUtil.INSTANCE.getProperty("config.decrypt.key");
    String password = ConfigTools.decrypt(publicKey, pwd);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(url);
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername(username);
    dsc.setPassword(password);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.github.lybgeek.orm");
    pc.setEntity("model");
    pc.setMapper("dao");
    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/mapperPlus/" + pc.getModuleName()
    // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    // }
    // });
    focList.add(new FileOutConfig(templatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return projectPath + "/src/main/resources/mapper/mybatisplus/" + tableInfo.getEntityName().toLowerCase() + "/" + 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);
    strategy.setSuperEntityClass("com.github.lybgeek.orm.common.model.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // strategy.setSuperControllerClass("com.github.lybgeek.orm.controller.BaseController");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setSuperEntityColumns("id", "create_date", "update_date");
    strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    // 移除表的前缀
    // strategy.setTablePrefix("t_");
    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 4 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)

Example 5 with InjectionConfig

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

the class CodeGeneratorTest method initInjectionConfig.

private InjectionConfig initInjectionConfig() {
    String prefix = "/templates/freemarker/";
    InjectionConfig config = new InjectionConfig() {

        @Override
        public void initMap() {
            setMap(new HashMap<String, Object>() {

                {
                    // 注入自定义属性
                    put("key", "value");
                }
            });
        }
    };
    // config.setFileOutConfigList(Arrays.asList(xmlFileOutConfig));
    return config;
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig)

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