Search in sources :

Example 66 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project springboot-cli by liangqiding.

the class GenerationUtils method main.

public static void main(String[] args) {
    boolean fileOverride = false;
    GlobalConfig config = new GlobalConfig();
    String path = System.getProperty("user.dir");
    config.setActiveRecord(true).setAuthor("ding").setOutputDir(path + "\\src\\main\\java\\").setBaseResultMap(true).setBaseColumnList(true).setFileOverride(fileOverride);
    // ****************************** resource ***************************************
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USERNAME).setPassword(PASSWORD).setDriverName(DRIVER_NAME).setTypeConvert(new MySqlTypeConvert() {

        @Override
        public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            System.out.println("转换类型:" + fieldType);
            // tinyint转换成Boolean
            if (fieldType.toLowerCase().contains("tinyint")) {
                return DbColumnType.BOOLEAN;
            }
            if (fieldType.toLowerCase().contains("datetime")) {
                return DbColumnType.DATE;
            }
            return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
        }
    });
    // ****************************** Policy configuration ******************************************************
    List<TableFill> tableFillList = new ArrayList<>();
    tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));
    tableFillList.add(new TableFill("gmt_create", FieldFill.INSERT));
    tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setNaming(NamingStrategy.underline_to_camel).setTableFillList(tableFillList).setInclude(TABLE_NAME);
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setPackageInfo(new PackageConfig().setParent(PACKAGE).setController("controller").setEntity("domain").setMapper("dao").setXml("dao")).setTemplate(new TemplateConfig().setServiceImpl("templates/serviceImpl.java")).execute();
}
Also used : DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) ArrayList(java.util.ArrayList) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 67 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project springboot-cli by liangqiding.

the class GlobalConfigs method main.

public static void main(String[] args) {
    boolean fileOverride = false;
    GlobalConfig config = new GlobalConfig();
    String path = System.getProperty("user.dir");
    config.setActiveRecord(true).setAuthor("qiDing").setOutputDir(path + "\\src\\main\\java\\").setBaseResultMap(true).setBaseColumnList(true).setFileOverride(fileOverride);
    // ****************************** resource ***************************************
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USERNAME).setPassword(PASSWORD).setDriverName(DRIVER_NAME).setTypeConvert(new MySqlTypeConvert() {

        @Override
        public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            System.out.println("转换类型:" + fieldType);
            // tinyint转换成Boolean
            if (fieldType.toLowerCase().contains("tinyint")) {
                return DbColumnType.BOOLEAN;
            }
            if (fieldType.toLowerCase().contains("datetime")) {
                return DbColumnType.DATE;
            }
            return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
        }
    });
    // ****************************** Policy configuration ******************************************************
    List<TableFill> tableFillList = new ArrayList<>();
    tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));
    tableFillList.add(new TableFill("gmt_create", FieldFill.INSERT));
    tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setNaming(NamingStrategy.underline_to_camel).setTableFillList(tableFillList).setInclude(TABLE_NAME);
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setPackageInfo(new PackageConfig().setParent(PACKAGE).setController("controller").setEntity("domain").setMapper("dao").setXml("dao")).setTemplate(new TemplateConfig().setServiceImpl("templates/serviceImpl.java")).execute();
}
Also used : DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) ArrayList(java.util.ArrayList) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 68 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project seckill by yt-King.

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");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("yt");
    gc.setOpen(false);
    gc.setFileOverride(true);
    // gc.setSwagger2(true); //实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://47.96.23.31:3306/zhgx?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.yt.seckill");
    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/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("你自己的父类控制器,没有就不用设置!");
    // 写于父类中的公共字段
    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 69 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project tutorials-java by Artister.

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");
    gc.setOutputDir(projectPath + "/mybatis-plus-sample-generator/src/main/java");
    gc.setAuthor("jobob");
    gc.setOpen(false);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("1q2w3e4r");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.baomidou.mybatisplus.samples.generator");
    mpg.setPackageInfo(pc);
    // 自定义配置
    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 + "/mybatis-plus-sample-generator/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_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.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");
    strategy.setInclude(scanner("表名"));
    strategy.setSuperEntityColumns("id");
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    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) 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)

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