Search in sources :

Example 1 with PackageConfig

use of com.baomidou.mybatisplus.generator.config.PackageConfig in project goodsKill by techa03.

the class GeneratorServiceEntity method generateByTables.

private static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
    GlobalConfig config = new GlobalConfig();
    String dbUrl = "jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(dbUrl).setUsername("root").setPassword("Password123").setDriverName("com.mysql.cj.jdbc.Driver");
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setNaming(NamingStrategy.underline_to_camel).setInclude(// 修改替换成你需要的表名,多个表名传数组
    tableNames);
    final String username = System.getProperty("user.name");
    config.setActiveRecord(false).setAuthor(username).setEnableCache(false).setBaseResultMap(false).setOutputDir("C:\\Users\\heng\\IdeaProjects\\1\\goodskill\\goodskill-spring-boot-provider\\goodskill-mp-dao\\src\\main\\java").setFileOverride(true).setDateType(DateType.ONLY_DATE);
    if (!serviceNameStartWithI) {
        config.setServiceName("");
    }
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setPackageInfo(new PackageConfig().setParent(packageName).setController("").setEntity("entity")).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 2 with PackageConfig

use of com.baomidou.mybatisplus.generator.config.PackageConfig 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 3 with PackageConfig

use of com.baomidou.mybatisplus.generator.config.PackageConfig 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 4 with PackageConfig

use of com.baomidou.mybatisplus.generator.config.PackageConfig 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 5 with PackageConfig

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

Aggregations

PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)19 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)16 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)16 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)16 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)16 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)5 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)4 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)3 MySqlTypeConvert (com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert)3 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)3 ArrayList (java.util.ArrayList)3 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)2 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)2 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)1 IColumnType (com.baomidou.mybatisplus.generator.config.rules.IColumnType)1 VelocityTemplateEngine (com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1