Search in sources :

Example 1 with AutoGenerator

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

use of com.baomidou.mybatisplus.generator.AutoGenerator in project demo by breeze0630.

the class AutoGen method main.

public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    // 全局配置
    mpg.setGlobalConfig(globalConfig());
    // 数据源
    mpg.setDataSource(dataSourceConfig());
    // 包
    mpg.setPackageInfo(packageConfig());
    // 策略
    mpg.setStrategy(strategyConfig());
    mpg.execute();
}
Also used : AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 3 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project java-example by saxingz.

the class GenCode method gen.

// /**
// * <p>
// * 读取控制台内容
// * </p>
// */
// public static String scanner(String tip) {
// Scanner scanner = new Scanner(System.in);
// StringBuilder help = new StringBuilder();
// help.append("请输入" + tip + ":");
// System.out.println(help.toString());
// if (scanner.hasNext()) {
// String ipt = scanner.next();
// if (StringUtils.isNotBlank(ipt)) {
// return ipt;
// }
// }
// throw new MybatisPlusException("请输入正确的" + tip + "!");
// }
@Test
public void gen() {
    String[] tables = new String[] { "channel" };
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("saxing");
    gc.setOpen(false);
    gc.setFileOverride(false);
    gc.setIdType(IdType.AUTO);
    // 实体属性 Swagger2 注解
    gc.setSwagger2(true);
    gc.setDateType(DateType.ONLY_DATE);
    // gc.setEntityName("%sDO");
    // gc.setXmlName("%sMapper");
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(jdbcUrl);
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername(username);
    dsc.setPassword(password);
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName("");
    pc.setParent("org.saxing.a0041_wemedia");
    pc.setEntity("domain.entity");
    pc.setService("logic");
    pc.setServiceImpl("logic.impl");
    pc.setController("controller");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            // to do nothing
            this.getConfig().getTableInfoList().forEach(tableInfo -> {
                String entityName = tableInfo.getEntityName();
                if (!entityName.endsWith("DO")) {
                    // 实体类和表名不一致
                    tableInfo.setConvert(true);
                    tableInfo.setEntityName(entityName + "DO");
                    tableInfo.setServiceName("I" + entityName + "Logic");
                    tableInfo.setServiceImplName(entityName + "Logic");
                }
            });
        }
    };
    // 自定义输出配置
    // 如果模板引擎是 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;
            String entityName = tableInfo.getEntityName();
            entityName = entityName.endsWith("DO") ? entityName.substring(0, entityName.length() - 2) : entityName;
            return projectPath + "/src/main/resources/mapper/" + /*+ pc.getModuleName()*/
            "/" + entityName + "Mapper" + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // 此处设置为null,就不会再java下创建xml的文件夹了
    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.setLogicDeleteFieldName("is_deleted");
    // 用数据库自带的时间更新
    // TableFill createdTime = new TableFill("created_time", FieldFill.INSERT);
    // TableFill updatedTime = new TableFill("updated_time", FieldFill.INSERT_UPDATE);
    // strategy.setTableFillList(Arrays.asList(createdTime, updatedTime));
    // 写于父类中的公共字段
    strategy.setInclude(tables);
    strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.execute();
}
Also used : ArrayList(java.util.ArrayList) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project weather-push by yangh124.

the class H2CodeGeneratorTest method testTablePrefix.

/**
 * 过滤表前缀(后缀同理,支持多个) result: t_simple -> simple
 */
@Test
public void testTablePrefix() {
    AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
    generator.strategy(strategyConfig().addTablePrefix("sys_").build());
    generator.global(globalConfig().build());
    generator.execute();
}
Also used : AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Example 5 with AutoGenerator

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

the class CodeGenerator method main.

public static void main(String[] args) throws Exception {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String basePath = CodeGenerator.class.getResource("").getPath();
    String projectPath = basePath.substring(0, basePath.indexOf("/target"));
    // String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("lyb-geek");
    gc.setOpen(false);
    gc.setBaseColumnList(true);
    gc.setBaseResultMap(true);
    gc.setServiceName("%sService");
    // gc.setSwagger2(true);// 实体属性 Swagger2 注解
    gc.setDateType(DateType.ONLY_DATE);
    mpg.setGlobalConfig(gc);
    String url = YmlUtil.getValue("spring.datasource.druid.url").toString();
    String username = YmlUtil.getValue("spring.datasource.druid.username").toString();
    String pwd = PropertiesUtil.INSTANCE.getProperty("password");
    String publicKey = PropertiesUtil.INSTANCE.getProperty("config.decrypt.key");
    String password = ConfigTools.decrypt(publicKey, pwd);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(url);
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername(username);
    dsc.setPassword(password);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.github.lybgeek.orm");
    pc.setEntity("model");
    pc.setMapper("dao");
    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/mapperPlus/" + pc.getModuleName()
    // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    // }
    // });
    focList.add(new FileOutConfig(templatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return projectPath + "/src/main/resources/mapper/mybatisplus/" + tableInfo.getEntityName().toLowerCase() + "/" + 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(null);
    mpg.setTemplate(templateConfig);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setSuperEntityClass("com.github.lybgeek.orm.common.model.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // strategy.setSuperControllerClass("com.github.lybgeek.orm.controller.BaseController");
    strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setSuperEntityColumns("id", "create_date", "update_date");
    strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    // 移除表的前缀
    // 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)

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