Search in sources :

Example 6 with FreemarkerTemplateEngine

use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project spring-cloud-framework by zhuwj921.

the class CodeAutoGeneratorUtil method generator.

public static void generator(String parentName, String tableName, String next) {
    String projectPath = System.getProperty(SystemUtil.USER_DIR);
    String moduleName = SERVICE + parentName;
    String outputDir = projectPath + "/" + next + moduleName + "/src/main/java/";
    Console.log(outputDir);
    FastAutoGenerator.create(URL, USERNAME, PASSWORD).globalConfig(builder -> {
        // 设置作者
        builder.author("zhuwj").enableSwagger().disableOpenDir().outputDir(// 指定输出目录
        outputDir);
    }).packageConfig(builder -> {
        // 设置父包名
        builder.parent("com.cloud").moduleName(// 设置父包模块名
        parentName).pathInfo(// 设置mapperXml生成路径
        Collections.singletonMap(OutputFile.xml, projectPath + "/" + next + moduleName + "/src/main/resources/mapper/"));
    }).strategyConfig(builder -> {
        // 设置需要生成的表名
        builder.addInclude(tableName).entityBuilder().superClass("com.cloud.common.base.BaseEntity").versionColumnName("version").logicDeleteColumnName("is_deleted").logicDeletePropertyName("deleted").enableTableFieldAnnotation().fileOverride().enableLombok().disableSerialVersionUID().enableRemoveIsPrefix().disableSerialVersionUID().addSuperEntityColumns(new String[] { "id", "create_by", "create_time", "is_deleted", "modified_by", "modified_time", "version" }).serviceBuilder().fileOverride().controllerBuilder().enableRestStyle().fileOverride().mapperBuilder().enableBaseResultMap().enableBaseColumnList().fileOverride();
    }).templateEngine(// 使用Freemarker引擎模板,默认的是Velocity引擎模板
    new FreemarkerTemplateEngine()).templateConfig(builder -> builder.controller("")).execute();
}
Also used : OutputFile(com.baomidou.mybatisplus.generator.config.OutputFile) FastAutoGenerator(com.baomidou.mybatisplus.generator.FastAutoGenerator) Console(cn.hutool.core.lang.Console) SystemUtil(cn.hutool.system.SystemUtil) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) Collections(java.util.Collections) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)

Example 7 with FreemarkerTemplateEngine

use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project demo-parent by yindanqing925.

the class MysqlGenerator method main.

/**
 * RUN THIS
 */
public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir") + "/mybatisplus-demo";
    gc.setOutputDir(projectPath + "/src/main/java");
    // TODO 设置用户名
    gc.setAuthor("yindanqing");
    gc.setOpen(false);
    // service 命名方式
    gc.setServiceName("%sService");
    // service impl 命名方式
    gc.setServiceImplName("%sServiceImpl");
    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    gc.setMapperName("%sMapper");
    gc.setXmlName("%sMapper");
    gc.setFileOverride(true);
    gc.setActiveRecord(true);
    // XML 二级缓存
    gc.setEnableCache(false);
    // XML ResultMap
    gc.setBaseResultMap(true);
    // XML columList
    gc.setBaseColumnList(false);
    mpg.setGlobalConfig(gc);
    // TODO 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://cdb-1v2wt3os.bj.tencentcdb.com:10243/nh?useUnicode=true&characterEncoding=UTF8");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("user_dev");
    dsc.setPassword("user_dev_nh");
    mpg.setDataSource(dsc);
    // TODO 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("org.nh.mybatisplus.dict");
    pc.setEntity("domain");
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    mpg.setPackageInfo(pc);
    // 自定义需要填充的字段
    List<TableFill> tableFillList = new ArrayList<>();
    // 如 每张表都有一个创建时间、修改时间
    // 而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改
    // 修改时,修改时间会修改,
    // 虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了,
    // 使用公共字段填充功能,就可以实现,自动按场景更新了。
    // 如下是配置
    // TableFill createField = new TableFill("gmt_create", FieldFill.INSERT);
    // TableFill modifiedField = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
    // tableFillList.add(createField);
    // tableFillList.add(modifiedField);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    List<FileOutConfig> focList = new ArrayList<>();
    focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输入文件名称
            return projectPath + "/src/main/java/org/nh/mybatisplus/dict/mapper/" + tableInfo.getEntityName() + "Mapper" + ".xml";
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    mpg.setTemplate(new TemplateConfig().setXml(null));
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true);
    // 设置逻辑删除键
    strategy.setLogicDeleteFieldName("deleted");
    // TODO 指定生成的bean的数据库表名
    strategy.setInclude("transfer_dict");
    // strategy.setSuperEntityColumns("id");
    // 驼峰转连字符
    strategy.setControllerMappingHyphenStyle(true);
    mpg.setStrategy(strategy);
    // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) ArrayList(java.util.ArrayList) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) FileOutConfig(com.baomidou.mybatisplus.generator.config.FileOutConfig) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 8 with FreemarkerTemplateEngine

use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project dynamic_dataSource by tianliuzhen.

the class CodeGenerator method main.

public static void main(String[] args) {
    String pathStr = "/mybatis-plus";
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // 项目的相对路径
    String projectPath = System.getProperty("user.dir") + pathStr;
    System.out.println("项目的相对路径" + projectPath);
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("Mr.tian");
    gc.setOpen(false);
    // gc.setSwagger2(true); //实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://47.98.253.2:3306/master?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // 指定数据库
    dsc.setSchemaName("test1");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("Tlz19970905");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // TODO:  会新加一层路径
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.aaa.mybatisplus");
    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";
    // 自定义输出配置  xml 配置文件
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    // TODO:  生成 xml 文件
    focList.add(new FileOutConfig(templatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return // + pc.getModuleName()
            projectPath + "/src/main/resources/mapper/" + "/" + 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();
    // 这里会在 mapper 目录下默认生成 xml 目录及文件
    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.aaa.mybatisplus.common.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    strategy.setSuperControllerClass("com.aaa.mybatisplus.common.BaseController");
    // 写于父类中的公共字段
    strategy.setSuperEntityColumns("id");
    // TODO:要设置生成哪些表 如果不设置就是生成所有的表
    // 2019/12/21
    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 9 with FreemarkerTemplateEngine

use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine in project c4p by xseuen.

the class CodeGenerator method codeGenerator.

/**
 * 代码生成
 *
 * @see OutputFile
 */
@Test
public void codeGenerator() {
    String projectPath = System.getProperty("user.dir");
    String javaPath = projectPackage.replaceAll("\\.", "/");
    // 设置自定义路径
    Map<OutputFile, String> pathInfo = new HashMap<>();
    pathInfo.put(OutputFile.mapperXml, projectPath + "/src/main/resources/mybatis/mapper/");
    pathInfo.put(OutputFile.entity, projectPath + "/src/main/java/" + javaPath + "/entity/");
    pathInfo.put(OutputFile.controller, projectPath + "/src/main/java/" + javaPath + "/controller/");
    pathInfo.put(OutputFile.service, projectPath + "/src/main/java/" + javaPath + "/service/");
    pathInfo.put(OutputFile.serviceImpl, projectPath + "/src/main/java/" + javaPath + "/service/impl/");
    pathInfo.put(OutputFile.mapper, projectPath + "/src/main/java/" + javaPath + "/mapper/");
    pathInfo.put(OutputFile.other, projectPath + "/src/main/java/" + javaPath + "/dto/");
    AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
    generator.strategy(strategyConfig().entityBuilder().enableLombok().enableChainModel().idType(IdType.ASSIGN_ID).logicDeleteColumnName("is_deleted").addTableFills(// 基于数据库字段填充
    new Column("gmt_created", FieldFill.INSERT)).addTableFills(new Column("gmt_modified", FieldFill.INSERT_UPDATE)).controllerBuilder().enableRestStyle().build());
    generator.packageInfo(packageConfig().pathInfo(pathInfo).build());
    generator.global(globalConfig().outputDir(projectPath + "/src/main/java").enableSwagger().disableOpenDir().build());
    generator.injection(injectionConfig().build());
    generator.execute(new FreemarkerTemplateEngine() {

        @Override
        protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
            String entityName = tableInfo.getEntityName();
            String otherPath = getPathInfo(OutputFile.other);
            customFile.forEach((key, value) -> {
                String fileName = String.format((otherPath + File.separator + "%s"), entityName + key);
                outputFile(new File(fileName), objectMap, value);
            });
        }
    });
}
Also used : Column(com.baomidou.mybatisplus.generator.fill.Column) Properties(java.util.Properties) IOException(java.io.IOException) HashMap(java.util.HashMap) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) NotNull(javax.validation.constraints.NotNull) File(java.io.File) Test(org.junit.jupiter.api.Test) IdType(com.baomidou.mybatisplus.annotation.IdType) com.baomidou.mybatisplus.generator.config(com.baomidou.mybatisplus.generator.config) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) Map(java.util.Map) FieldFill(com.baomidou.mybatisplus.annotation.FieldFill) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) Column(com.baomidou.mybatisplus.generator.fill.Column) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) File(java.io.File) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Example 10 with FreemarkerTemplateEngine

use of com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine 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)

Aggregations

FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)18 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)16 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)13 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)10 ArrayList (java.util.ArrayList)9 Collections (java.util.Collections)3 FieldFill (com.baomidou.mybatisplus.annotation.FieldFill)2 FastAutoGenerator (com.baomidou.mybatisplus.generator.FastAutoGenerator)2 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)2 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)2 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)2 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)2 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)2 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)2 Column (com.baomidou.mybatisplus.generator.fill.Column)2 Test (org.junit.jupiter.api.Test)2 Console (cn.hutool.core.lang.Console)1 SystemUtil (cn.hutool.system.SystemUtil)1 IdType (com.baomidou.mybatisplus.annotation.IdType)1 com.baomidou.mybatisplus.generator.config (com.baomidou.mybatisplus.generator.config)1