Search in sources :

Example 21 with TableInfo

use of com.baomidou.mybatisplus.generator.config.po.TableInfo 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 22 with TableInfo

use of com.baomidou.mybatisplus.generator.config.po.TableInfo in project longmarch by yuyueqty.

the class CodeGenerator method getInjectionConfig.

/**
 * 自定义配置
 *
 * @return
 */
public InjectionConfig getInjectionConfig(PackageConfig 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/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + 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 23 with TableInfo

use of com.baomidou.mybatisplus.generator.config.po.TableInfo in project longmarch by yuyueqty.

the class GeneratorCodeEngine method buildInjectionConfig.

private InjectionConfig buildInjectionConfig(String tableName) {
    InjectionConfig injectionConfig = new InjectionConfig() {

        @Override
        public void initMap() {
        }
    };
    List<FileOutConfig> fileOutConfigList = new ArrayList<>();
    fileOutConfigList.add(new FileOutConfig("/generator/index.vue.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return String.format("%s/page/index.vue", projectPath);
        }
    });
    fileOutConfigList.add(new FileOutConfig("/generator/api.js.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return String.format("%s/page/%sApi.js", projectPath, tableInfo.getEntityName());
        }
    });
    fileOutConfigList.add(new FileOutConfig("/generator/lang_zh.js.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return String.format("%s/page/lang_zh.js", projectPath);
        }
    });
    fileOutConfigList.add(new FileOutConfig("/generator/lang_en.js.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return String.format("%s/page/lang_en.js", projectPath);
        }
    });
    fileOutConfigList.add(new FileOutConfig("/generator/longmarch.sql.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            return String.format("%s/sql/%s.sql", projectPath, tableName);
        }
    });
    injectionConfig.setFileOutConfigList(fileOutConfigList);
    return injectionConfig;
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo)

Example 24 with TableInfo

use of com.baomidou.mybatisplus.generator.config.po.TableInfo in project mall-learning by macrozheng.

the class MyBatisPlusGenerator method initInjectionConfig.

/**
 * 初始化自定义配置
 */
private static InjectionConfig initInjectionConfig(String projectPath, String moduleName) {
    // 自定义配置
    InjectionConfig injectionConfig = new InjectionConfig() {

        @Override
        public void initMap() {
        // 可用于自定义属性
        }
    };
    // 模板引擎是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/" + moduleName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    injectionConfig.setFileOutConfigList(focList);
    return injectionConfig;
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo)

Example 25 with TableInfo

use of com.baomidou.mybatisplus.generator.config.po.TableInfo 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)

Aggregations

TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)28 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)23 ArrayList (java.util.ArrayList)20 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)16 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)10 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)6 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)6 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)5 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)4 IOException (java.io.IOException)4 Map (java.util.Map)4 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)3 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)3 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)3 TableField (com.baomidou.mybatisplus.generator.config.po.TableField)3 File (java.io.File)3 FileUtil (cn.hutool.core.io.FileUtil)2 StrUtil (cn.hutool.core.util.StrUtil)2 FastAutoGenerator (com.baomidou.mybatisplus.generator.FastAutoGenerator)2 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)2