Search in sources :

Example 21 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project mall-learning by macrozheng.

the class MyBatisPlusGenerator method main.

public static void main(String[] args) {
    String projectPath = System.getProperty("user.dir") + "/mall-tiny-plus";
    String moduleName = scanner("模块名");
    String[] tableNames = scanner("表名,多个英文逗号分割").split(",");
    // 代码生成器
    AutoGenerator autoGenerator = new AutoGenerator();
    autoGenerator.setGlobalConfig(initGlobalConfig(projectPath));
    autoGenerator.setDataSource(initDataSourceConfig());
    autoGenerator.setPackageInfo(initPackageConfig(moduleName));
    autoGenerator.setCfg(initInjectionConfig(projectPath, moduleName));
    autoGenerator.setTemplate(initTemplateConfig());
    autoGenerator.setStrategy(initStrategyConfig(tableNames));
    autoGenerator.setTemplateEngine(new VelocityTemplateEngine());
    autoGenerator.execute();
}
Also used : VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 22 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project Hospital_BackEnd by ZJU-SE-2022-G.

the class CodeGeneratorTest method testTablePrefix.

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

Example 23 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project Hospital_BackEnd by ZJU-SE-2022-G.

the class CodeGeneratorTest method testSimple.

/**
 * 简单生成
 */
@Test
public void testSimple() {
    AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
    generator.strategy(strategyConfig().build());
    generator.global(globalConfig().build());
    generator.execute();
}
Also used : AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Example 24 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project Hospital_BackEnd by ZJU-SE-2022-G.

the class CodeGeneratorTest method testCustomTemplate.

/**
 * 自定义模板
 */
@Test
public void testCustomTemplate() {
    AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
    generator.strategy(strategyConfig().build());
    generator.template(templateConfig().entity("/templates/entity1.java").build());
    generator.global(globalConfig().build());
    generator.execute();
}
Also used : AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Example 25 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project drug by xiabud.

the class CodeGenerator method cg.

@Test
public void cg() {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("xiewc");
    gc.setOpen(false);
    gc.setFileOverride(false);
    gc.setServiceName("%sService");
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    // dsc.setUrl("jdbc:oracle:thin:@10.20.11.11:1521:JSGLAX1");
    dsc.setUrl("jdbc:mysql://localhost:3306/drug");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("admin");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.xiecode.drug");
    pc.setEntity("pojo");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    // 自定义配置
    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/mybatis/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);
    mpg.setPackageInfo(pc);
    // 如果模板引擎是 velocity
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // templateConfig.setEntity("templates/entity2.java");
    // templateConfig.setService();
    // templateConfig.setController();
    templateConfig.setXml(null);
    mpg.setTemplate(templateConfig);
    mpg.setPackageInfo(pc);
    // 如果模板引擎是 velocity
    // 策略配置
    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("你自己的父类控制器,没有就不用设置!");
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id");
    // strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    strategy.setInclude("sell_is_gone");
    strategy.setControllerMappingHyphenStyle(true);
    // strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new VelocityTemplateEngine());
    mpg.execute();
}
Also used : VelocityTemplateEngine(com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine) 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)

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