MyBatis-Plus 自动生成器自定义变量

"该博客介绍了如何在MyBatis-Plus中使用自定义变量来修改XML位置,并在模板语言中使用这些变量。通过创建InjectionConfig,设置Map中的自定义变量,并在FileOutConfig中进行配置,实现自定义输出文件名。在模板中,可以通过${cfg.xxx}
摘要由CSDN通过智能技术生成

MyBatis-Plus 自动生成器自定义变量

    /**
     * 修改xml位置并添加自定义变量
     *
     * @return
     */
    private static InjectionConfig getInjectionConfig() {
        String moduleName = StringUtils.substring(MODULE_NAME, 1);
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // map中塞入自定义变量
                Map<String, Object> map = new HashedMap(8);
                map.put("module", moduleName);
                this.setMap(map);
            }
        };
        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 userDir + "/src/main/resources/mybatis/" + moduleName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

 然后再vm模板语言中使用以下配置可以生效,xxx是塞入map中的key值

${cfg.xxx}

直接set进AutoGenerator对象即可

    public static void main(String[] args) {
        new AutoGenerator().setGlobalConfig(getGlobalGenerate())
                .setDataSource(getDataSourceConfig())
                .setStrategy(getStrategyGenerate())
                .setPackageInfo(getPackageGenerate())
                .setTemplate(getTemplateConfig())
                .setCfg(getInjectionConfig())
                .execute();
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值