Search in sources :

Example 36 with AutoGenerator

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

the class GeneratorUtil method execute.

public static void execute(CodeConfig config) {
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setOutputDir(config.getOutputDir());
    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.setDbType(config.getDbType());
    dsc.setDriverName(config.getDriverName());
    dsc.setUrl(config.getUrl());
    dsc.setUsername(config.getUsername());
    dsc.setPassword(config.getPassword());
    dsc.setTypeConvert(new MySqlTypeConvert() {

        // 自定义数据库表字段类型转换【可选】
        @Override
        public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            // 将数据库中datetime转换成date
            if (fieldType.toLowerCase().contains("datetime")) {
                return DbColumnType.LOCAL_DATE_TIME;
            }
            return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
        }
    });
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // 此处设置包名,需要自定义
    pc.setParent(config.getPackageName());
    pc.setXml("mapper");
    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 config.getOutputDir() + "/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();
    // templateConfig.setXml("mapper");
    // 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(config.getTableName().split(","));
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(config.getPrefix());
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new VelocityTemplateEngine());
    mpg.execute();
}
Also used : DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 37 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project bbq-ddd by smingjie.

the class MybatisPlusGenerator method main.

public static void main(String[] args) {
    // 加载配置
    GeneratorConfig config = new GeneratorConfig();
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    mpg.setGlobalConfig(config.getGlobalConfig());
    mpg.setDataSource(config.getDataSourceConfig());
    mpg.setPackageInfo(config.getPackageConfig());
    mpg.setStrategy(config.getStrategyConfig());
    mpg.setTemplate(config.getTemplateConfig());
    mpg.setCfg(config.getInjectionConfig());
    mpg.setTemplateEngine(config.getTemplateEngine());
    mpg.execute();
}
Also used : AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 38 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project spring-boot-student by wyh-spring-ecosystem-student.

the class MyBatisPlusGenerator method main.

public static void main(String[] args) throws SQLException {
    // 1. 全局配置
    GlobalConfig config = new GlobalConfig();
    // 是否支持AR模式
    config.setActiveRecord(true).setAuthor(// 作者
    "yuhao.wang3").setOutputDir(// 生成路径
    "D:\\mybatis-plus").setFileOverride(// 文件覆盖
    true).setIdType(// 主键策略
    IdType.AUTO).setServiceName(// 设置生成的service接口的名字的首字母是否为I
    "%sService").setBaseResultMap(// 生成基本的resultMap
    true).setEnableCache(// 是否开启缓存
    false).setBaseColumnList(// 生成基本的SQL片段
    true);
    // 2. 数据源配置
    DataSourceConfig dsConfig = new DataSourceConfig();
    // 设置数据库类型
    dsConfig.setDbType(DbType.MYSQL).setDriverName("com.mysql.jdbc.Driver").setUrl("jdbc:mysql://host:3306/db").setUsername("admin").setPassword("admin");
    // 3. 策略配置globalConfiguration中
    StrategyConfig stConfig = new StrategyConfig();
    // 全局大写命名
    stConfig.setCapitalMode(true).setControllerMappingHyphenStyle(true).setRestControllerStyle(true).setEntityLombokModel(true).setColumnNaming(NamingStrategy.underline_to_camel).setNaming(// 数据库表映射到实体的命名策略
    NamingStrategy.underline_to_camel).setInclude(// 生成的表
    "fis_record_ext");
    // 4. 包名策略配置
    PackageConfig pkConfig = new PackageConfig();
    pkConfig.setParent("com.wlqq.insurance").setMapper(// dao
    "mybatis.mapper.withSharding").setService(// servcie
    "service").setController(// controller
    "controller").setEntity("mybatis.domain").setXml(// mapper.xml
    "mapper-xml");
    // 5. 整合配置
    AutoGenerator ag = new AutoGenerator();
    ag.setGlobalConfig(config).setDataSource(dsConfig).setStrategy(stConfig).setPackageInfo(pkConfig);
    // 6. 执行
    ag.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 39 with AutoGenerator

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

Example 40 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project project by Ahaochan.

the class CodeGeneratorTest method generator.

@Test
public void generator() {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // show table status WHERE 1=1 AND NAME IN ('cl_transfer_queue_fail')
    String[] tableName = { "user" };
    // 1. 数据源配置 https://mp.baomidou.com/config/generator-config.html#%E6%95%B0%E6%8D%AE%E6%BA%90-datasourceconfig-%E9%85%8D%E7%BD%AE
    DataSourceConfig dataSourceConfig = initDataSourceConfig(properties);
    mpg.setDataSource(dataSourceConfig);
    // 2. 数据库表配置 https://mp.baomidou.com/config/generator-config.html#%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8%E9%85%8D%E7%BD%AE
    StrategyConfig strategyConfig = initStrategyConfig(tableName);
    mpg.setStrategy(strategyConfig);
    // 3. 包名配置 https://mp.baomidou.com/config/generator-config.html#%E5%8C%85%E5%90%8D%E9%85%8D%E7%BD%AE
    PackageConfig packageConfig = initPackageConfig("moe.ahao.spring.boot.mybatis.plus", "module");
    mpg.setPackageInfo(packageConfig);
    // 4. 模板配置 https://mp.baomidou.com/config/generator-config.html#%E6%A8%A1%E6%9D%BF%E9%85%8D%E7%BD%AE
    TemplateConfig templateConfig = initTemplateConfig();
    mpg.setTemplate(templateConfig);
    // 5. 全局配置 https://mp.baomidou.com/config/generator-config.html#%E5%85%A8%E5%B1%80%E7%AD%96%E7%95%A5-globalconfig-%E9%85%8D%E7%BD%AE
    GlobalConfig globalConfig = initGlobalConfig();
    mpg.setGlobalConfig(globalConfig);
    // 6. 注入配置 https://mp.baomidou.com/config/generator-config.html#%E6%B3%A8%E5%85%A5-injectionconfig-%E9%85%8D%E7%BD%AE
    InjectionConfig injectionConfig = initInjectionConfig();
    mpg.setCfg(injectionConfig);
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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