Search in sources :

Example 11 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project springbootquickstart by Pace2Car.

the class CodeGenerator method generateByTables.

public void generateByTables(String moduleName, String... tableNames) {
    // 全局配置
    GlobalConfig config = new GlobalConfig();
    config.setActiveRecord(false).setAuthor(AUTHOR).setOutputDir(OUTPUT_FILE).setFileOverride(false).setOpen(false).setServiceName("%sService");
    // 数据源配置
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USER_NAME).setPassword(PASSWORD).setDriverName(DRIVER);
    // 策略配置
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setEntityLombokModel(true).setCapitalMode(true).setNaming(NamingStrategy.underline_to_camel).setColumnNaming(NamingStrategy.underline_to_camel).setInclude(tableNames);
    // 自定义配置
    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) {
            // 自定义输出文件名
            return PROJECT_PATH + "/src/main/resources/mapper/" + (moduleName == null ? "" : moduleName) + "/" + tableInfo.getEntityName() + "Mapper" + ".xml";
        }
    });
    cfg.setFileOutConfigList(focList);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setCfg(cfg).setTemplate(templateConfig).setTemplateEngine(new FreemarkerTemplateEngine()).setPackageInfo(// 包配置
    new PackageConfig().setModuleName(moduleName).setParent(PACKAGE).setController("controller").setEntity("entity")).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 12 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project ddf-common by dongfangding.

the class CodeGenerator method generate.

public static void generate() {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir") + "/ddf-common-mybatis-generator";
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("mybatis-plus-generator");
    gc.setFileOverride(true);
    gc.setActiveRecord(false);
    gc.setBaseResultMap(true);
    gc.setBaseColumnList(true);
    gc.setEnableCache(false);
    gc.setOpen(false);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/better-together?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&tinyInt1isBit=false");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123");
    dsc.setTypeConvert(new MySqlTypeConvertCustom());
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.ddf.better.together");
    pc.setEntity("model.entity");
    /*        pc.setService("com.ddf.boot.service");
        pc.setServiceImpl("com.ddf.boot.service.impl");
        pc.setController("com.ddf.boot.controller");
        pc.setMapper("com.ddf.boot.mapper");*/
    pc.setXml(null);
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    // 如果模板引擎是 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;
        }
    });
    /*        // 自定义vo
        focList.add(new FileOutConfig("myTemplates/vo.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return gc.getOutputDir() + "/com/code/generator/domain/vo/"
                        + tableInfo.getEntityName()+"Vo" + StringPool.DOT_JAVA;
            }
        });*/
    // 应用自定义文件输出
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    mpg.setTemplateEngine(new VelocityTemplateEngine());
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // 过滤表名前缀
    strategy.setTablePrefix("");
    // 驼峰命名
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // 设置父类,如果存在的话
    // strategy.setSuperEntityClass("com.ddf.boot.common.core.model.BaseDomain");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id", "create_by", "create_time", "modify_by", "modify_time", "is_del", "version");
    // 其它带父类的
    // strategy.setSuperServiceClass("");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.execute();
}
Also used : DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) 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) StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 13 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project GMall by 18391713434.

the class CodeGenerator method main.

/**
 * <p>
 * 读取控制台内容
 * </p>
 */
public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // String projectPath = System.getProperty("user.dir");
    gc.setOutputDir("D:\\IDEA WORKSPACE4\\GMall\\gmall-api\\src\\main\\java");
    gc.setAuthor("LiangHuan");
    gc.setOpen(false);
    gc.setFileOverride(false);
    // gc.setSwagger2(true); //实体属性 Swagger2 注解
    gc.setControllerName("CartController");
    gc.setServiceName("PaymentService");
    gc.setServiceImplName("PaymentServiceImpl");
    gc.setMapperName("PaymentMapper");
    gc.setXmlName("Payment  Mapper");
    gc.setIdType(IdType.AUTO);
    gc.setDateType(DateType.ONLY_DATE);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://192.168.0.106:3306/gmall?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123456");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName("gmall");
    pc.setParent("com.xatu");
    pc.setEntity("entity");
    pc.setController("controller");
    pc.setMapper("mapper");
    pc.setService("service");
    mpg.setPackageInfo(pc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // strategy.setCapitalMode(true);//全局大写命名
    // 表名转驼峰
    strategy.setNaming(NamingStrategy.underline_to_camel);
    // 字段转驼峰
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setTablePrefix("");
    // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
    // strategy.setEntityLombokModel(true);
    // strategy.setRestControllerStyle(true);
    /* 公共父类
         strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");// 写于父类中的公共字段
         strategy.setSuperEntityColumns("id");*/
    // 设置要要映射的表名
    strategy.setInclude("payment_info");
    /* strategy.setControllerMappingHyphenStyle(true);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());*/
    /**
     * 自动填充策略
     */
    /*   TableFill gmtCreate=new TableFill("create_time", FieldFill.INSERT);
         TableFill gmtModified=new TableFill("modified_time",FieldFill.INSERT_UPDATE);
         ArrayList<TableFill> tableFills=new ArrayList<>();
         tableFills.add(gmtCreate);
         tableFills.add(gmtModified);
         strategy.setTableFillList(tableFills);*/
    /**
     * 乐观锁配置
     */
    // strategy.setVersionFieldName("version");
    mpg.setStrategy(strategy);
    mpg.execute();
}
Also used : AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 14 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project orion-ops by lijiahangmax.

the class CodeGenerator method runGenerator.

/**
 * 代码生成器
 */
private static void runGenerator() {
    // 全局配置
    GlobalConfig gbConfig = new GlobalConfig().setActiveRecord(false).setAuthor(Const.ORION_AUTHOR).setOutputDir("D:/MP/").setFileOverride(true).setIdType(IdType.AUTO).setServiceName("%sService").setServiceImplName("%sServiceImpl").setEntityName("%sDO").setMapperName("%sDAO").setXmlName("%sMapper").setControllerName("%sController").setSwagger2(false).setKotlin(false).setBaseResultMap(true).setEnableCache(false).setDateType(DateType.ONLY_DATE).setBaseColumnList(true);
    // 数据源配置
    DataSourceConfig dsConfig = new DataSourceConfig().setDbType(DbType.MYSQL).setDriverName("com.mysql.cj.jdbc.Driver").setUrl("jdbc:mysql://localhost:3306/orion-ops?characterEncoding=utf8").setUsername("root").setPassword("admin123").setTypeConvert(new MySqlTypeConvert() {

        @Override
        public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            if (fieldType.toLowerCase().contains("tinyint")) {
                return DbColumnType.INTEGER;
            }
            return super.processTypeConvert(globalConfig, fieldType);
        }
    });
    // 策略配置
    StrategyConfig stConfig = new StrategyConfig().setCapitalMode(true).setEntityTableFieldAnnotationEnable(true).setEntityLombokModel(true).setRestControllerStyle(true).setControllerMappingHyphenStyle(false).setEntityBooleanColumnRemoveIsPrefix(false).setNaming(NamingStrategy.underline_to_camel).setEntityColumnConstant(false).setChainModel(false).setTablePrefix("").setFieldPrefix("").setInclude("web_side_message");
    // 包名策略配置
    PackageConfig pkConfig = new PackageConfig().setParent("com.orion.ops").setMapper("dao").setService("service").setServiceImpl("service.impl").setController("controller").setEntity("entity.domain").setXml("mapper");
    // 整合配置
    AutoGenerator ag = new AutoGenerator().setGlobalConfig(gbConfig).setDataSource(dsConfig).setStrategy(stConfig).setPackageInfo(pkConfig);
    // 执行
    ag.execute();
}
Also used : IColumnType(com.baomidou.mybatisplus.generator.config.rules.IColumnType) StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 15 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project matecloud by matevip.

the class ManualGenerator method main.

public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // String projectPath = System.getProperty("user.dir");
    // 生成路径改为从控制台输入
    String projectPath = scanner("生成路径");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setFileOverride(true);
    // ActiveRecord特性
    gc.setActiveRecord(false);
    // XML ResultMap
    gc.setBaseResultMap(true);
    // XML columList
    gc.setBaseColumnList(true);
    gc.setEnableCache(false);
    // 自动打开输出目录
    gc.setOpen(false);
    gc.setAuthor("pangu");
    gc.setSwagger2(true);
    // 主键策略
    gc.setIdType(IdType.AUTO);
    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    gc.setServiceName("I%sService");
    gc.setServiceImplName("%sServiceImpl");
    gc.setMapperName("%sMapper");
    gc.setXmlName("%sMapper");
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/matex?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123456");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    // 此处设置包名,需要自定义
    pc.setParent(scanner("包名"));
    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 gc.getOutputDir() + StringPool.SLASH + pc.getParent().replace(".", StringPool.SLASH) + "/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();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setSuperEntityClass("vip.mate.core.database.entity.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    strategy.setSuperControllerClass("vip.mate.core.web.controller.BaseController");
    // 写于父类中的公共字段
    strategy.setSuperEntityColumns("id");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(scanner("表前缀"));
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new VelocityTemplateEngine());
    mpg.execute();
}
Also used : VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Aggregations

AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)69 Test (org.junit.jupiter.api.Test)25 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)22 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)17 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)17 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)17 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)17 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)16 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)16 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)10 MySqlTypeConvert (com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert)7 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)6 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)6 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)5 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)4 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)3 Column (com.baomidou.mybatisplus.generator.fill.Column)3 Property (com.baomidou.mybatisplus.generator.fill.Property)3 File (java.io.File)2