Search in sources :

Example 31 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project vip by guangdada.

the class MyBatisPlusGenerator method main.

public static void main(String[] args) {
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // 这里写你自己的java目录
    gc.setOutputDir("D:\\ideaSpace\\guns2\\src\\main\\java");
    // 是否覆盖
    gc.setFileOverride(true);
    gc.setActiveRecord(true);
    // XML 二级缓存
    gc.setEnableCache(false);
    // XML ResultMap
    gc.setBaseResultMap(true);
    // XML columList
    gc.setBaseColumnList(false);
    gc.setAuthor("chengxg");
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    dsc.setTypeConvert(new MySqlTypeConvert() {

        // 自定义数据库表字段类型转换【可选】
        @Override
        public DbColumnType processTypeConvert(String fieldType) {
            return super.processTypeConvert(fieldType);
        }
    });
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("kkjj");
    dsc.setPassword("KKjj111111!@#");
    dsc.setUrl("jdbc:mysql://192.168.168.153:3306/vipdb?characterEncoding=utf8");
    mpg.setDataSource(dsc);
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // 此处可以修改为您的表前缀
    strategy.setTablePrefix(new String[] { "v_" });
    // 表名生成策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    // 需要生成的表
    strategy.setInclude(new String[] { "v_store_coupon" });
    mpg.setStrategy(strategy);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent(null);
    pc.setEntity("com.ikoori.vip.common.persistence.model");
    pc.setMapper("com.ikoori.vip.common.persistence.dao");
    pc.setXml("com.ikoori.vip.common.persistence.dao.mapping");
    // 本项目没用,生成之后删掉
    pc.setService("TTT");
    // 本项目没用,生成之后删掉
    pc.setServiceImpl("TTT");
    // 本项目没用,生成之后删掉
    pc.setController("TTT");
    mpg.setPackageInfo(pc);
    // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            Map<String, Object> map = new HashMap<>();
            map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
            this.setMap(map);
        }
    };
    mpg.setCfg(cfg);
    // 执行生成
    mpg.execute();
    // 打印注入设置
    System.err.println(mpg.getCfg().getMap().get("abc"));
}
Also used : DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) HashMap(java.util.HashMap) DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) StrategyConfig(com.baomidou.mybatisplus.generator.config.StrategyConfig) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 32 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project java-apply by javachengwc.

the class AbstractGeneratorConfig method doMpGeneration.

public void doMpGeneration() {
    init();
    AutoGenerator autoGenerator = new AutoGenerator();
    autoGenerator.setGlobalConfig(globalConfig);
    autoGenerator.setDataSource(dataSourceConfig);
    autoGenerator.setStrategy(strategyConfig);
    autoGenerator.setPackageInfo(packageConfig);
    autoGenerator.execute();
    destory();
    // 获取table信息,用于guns代码生成
    List<TableInfo> tableInfoList = autoGenerator.getConfig().getTableInfoList();
    if (tableInfoList != null && tableInfoList.size() > 0) {
        this.tableInfo = tableInfoList.get(0);
    }
}
Also used : TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 33 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project dynamic_dataSource by tianliuzhen.

the class CodeGenerator method main.

public static void main(String[] args) {
    String pathStr = "/mybatis-plus";
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // 项目的相对路径
    String projectPath = System.getProperty("user.dir") + pathStr;
    System.out.println("项目的相对路径" + projectPath);
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("Mr.tian");
    gc.setOpen(false);
    // gc.setSwagger2(true); //实体属性 Swagger2 注解
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://47.98.253.2:3306/master?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // 指定数据库
    dsc.setSchemaName("test1");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("Tlz19970905");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // TODO:  会新加一层路径
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("com.aaa.mybatisplus");
    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";
    // 自定义输出配置  xml 配置文件
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    // TODO:  生成 xml 文件
    focList.add(new FileOutConfig(templatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            return // + pc.getModuleName()
            projectPath + "/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();
    // 这里会在 mapper 目录下默认生成 xml 目录及文件
    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.aaa.mybatisplus.common.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    strategy.setSuperControllerClass("com.aaa.mybatisplus.common.BaseController");
    // 写于父类中的公共字段
    strategy.setSuperEntityColumns("id");
    // TODO:要设置生成哪些表 如果不设置就是生成所有的表
    // 2019/12/21
    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 34 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project c4p by xseuen.

the class CodeGenerator method codeGenerator.

/**
 * 代码生成
 *
 * @see OutputFile
 */
@Test
public void codeGenerator() {
    String projectPath = System.getProperty("user.dir");
    String javaPath = projectPackage.replaceAll("\\.", "/");
    // 设置自定义路径
    Map<OutputFile, String> pathInfo = new HashMap<>();
    pathInfo.put(OutputFile.mapperXml, projectPath + "/src/main/resources/mybatis/mapper/");
    pathInfo.put(OutputFile.entity, projectPath + "/src/main/java/" + javaPath + "/entity/");
    pathInfo.put(OutputFile.controller, projectPath + "/src/main/java/" + javaPath + "/controller/");
    pathInfo.put(OutputFile.service, projectPath + "/src/main/java/" + javaPath + "/service/");
    pathInfo.put(OutputFile.serviceImpl, projectPath + "/src/main/java/" + javaPath + "/service/impl/");
    pathInfo.put(OutputFile.mapper, projectPath + "/src/main/java/" + javaPath + "/mapper/");
    pathInfo.put(OutputFile.other, projectPath + "/src/main/java/" + javaPath + "/dto/");
    AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
    generator.strategy(strategyConfig().entityBuilder().enableLombok().enableChainModel().idType(IdType.ASSIGN_ID).logicDeleteColumnName("is_deleted").addTableFills(// 基于数据库字段填充
    new Column("gmt_created", FieldFill.INSERT)).addTableFills(new Column("gmt_modified", FieldFill.INSERT_UPDATE)).controllerBuilder().enableRestStyle().build());
    generator.packageInfo(packageConfig().pathInfo(pathInfo).build());
    generator.global(globalConfig().outputDir(projectPath + "/src/main/java").enableSwagger().disableOpenDir().build());
    generator.injection(injectionConfig().build());
    generator.execute(new FreemarkerTemplateEngine() {

        @Override
        protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
            String entityName = tableInfo.getEntityName();
            String otherPath = getPathInfo(OutputFile.other);
            customFile.forEach((key, value) -> {
                String fileName = String.format((otherPath + File.separator + "%s"), entityName + key);
                outputFile(new File(fileName), objectMap, value);
            });
        }
    });
}
Also used : Column(com.baomidou.mybatisplus.generator.fill.Column) Properties(java.util.Properties) IOException(java.io.IOException) HashMap(java.util.HashMap) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) NotNull(javax.validation.constraints.NotNull) File(java.io.File) Test(org.junit.jupiter.api.Test) IdType(com.baomidou.mybatisplus.annotation.IdType) com.baomidou.mybatisplus.generator.config(com.baomidou.mybatisplus.generator.config) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) Map(java.util.Map) FieldFill(com.baomidou.mybatisplus.annotation.FieldFill) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) Column(com.baomidou.mybatisplus.generator.fill.Column) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) File(java.io.File) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator) Test(org.junit.jupiter.api.Test)

Example 35 with AutoGenerator

use of com.baomidou.mybatisplus.generator.AutoGenerator in project finacialpotal by f117-sercet.

the class CodeGenerator method genCode.

@Test
public void genCode() {
    // 1、创建代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 2、全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("Helen");
    // 生成后是否打开资源管理器
    gc.setOpen(false);
    // 去掉Service接口的首字母I
    gc.setServiceName("%sService");
    // 主键策略
    gc.setIdType(IdType.AUTO);
    // 开启Swagger2模式
    gc.setSwagger2(true);
    mpg.setGlobalConfig(gc);
    // 3、数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/srb_core?serverTimezone=GMT%2B8&characterEncoding=utf-8");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("123123");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    // 4、包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.atguigu.srb.core");
    // 此对象与数据库表结构一一对应,通过 DAO 层向上传输数据源对象。
    pc.setEntity("pojo.entity");
    mpg.setPackageInfo(pc);
    // 5、策略配置
    StrategyConfig strategy = new StrategyConfig();
    // 数据库表映射到实体的命名策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    // 数据库表字段映射到实体的命名策略
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // lombok
    strategy.setEntityLombokModel(true);
    // 逻辑删除字段名
    strategy.setLogicDeleteFieldName("is_deleted");
    // 去掉布尔值的is_前缀(确保tinyint(1))
    strategy.setEntityBooleanColumnRemoveIsPrefix(true);
    // restful api风格控制器
    strategy.setRestControllerStyle(true);
    mpg.setStrategy(strategy);
    // 6、执行
    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) Test(org.junit.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