Search in sources :

Example 11 with GlobalConfig

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

use of com.baomidou.mybatisplus.generator.config.GlobalConfig in project chao-cloud by chaojunzi.

the class MybatisGeneratorConfig method globalConfig.

@Bean
@ConfigurationProperties(prefix = GLOBAL_CONFIG_PREFIX)
public GlobalConfig globalConfig() {
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setAuthor(author);
    gc.setOpen(false);
    gc.setFileOverride(true);
    // 仅用date 替代
    gc.setDateType(DateType.ONLY_DATE);
    // gc.setActiveRecord(true);
    // gc.setBaseColumnList(true);
    // gc.setBaseResultMap(true);
    gc.setServiceName("%sService");
    // gc.setSwagger2(true); 实体属性 Swagger2 注解
    return gc;
}
Also used : GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) ConfigurationProperties(org.springframework.boot.context.properties.ConfigurationProperties) Bean(org.springframework.context.annotation.Bean)

Example 13 with GlobalConfig

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

Example 14 with GlobalConfig

use of com.baomidou.mybatisplus.generator.config.GlobalConfig in project spring-boot-student by wyh-spring-ecosystem-student.

the class MyBatisPlusGenerator method main.

public static void main(String[] args) throws SQLException {
    // 1. 全局配置
    GlobalConfig config = new GlobalConfig();
    // 是否支持AR模式
    config.setActiveRecord(true).setAuthor(// 作者
    "yuhao.wang3").setOutputDir(// 生成路径
    "D:\\mybatis-plus").setFileOverride(// 文件覆盖
    true).setIdType(// 主键策略
    IdType.AUTO).setServiceName(// 设置生成的service接口的名字的首字母是否为I
    "%sService").setBaseResultMap(// 生成基本的resultMap
    true).setEnableCache(// 是否开启缓存
    false).setBaseColumnList(// 生成基本的SQL片段
    true);
    // 2. 数据源配置
    DataSourceConfig dsConfig = new DataSourceConfig();
    // 设置数据库类型
    dsConfig.setDbType(DbType.MYSQL).setDriverName("com.mysql.jdbc.Driver").setUrl("jdbc:mysql://host:3306/db").setUsername("admin").setPassword("admin");
    // 3. 策略配置globalConfiguration中
    StrategyConfig stConfig = new StrategyConfig();
    // 全局大写命名
    stConfig.setCapitalMode(true).setControllerMappingHyphenStyle(true).setRestControllerStyle(true).setEntityLombokModel(true).setColumnNaming(NamingStrategy.underline_to_camel).setNaming(// 数据库表映射到实体的命名策略
    NamingStrategy.underline_to_camel).setInclude(// 生成的表
    "fis_record_ext");
    // 4. 包名策略配置
    PackageConfig pkConfig = new PackageConfig();
    pkConfig.setParent("com.wlqq.insurance").setMapper(// dao
    "mybatis.mapper.withSharding").setService(// servcie
    "service").setController(// controller
    "controller").setEntity("mybatis.domain").setXml(// mapper.xml
    "mapper-xml");
    // 5. 整合配置
    AutoGenerator ag = new AutoGenerator();
    ag.setGlobalConfig(config).setDataSource(dsConfig).setStrategy(stConfig).setPackageInfo(pkConfig);
    // 6. 执行
    ag.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 15 with GlobalConfig

use of com.baomidou.mybatisplus.generator.config.GlobalConfig 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);
    System.out.println(mybatis.properties());
    // 数据库配置
    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") + File.separatorChar + ".." + File.separatorChar + "framework-core" + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "java").setFileOverride(true);
    if (!serviceNameStartWithI) {
        config.setServiceName("%sService");
    }
    TemplateConfig tc = new TemplateConfig();
    // 不输出web层
    tc.setController(null);
    /**
     * 如果是重新生成true,只生成 model<br/>
     * false,全部生成
     */
    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").setMapper("mapper").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) TemplateConfig(com.baomidou.mybatisplus.generator.config.TemplateConfig) PackageConfig(com.baomidou.mybatisplus.generator.config.PackageConfig) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Aggregations

GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)19 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)16 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)16 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)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