Search in sources :

Example 6 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project springboot-learning by lyb-geek.

the class CodeGeneratorUtils method execute.

/**
 * 代码生成逻辑
 * @param codeGeneratorHelper
 * @throws Exception
 */
public static void execute(CodeGeneratorHelper codeGeneratorHelper) throws Exception {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 项目路径
    String projectPath = codeGeneratorHelper.getProjectPath();
    if (StringUtils.isBlank(projectPath)) {
        projectPath = getProjectPath();
        System.out.println("-----------------项目根路径未配置将使用默认配置路径--->" + projectPath + "----------------");
    }
    // 全局配置
    GlobalConfig gc = getGlobalConfig(codeGeneratorHelper, projectPath);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = getDataSourceConfig(codeGeneratorHelper);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = getPackageConfig(codeGeneratorHelper);
    mpg.setPackageInfo(pc);
    InjectionConfig cfg = getInjectionConfig(pc, codeGeneratorHelper, projectPath);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = getStrategyConfig(codeGeneratorHelper);
    mpg.setStrategy(strategy);
    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)

Example 7 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project zblCode by zbl5337.

the class CodeGenerator method main.

public static void main(String[] args) {
    AutoGenerator mpg = new AutoGenerator();
    String outputPath = null;
    try {
        outputPath = ResourceUtils.getURL("classpath:").getPath().replace("code-generator/target/classes/", "code-service/src/main/java");
    } catch (Exception e) {
        e.printStackTrace();
    }
    String modelName = scanner("模块名");
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("zbl5337");
    dsc.setUrl("jdbc:mysql://127.0.0.1:3306/code?characterEncoding=utf8&useSSL=false");
    mpg.setDataSource(dsc);
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setOutputDir(outputPath);
    gc.setFileOverride(true);
    gc.setEnableCache(false);
    gc.setBaseResultMap(false);
    gc.setBaseColumnList(true);
    gc.setAuthor("zbl");
    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    gc.setMapperName("%sMapper");
    gc.setServiceName("%sService");
    gc.setServiceImplName("%sServiceImpl");
    mpg.setGlobalConfig(gc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setTablePrefix(new String[] { "sys_", "res_", "uc_", "stdy_", "opera_", "fin_" });
    // 表名生成策略
    strategy.setNaming(NamingStrategy.remove_prefix_and_camel);
    strategy.setFieldNaming(NamingStrategy.underline_to_camel);
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setEntityBuliderModel(true);
    mpg.setStrategy(strategy);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.zbl.code");
    pc.setEntity(modelName + ".pojo.po");
    pc.setMapper(modelName + ".dao");
    pc.setService(modelName + ".service");
    pc.setServiceImpl(modelName + ".service.impl");
    pc.setXml(modelName + ".dao");
    mpg.setPackageInfo(pc);
    // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
    // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
    TemplateConfig tc = new TemplateConfig();
    tc.setEntity("/mybatis-generator/model.java.vm");
    tc.setMapper("/mybatis-generator/mapper.java.vm");
    tc.setXml("/mybatis-generator/mapper.xml.vm");
    tc.setService("/mybatis-generator/service.java.vm");
    tc.setServiceImpl("/mybatis-generator/serviceImpl.java.vm");
    mpg.setTemplate(tc);
    // 执行生成
    mpg.execute();
}
Also used : MybatisPlusException(com.baomidou.mybatisplus.exceptions.MybatisPlusException) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 8 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project spring-boot-web by wangchengming666.

the class MybatisPlusCodeGenerator method generateByTables.

private void generateByTables(String prefix, String packageName, String... tableNames) {
    // user -> UserService, 设置成true: user -> IUserService
    boolean serviceNameStartWithI = false;
    GlobalConfig config = new GlobalConfig();
    MybatisPlusConfig mybatis = new MybatisPlusConfig().setPrefix(prefix);
    // 数据库配置
    String dbUrl = mybatis.getString("jdbc-url");
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(dbUrl).setUsername(mybatis.getString("username")).setPassword(mybatis.getString("password")).setDriverName(mybatis.getString("driver-class-name"));
    // 生成策略配置
    StrategyConfig strategyConfig = new StrategyConfig();
    // 全局大写命名 ORACLE 注意
    strategyConfig.setCapitalMode(true).setEntityLombokModel(false).setNaming(NamingStrategy.underline_to_camel).setInclude(// 修改替换成你需要的表名,多个表名传数组
    tableNames);
    /**
     * 设置超类
     */
    strategyConfig.setSuperEntityClass("com.myname.skeleton.commons.BaseObject");
    // 输出目录
    config.setActiveRecord(false).setOutputDir(System.getProperty("user.dir") + "//src//main//java").setFileOverride(true);
    if (!serviceNameStartWithI) {
        config.setServiceName("%sService");
    }
    TemplateConfig tc = new TemplateConfig();
    // 不输出web层
    tc.setController(null);
    /**
     * 如果是重新生成,只生成 model
     */
    if (false) {
        tc.setMapper(null);
        tc.setService(null);
        tc.setServiceImpl(null);
        tc.setXml(null);
    }
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setTemplate(tc).setPackageInfo(new PackageConfig().setParent(packageName).setXml("mapper." + mybatis.getSchemaName(dbUrl)).setMapper("mapper." + mybatis.getSchemaName(dbUrl)).setEntity("entity." + mybatis.getSchemaName(dbUrl))).execute();
}
Also used : StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 9 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project codingmore-learning by itwanger.

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("D:\\Temp\\CodeGenerate");
    gc.setAuthor("石磊");
    gc.setOpen(false);
    gc.setDateType(DateType.ONLY_DATE);
    gc.setSwagger2(true);
    gc.setIdType(IdType.AUTO);
    mpg.setGlobalConfig(gc);
    gc.setBaseColumnList(true);
    gc.setBaseResultMap(true);
    gc.setFileOverride(true);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://118.190.99.232:3306/learn?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("learn");
    dsc.setPassword("pet8nwhBbinRyrNB");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.learn");
    pc.setEntity("model");
    mpg.setPackageInfo(pc);
    StrategyConfig strategyConfig = new StrategyConfig();
    // 配置数据表与实体类名之间映射的策略
    strategyConfig.setInclude("term_taxonomy");
    strategyConfig.setNaming(NamingStrategy.underline_to_camel);
    // 配置数据表的字段与实体类的属性名之间映射的策略
    strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
    strategyConfig.setEntityLombokModel(true);
    mpg.setStrategy(strategyConfig);
    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 10 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project mybatis-plus-plugin by kana112233.

the class GenUtil method generatorCode.

public static void generatorCode(String tableName, GenConfig genConfig) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // String projectPath = System.getProperty("user.dir");
    String projectPath = genConfig.getRootFolder();
    gc.setOutputDir(projectPath + File.separator + genConfig.getModuleName() + "/src/main/java");
    gc.setAuthor(genConfig.getAuthor());
    gc.setOpen(false);
    gc.setFileOverride(genConfig.isCover());
    // 实体属性 Swagger2 注解
    gc.setSwagger2(genConfig.isSwagger());
    // 设置基础resultMap
    gc.setBaseResultMap(genConfig.isResultMap());
    // 是否在xml中添加二级缓存配置
    gc.setEnableCache(genConfig.isEnableCache());
    // 时间类型对应策略
    // gc.setDateType()
    // 开启 baseColumnList
    gc.setBaseColumnList(genConfig.isBaseColumnList());
    // 设置主键id
    gc.setIdType(IDTYPES[genConfig.getIdtype()].getIdType());
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(MysqlUtil.getInstance().getDbUrl());
    // dsc.setSchemaName("public");
    dsc.setDriverName(MysqlUtil.getInstance().getJdbcDriver());
    dsc.setUsername(MysqlUtil.getInstance().getUsername());
    dsc.setPassword(MysqlUtil.getInstance().getPassword());
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // 在pack下的文件,现在不要设置null值
    pc.setModuleName(null);
    pc.setParent(genConfig.getPack());
    // 配置输出的包名
    pc.setEntity(genConfig.getEntityName());
    pc.setMapper(genConfig.getMapperName());
    pc.setController(genConfig.getControllerName());
    pc.setService(genConfig.getServiceName());
    pc.setServiceImpl(genConfig.getServiceImplName());
    String xmlName = "mapper";
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            // 生成自定义的Map obj.Result
            Map<String, Object> map = new HashMap<>();
            map.put("obj", pc.getParent() + ".obj");
            String idType = "String";
            map.put("camelTableName", underlineToCamel(tableName));
            setMap(map);
        }
    };
    // 如果模板引擎是 freemarker
    // String templatePath = "/templates/mapper.xml.ftl";
    String templatePath = "/templates";
    String mapperTemplatePath = templatePath + "/mapper.xml.ftl";
    // 如果模板引擎是 velocity
    // String templatePath = "/templates/mapper.xml.vm";
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig(mapperTemplatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            String result = projectPath + "/" + genConfig.getModuleName() + "/src/main/resources/" + xmlName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            return result;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // templateConfig.setEntity("templates/entity2.java");
    templateConfig.setEntity(templatePath + "/entity.java");
    templateConfig.setMapper(templatePath + "/mapper.java");
    templateConfig.setEntityKt(templatePath + "/entity.kt");
    templateConfig.setService(templatePath + "/service.java");
    templateConfig.setService(templatePath + "/service.java");
    templateConfig.setServiceImpl(templatePath + "/serviceImpl.java");
    templateConfig.setController(templatePath + "/controller.java");
    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.baomidou.ant.common.BaseEntity");
    // entity 是否使用lombok
    strategy.setEntityLombokModel(genConfig.isLombok());
    // 是否使用restController
    strategy.setRestControllerStyle(genConfig.isRestController());
    // 公共父类
    // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id");
    strategy.setInclude(tableName);
    strategy.setControllerMappingHyphenStyle(true);
    // 表前缀
    strategy.setTablePrefix(pc.getModuleName() + "_");
    // 是否使用自动填充
    if (genConfig.isFill()) {
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(tableFillList);
    }
    // 乐观锁
    // strategy.setVersionFieldName("version_name");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new MyFreemarkerTemplateEngine(projectPath));
    mpg.execute();
}
Also used : MyFreemarkerTemplateEngine(com.baomidou.plugin.idea.mybatisx.codegenerator.MyFreemarkerTemplateEngine) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) 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