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();
}
"该博客介绍了如何在MyBatis-Plus中使用自定义变量来修改XML位置,并在模板语言中使用这些变量。通过创建InjectionConfig,设置Map中的自定义变量,并在FileOutConfig中进行配置,实现自定义输出文件名。在模板中,可以通过${cfg.xxx}
3800

被折叠的 条评论
为什么被折叠?



