Search in sources :

Example 1 with TableFill

use of com.baomidou.mybatisplus.generator.config.po.TableFill in project mybatis-plus-plugin by kana112233.

the class GenUtil method generatorCode.

public static void generatorCode(String tableName, GenConfig genConfig) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // String projectPath = System.getProperty("user.dir");
    String projectPath = genConfig.getRootFolder();
    gc.setOutputDir(projectPath + File.separator + genConfig.getModuleName() + "/src/main/java");
    gc.setAuthor(genConfig.getAuthor());
    gc.setOpen(false);
    gc.setFileOverride(genConfig.isCover());
    // 实体属性 Swagger2 注解
    gc.setSwagger2(genConfig.isSwagger());
    // 设置基础resultMap
    gc.setBaseResultMap(genConfig.isResultMap());
    // 是否在xml中添加二级缓存配置
    gc.setEnableCache(genConfig.isEnableCache());
    // 时间类型对应策略
    // gc.setDateType()
    // 开启 baseColumnList
    gc.setBaseColumnList(genConfig.isBaseColumnList());
    // 设置主键id
    gc.setIdType(IDTYPES[genConfig.getIdtype()].getIdType());
    mpg.setGlobalConfig(gc);
    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(MysqlUtil.getInstance().getDbUrl());
    // dsc.setSchemaName("public");
    dsc.setDriverName(MysqlUtil.getInstance().getJdbcDriver());
    dsc.setUsername(MysqlUtil.getInstance().getUsername());
    dsc.setPassword(MysqlUtil.getInstance().getPassword());
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // 在pack下的文件,现在不要设置null值
    pc.setModuleName(null);
    pc.setParent(genConfig.getPack());
    // 配置输出的包名
    pc.setEntity(genConfig.getEntityName());
    pc.setMapper(genConfig.getMapperName());
    pc.setController(genConfig.getControllerName());
    pc.setService(genConfig.getServiceName());
    pc.setServiceImpl(genConfig.getServiceImplName());
    String xmlName = "mapper";
    mpg.setPackageInfo(pc);
    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {

        @Override
        public void initMap() {
            // 生成自定义的Map obj.Result
            Map<String, Object> map = new HashMap<>();
            map.put("obj", pc.getParent() + ".obj");
            String idType = "String";
            map.put("camelTableName", underlineToCamel(tableName));
            setMap(map);
        }
    };
    // 如果模板引擎是 freemarker
    // String templatePath = "/templates/mapper.xml.ftl";
    String templatePath = "/templates";
    String mapperTemplatePath = templatePath + "/mapper.xml.ftl";
    // 如果模板引擎是 velocity
    // String templatePath = "/templates/mapper.xml.vm";
    // 自定义输出配置
    List<FileOutConfig> focList = new ArrayList<>();
    // 自定义配置会被优先输出
    focList.add(new FileOutConfig(mapperTemplatePath) {

        @Override
        public String outputFile(TableInfo tableInfo) {
            // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
            String result = projectPath + "/" + genConfig.getModuleName() + "/src/main/resources/" + xmlName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            return result;
        }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    // 配置模板
    TemplateConfig templateConfig = new TemplateConfig();
    // 配置自定义输出模板
    // 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
    // templateConfig.setEntity("templates/entity2.java");
    templateConfig.setEntity(templatePath + "/entity.java");
    templateConfig.setMapper(templatePath + "/mapper.java");
    templateConfig.setEntityKt(templatePath + "/entity.kt");
    templateConfig.setService(templatePath + "/service.java");
    templateConfig.setService(templatePath + "/service.java");
    templateConfig.setServiceImpl(templatePath + "/serviceImpl.java");
    templateConfig.setController(templatePath + "/controller.java");
    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.baomidou.ant.common.BaseEntity");
    // entity 是否使用lombok
    strategy.setEntityLombokModel(genConfig.isLombok());
    // 是否使用restController
    strategy.setRestControllerStyle(genConfig.isRestController());
    // 公共父类
    // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id");
    strategy.setInclude(tableName);
    strategy.setControllerMappingHyphenStyle(true);
    // 表前缀
    strategy.setTablePrefix(pc.getModuleName() + "_");
    // 是否使用自动填充
    if (genConfig.isFill()) {
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(tableFillList);
    }
    // 乐观锁
    // strategy.setVersionFieldName("version_name");
    mpg.setStrategy(strategy);
    mpg.setTemplateEngine(new MyFreemarkerTemplateEngine(projectPath));
    mpg.execute();
}
Also used : MyFreemarkerTemplateEngine(com.baomidou.plugin.idea.mybatisx.codegenerator.MyFreemarkerTemplateEngine) InjectionConfig(com.baomidou.mybatisplus.generator.InjectionConfig) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 2 with TableFill

use of com.baomidou.mybatisplus.generator.config.po.TableFill in project longmarch by yuyueqty.

the class CodeGenerator method getStrategyConfig.

/**
 * 策略配置
 *
 * @param pc
 * @return
 */
public StrategyConfig getStrategyConfig(PackageConfig pc) {
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
    // 数据库字段名称 : 'is_xxx',类型为 : tinyint. 在映射实体的时候则会去掉is
    strategy.setEntityBooleanColumnRemoveIsPrefix(true);
    strategy.setEntityLombokModel(true);
    strategy.setRestControllerStyle(true);
    // 公共父类
    // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
    // 写于父类中的公共字段
    // strategy.setSuperEntityColumns("id");
    // strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
    // 是否生成实体时,生成字段注解
    strategy.setEntityTableFieldAnnotationEnable(true);
    // 表填充字段
    List<TableFill> tableFillList = new ArrayList<>();
    tableFillList.add(new TableFill("create_by", FieldFill.INSERT));
    tableFillList.add(new TableFill("update_by", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
    tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
    strategy.setTableFillList(tableFillList);
    strategy.setControllerMappingHyphenStyle(true);
    strategy.setTablePrefix(pc.getModuleName() + "_");
    strategy.setInclude(tableIncludeList);
    strategy.setExclude(tableExcludeList);
    return strategy;
}
Also used : TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) ArrayList(java.util.ArrayList)

Example 3 with TableFill

use of com.baomidou.mybatisplus.generator.config.po.TableFill in project demo-parent by yindanqing925.

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") + "/mybatisplus-demo";
    gc.setOutputDir(projectPath + "/src/main/java");
    // TODO 设置用户名
    gc.setAuthor("yindanqing");
    gc.setOpen(false);
    // service 命名方式
    gc.setServiceName("%sService");
    // service impl 命名方式
    gc.setServiceImplName("%sServiceImpl");
    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    gc.setMapperName("%sMapper");
    gc.setXmlName("%sMapper");
    gc.setFileOverride(true);
    gc.setActiveRecord(true);
    // XML 二级缓存
    gc.setEnableCache(false);
    // XML ResultMap
    gc.setBaseResultMap(true);
    // XML columList
    gc.setBaseColumnList(false);
    mpg.setGlobalConfig(gc);
    // TODO 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://cdb-1v2wt3os.bj.tencentcdb.com:10243/nh?useUnicode=true&characterEncoding=UTF8");
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUsername("user_dev");
    dsc.setPassword("user_dev_nh");
    mpg.setDataSource(dsc);
    // TODO 包配置
    PackageConfig pc = new PackageConfig();
    // pc.setModuleName(scanner("模块名"));
    pc.setParent("org.nh.mybatisplus.dict");
    pc.setEntity("domain");
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    mpg.setPackageInfo(pc);
    // 自定义需要填充的字段
    List<TableFill> tableFillList = new ArrayList<>();
    // 如 每张表都有一个创建时间、修改时间
    // 而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改
    // 修改时,修改时间会修改,
    // 虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了,
    // 使用公共字段填充功能,就可以实现,自动按场景更新了。
    // 如下是配置
    // TableFill createField = new TableFill("gmt_create", FieldFill.INSERT);
    // TableFill modifiedField = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
    // tableFillList.add(createField);
    // tableFillList.add(modifiedField);
    // 自定义配置
    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 + "/src/main/java/org/nh/mybatisplus/dict/mapper/" + tableInfo.getEntityName() + "Mapper" + ".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.setEntityLombokModel(true);
    // 设置逻辑删除键
    strategy.setLogicDeleteFieldName("deleted");
    // TODO 指定生成的bean的数据库表名
    strategy.setInclude("transfer_dict");
    // strategy.setSuperEntityColumns("id");
    // 驼峰转连字符
    strategy.setControllerMappingHyphenStyle(true);
    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) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) 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)

Example 4 with TableFill

use of com.baomidou.mybatisplus.generator.config.po.TableFill in project springboot-cli by liangqiding.

the class GenerationUtils method main.

public static void main(String[] args) {
    boolean fileOverride = false;
    GlobalConfig config = new GlobalConfig();
    String path = System.getProperty("user.dir");
    config.setActiveRecord(true).setAuthor("ding").setOutputDir(path + "\\src\\main\\java\\").setBaseResultMap(true).setBaseColumnList(true).setFileOverride(fileOverride);
    // ****************************** resource ***************************************
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USERNAME).setPassword(PASSWORD).setDriverName(DRIVER_NAME).setTypeConvert(new MySqlTypeConvert() {

        @Override
        public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            System.out.println("转换类型:" + fieldType);
            // tinyint转换成Boolean
            if (fieldType.toLowerCase().contains("tinyint")) {
                return DbColumnType.BOOLEAN;
            }
            if (fieldType.toLowerCase().contains("datetime")) {
                return DbColumnType.DATE;
            }
            return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
        }
    });
    // ****************************** Policy configuration ******************************************************
    List<TableFill> tableFillList = new ArrayList<>();
    tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));
    tableFillList.add(new TableFill("gmt_create", FieldFill.INSERT));
    tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setNaming(NamingStrategy.underline_to_camel).setTableFillList(tableFillList).setInclude(TABLE_NAME);
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setPackageInfo(new PackageConfig().setParent(PACKAGE).setController("controller").setEntity("domain").setMapper("dao").setXml("dao")).setTemplate(new TemplateConfig().setServiceImpl("templates/serviceImpl.java")).execute();
}
Also used : DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) ArrayList(java.util.ArrayList) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Example 5 with TableFill

use of com.baomidou.mybatisplus.generator.config.po.TableFill in project springboot-cli by liangqiding.

the class GlobalConfigs method main.

public static void main(String[] args) {
    boolean fileOverride = false;
    GlobalConfig config = new GlobalConfig();
    String path = System.getProperty("user.dir");
    config.setActiveRecord(true).setAuthor("qiDing").setOutputDir(path + "\\src\\main\\java\\").setBaseResultMap(true).setBaseColumnList(true).setFileOverride(fileOverride);
    // ****************************** resource ***************************************
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USERNAME).setPassword(PASSWORD).setDriverName(DRIVER_NAME).setTypeConvert(new MySqlTypeConvert() {

        @Override
        public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
            System.out.println("转换类型:" + fieldType);
            // tinyint转换成Boolean
            if (fieldType.toLowerCase().contains("tinyint")) {
                return DbColumnType.BOOLEAN;
            }
            if (fieldType.toLowerCase().contains("datetime")) {
                return DbColumnType.DATE;
            }
            return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
        }
    });
    // ****************************** Policy configuration ******************************************************
    List<TableFill> tableFillList = new ArrayList<>();
    tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));
    tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));
    tableFillList.add(new TableFill("gmt_create", FieldFill.INSERT));
    tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));
    tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));
    StrategyConfig strategyConfig = new StrategyConfig();
    strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setNaming(NamingStrategy.underline_to_camel).setTableFillList(tableFillList).setInclude(TABLE_NAME);
    new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig).setPackageInfo(new PackageConfig().setParent(PACKAGE).setController("controller").setEntity("domain").setMapper("dao").setXml("dao")).setTemplate(new TemplateConfig().setServiceImpl("templates/serviceImpl.java")).execute();
}
Also used : DbColumnType(com.baomidou.mybatisplus.generator.config.rules.DbColumnType) ArrayList(java.util.ArrayList) MySqlTypeConvert(com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert) TableFill(com.baomidou.mybatisplus.generator.config.po.TableFill) AutoGenerator(com.baomidou.mybatisplus.generator.AutoGenerator)

Aggregations

TableFill (com.baomidou.mybatisplus.generator.config.po.TableFill)5 AutoGenerator (com.baomidou.mybatisplus.generator.AutoGenerator)4 ArrayList (java.util.ArrayList)4 InjectionConfig (com.baomidou.mybatisplus.generator.InjectionConfig)2 MySqlTypeConvert (com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert)2 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)2 DbColumnType (com.baomidou.mybatisplus.generator.config.rules.DbColumnType)2 DataSourceConfig (com.baomidou.mybatisplus.generator.config.DataSourceConfig)1 FileOutConfig (com.baomidou.mybatisplus.generator.config.FileOutConfig)1 GlobalConfig (com.baomidou.mybatisplus.generator.config.GlobalConfig)1 PackageConfig (com.baomidou.mybatisplus.generator.config.PackageConfig)1 StrategyConfig (com.baomidou.mybatisplus.generator.config.StrategyConfig)1 TemplateConfig (com.baomidou.mybatisplus.generator.config.TemplateConfig)1 FreemarkerTemplateEngine (com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine)1 MyFreemarkerTemplateEngine (com.baomidou.plugin.idea.mybatisx.codegenerator.MyFreemarkerTemplateEngine)1