Search in sources :

Example 16 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project Spring-Cloud by zhao-staff-officer.

the class CodeGeneral method main.

public static void main(String[] args) {
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setOutputDir(Output_Dir);
    gc.setAuthor(Author);
    // 是否打开输出目录
    gc.setOpen(true);
    // 覆盖输出
    gc.setFileOverride(true);
    // Entity名称
    gc.setEntityName("%sEntity");
    // Dao名称
    gc.setMapperName("%sDao");
    // Mapper名称
    gc.setXmlName("%sMapper");
    // XML 二级缓存
    gc.setEnableCache(false);
    // XML ResultMap
    gc.setBaseResultMap(true);
    // XML columList
    gc.setBaseColumnList(true);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(Url);
    dsc.setDriverName(Driver_Name);
    dsc.setUsername(User_Name);
    dsc.setPassword(Pass_Word);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setController(controller);
    pc.setService(service);
    pc.setServiceImpl(serviceImpl);
    pc.setEntity(Entity);
    pc.setMapper(Dao);
    mpg.setPackageInfo(pc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    strategy.setInclude(tables);
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.execute();
}
Also used : StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 17 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project best-practices by Hansiyuan131.

the class CodeGeneratorTest method generatorCode.

@Test
public void generatorCode() {
    // 1、全局配置
    GlobalConfig config = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    // 开启AR模式
    config.setActiveRecord(true).setAuthor("hansiyuan").setOutputDir(projectPath + "/src/main/java").setFileOverride(true).setOpen(true).setServiceName("%sService").setBaseResultMap(true).setBaseColumnList(true);
    // 2、数据源配置
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    // 数据库类型
    dataSourceConfig.setDbType(DbType.MYSQL).setDriverName("com.mysql.cj.jdbc.Driver").setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false").setUsername("root").setPassword("mysql123456");
    // 3、策略配置
    StrategyConfig strategyConfig = new StrategyConfig();
    // 开启全局大写命名
    strategyConfig.setCapitalMode(true).setNaming(NamingStrategy.underline_to_camel).setColumnNaming(NamingStrategy.underline_to_camel).setRestControllerStyle(true).setEntityLombokModel(true).setInclude("user");
    // 4、包名策略配置
    PackageConfig packageConfig = new PackageConfig();
    // 设置包名的parent
    packageConfig.setParent("com.bp.scaffolding").setMapper("mapper").setService("service").setController("api").setEntity("domain.model").setXml("mapper");
    // 5、整合配置
    AutoGenerator autoGenerator = new AutoGenerator();
    autoGenerator.setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setPackageInfo(packageConfig);
    // 6、执行
    autoGenerator.execute();
}
Also used : StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Example 18 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project newBlog by 1537069101.

the class CodeGenerator method main.

public static void main(String[] args) {
    System.out.println("----------------------------============-----------------------------");
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    // gc.setOutputDir("D:\\test");
    gc.setAuthor("");
    gc.setOpen(false);
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    gc.setServiceName("%sService");
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://119.91.255.208:3306/wiki?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("Password");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(null);
    pc.setParent("cn.zzzyuan");
    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/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    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.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setControllerMappingHyphenStyle(true);
    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 19 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project shopzz by whoiszxl.

the class MyBatisFastAutoGenerator method main.

public static void main(String[] args) {
    // 1.数据源配置
    DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder("jdbc:mysql://aliyun.whoiszxl.com/shopzz?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false", "shopzz", "ECmzyDLN7bRSKtab");
    // 2.全局配置
    GlobalConfig.Builder globalConfigBuilder = new GlobalConfig.Builder();
    String projectPath = System.getProperty("user.dir");
    globalConfigBuilder.outputDir(projectPath + "/src/main/java");
    globalConfigBuilder.author(AUTHOR);
    globalConfigBuilder.disableOpenDir();
    if (ENABLE_SWAGGER) {
        globalConfigBuilder.enableSwagger();
    }
    // 3.包配置
    PackageConfig.Builder packageConfigBuilder = new PackageConfig.Builder();
    packageConfigBuilder.parent(PACKAGE_NAME);
    // 4.策略配置
    StrategyConfig.Builder strategyConfigBuilder = new StrategyConfig.Builder();
    // 过滤表前缀
    strategyConfigBuilder.addTablePrefix("sys_", "admin_", "oms_", "ums_", "vms_", "search_", "fms_", "rms_", "wms_", "pms_", "spms_");
    // 设置需要映射的表名
    strategyConfigBuilder.addInclude(DB_TABLES);
    // 下划线转驼峰
    strategyConfigBuilder.entityBuilder().naming(NamingStrategy.underline_to_camel);
    strategyConfigBuilder.entityBuilder().columnNaming(NamingStrategy.underline_to_camel);
    // entity的Lombok
    strategyConfigBuilder.entityBuilder().enableLombok();
    // 逻辑删除
    strategyConfigBuilder.entityBuilder().logicDeleteColumnName("is_deleted");
    strategyConfigBuilder.entityBuilder().logicDeletePropertyName("isDeleted");
    // 自动填充
    // 创建时间
    IFill createdAt = new Property("created_at", FieldFill.INSERT);
    // 更新时间
    IFill updatedAt = new Property("updated_at", FieldFill.INSERT_UPDATE);
    strategyConfigBuilder.entityBuilder().addTableFills(createdAt, updatedAt);
    // 乐观锁
    strategyConfigBuilder.entityBuilder().versionColumnName("version");
    strategyConfigBuilder.entityBuilder().versionPropertyName("version");
    // 使用RestController
    strategyConfigBuilder.controllerBuilder().enableRestStyle();
    // 将请求地址转换为驼峰命名,如 http://localhost:8080/hello_id_2
    strategyConfigBuilder.controllerBuilder().enableHyphenStyle();
    // 创建代码生成器对象,加载配置   对应1.2.3.4步
    AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfigBuilder.build());
    autoGenerator.global(globalConfigBuilder.build());
    autoGenerator.packageInfo(packageConfigBuilder.build());
    autoGenerator.strategy(strategyConfigBuilder.build());
    // 执行
    autoGenerator.execute();
}
Also used : DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) IFill(com.baomidou.mybatisplus.generator.IFill) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) Property(com.baomidou.mybatisplus.generator.fill.Property) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 20 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project longmarch by yuyueqty.

the class GeneratorCodeEngine method buildAutoGenerator.

public AutoGenerator buildAutoGenerator(String moduleName, String tableName, List<SysGenerator> fieldGenerationConditionList) {
    AutoGenerator autoSysGenerator = new AutoGenerator();
    GlobalConfig globalConfig = buildGlobalConfig();
    TemplateConfig templateConfig = buildTemplateConfig();
    DataSourceConfig dataSourceConfig = buildDataSourceConfig();
    PackageConfig packageConfig = buildPackageConfig(moduleName);
    InjectionConfig injectionConfig = buildInjectionConfig(tableName);
    StrategyConfig strategy = buildStrategyConfig(packageConfig, tableName);
    autoSysGenerator.setGlobalConfig(globalConfig);
    autoSysGenerator.setTemplate(templateConfig);
    autoSysGenerator.setDataSource(dataSourceConfig);
    autoSysGenerator.setPackageInfo(packageConfig);
    autoSysGenerator.setCfg(injectionConfig);
    autoSysGenerator.setStrategy(strategy);
    autoSysGenerator.setTemplateEngine(new FkTemplateEngine(fieldGenerationConditionList));
    return autoSysGenerator;
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) 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