Search in sources :

Example 16 with DataSourceConfig

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

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

Example 18 with DataSourceConfig

use of com.baomidou.mybatisplus.generator.config.DataSourceConfig in project shopzz by whoiszxl.

the class MyBatisCodeGenerator method main.

// 代码生成步骤四:直接运行就能生成了
public static void main(String[] args) {
    // 1. 创建代码生成器
    AutoGenerator generator = new AutoGenerator();
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    System.out.println(projectPath);
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor("whoiszxl");
    // 生成后是否打开资源管理器
    gc.setOpen(false);
    // 重新生成时文件是否覆盖
    gc.setFileOverride(false);
    // 去除Service生成的 I前缀
    gc.setServiceName("%sService");
    // 主键策略
    gc.setIdType(IdType.ID_WORKER);
    // 定义生成的实体类中日期类型
    gc.setDateType(DateType.ONLY_DATE);
    // 开启Swagger2模式
    gc.setSwagger2(true);
    generator.setGlobalConfig(gc);
    // 3、数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(URL);
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername(USERNAME);
    dsc.setPassword(PASSWORD);
    dsc.setDbType(DbType.MYSQL);
    generator.setDataSource(dsc);
    // 4、包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName("wms"); //模块名
    pc.setParent("com.whoiszxl");
    pc.setController("controller");
    pc.setEntity("entity");
    pc.setService("service");
    pc.setMapper("mapper");
    generator.setPackageInfo(pc);
    // 5、策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setInclude(tableNames);
    // 数据库表映射到实体的命名策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    // 生成实体时去掉表前缀
    strategy.setTablePrefix(tablePrefix);
    // 数据库表字段映射到实体的命名策略
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // lombok 模型 @Accessors(chain = true) setter链式操作
    strategy.setEntityLombokModel(true);
    // restful api风格控制器
    strategy.setRestControllerStyle(true);
    // url中驼峰转连字符
    strategy.setControllerMappingHyphenStyle(true);
    generator.setStrategy(strategy);
    // 6、执行
    generator.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 19 with DataSourceConfig

use of com.baomidou.mybatisplus.generator.config.DataSourceConfig in project tutorials-java by Artister.

the class MysqlGenerator method main.

/**
 * RUN THIS
 */
public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath + "/mybatis-plus-sample-generator/src/main/java");
    gc.setAuthor("jobob");
    gc.setOpen(false);
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");
    // dsc.setSchemaName("public");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("1q2w3e4r");
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName(scanner("模块名"));
    pc.setParent("com.baomidou.mybatisplus.samples.generator");
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
        // to do nothing
        }
    };
    List<FileOutConfig> focList = new ArrayList<>();
    focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输入文件名称
            return projectPath + "/mybatis-plus-sample-generator/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    mpg.setTemplate(new TemplateConfig().setXml(null));
    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");
    strategy.setEntityLombokModel(true);
    strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");
    strategy.setInclude(scanner("表名"));
    strategy.setSuperEntityColumns("id");
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    mpg.setStrategy(strategy);
    // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
}
Also used : DataSourceConfig(com.baomidou.mybatisplus.generator.config.DataSourceConfig) GlobalConfig(com.baomidou.mybatisplus.generator.config.GlobalConfig) 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) FreemarkerTemplateEngine(com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Aggregations

DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)19 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)16 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)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 MySqlTypeConvert (com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert)5 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)4 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)3 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)3 ArrayList (java.util.ArrayList)3 MySqlQuery (com.baomidou.mybatisplus.generator.config.querys.MySqlQuery)2 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)2 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)2 FieldFill (com.baomidou.mybatisplus.annotation.FieldFill)1 MybatisPlusException (com.baomidou.mybatisplus.core.exceptions.MybatisPlusException)1 FastAutoGenerator (com.baomidou.mybatisplus.generator.FastAutoGenerator)1 OutputFile (com.baomidou.mybatisplus.generator.config.OutputFile)1 TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)1 IColumnType (com.baomidou.mybatisplus.generator.config.rules.IColumnType)1