generator icon indicating copy to clipboard operation
generator copied to clipboard

CustomFile自定义生成类

Open yangjianben opened this issue 2 years ago • 3 comments

请将问题描述清楚,别耽误彼此打(xi)工(jiao)赚(nen)钱(mo)的时间. 如果能直接提供错误示例,请上传仓库或私发邮箱,对于私发邮箱的代码,承诺不泄露来源方信息(邮箱名,公司名等信息),阅后即焚.

mybatis-plus版本为3.5.2

​ 生成器版本:3.5.3

​ 数据库版本:

​ 模板引擎版本:

生成DTO/VO等自定义类时,类名和文件名问题

复现步骤:如:表名是user,new CustomFile.Builder().fileName("DTO.java"),当给实体定义后缀后,new StrategyConfig.Builder() .entityBuilder().formatFileName("%sEntity")时

错误结果:而实际生成的是:UserEntity和UserEntityDTO

期望结果:期望生成实体和DTO分别是UserEntity和UserDTO

yangjianben avatar Sep 29 '22 09:09 yangjianben

补充说明: 即使在自定义输出里定义了,也无法改变文件名 new InjectionConfig.Builder().beforeOutputFile((tableInfo, objectMap) -> { String entityDtoName = tableInfo.getEntityName().replace("Entity", "DTO"); String entityDaoName = tableInfo.getEntityName().replace("Entity", "DAO"); objectMap.put("entityDtoName", entityDtoName); objectMap.put("entityDaoName", entityDaoName); LOGGER.info("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size()); }).customFile(customFiles) .build()

yangjianben avatar Sep 29 '22 09:09 yangjianben

建议: 1、在TableInfo类增加原始实体名称字段(去除前缀未加后缀)

/** * 实体名称(未添加后缀) */ private String entityOriginName;

2、TableInfo#processTable方法给entityOriginName字段赋值entityName /** * 处理表信息(文件名与导包) * * @since 3.5.0 */ public void processTable() { this.entityOriginName = entity.getNameConvert().entityNameConvert(this); this.setEntityName(entity.getConverterFileName().convert(entityOriginName)); this.mapperName = strategyConfig.mapper().getConverterMapperFileName().convert(entityOriginName); this.xmlName = strategyConfig.mapper().getConverterXmlFileName().convert(entityOriginName); this.serviceName = strategyConfig.service().getConverterServiceFileName().convert(entityOriginName); this.serviceImplName = strategyConfig.service().getConverterServiceImplFileName().convert(entityOriginName); this.controllerName = strategyConfig.controller().getConverterFileName().convert(entityOriginName); this.importPackage(); }

3、AbstractTemplateEngine#outputCustomFile直接取tableInfo.getEntityOriginName() protected void outputCustomFile(@NotNull List<CustomFile> customFiles, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) { String entityName = tableInfo.getEntityOriginName(); String parentPath = getPathInfo(OutputFile.parent); customFiles.forEach(file -> { String filePath = StringUtils.isNotBlank(file.getFilePath()) ? file.getFilePath() : parentPath; if (StringUtils.isNotBlank(file.getPackageName())) { filePath = filePath + File.separator + file.getPackageName(); filePath = filePath.replaceAll("\\.", StringPool.BACK_SLASH + File.separator); } String fileName = filePath + File.separator + entityName + file.getFileName(); outputFile(new File(fileName), objectMap, file.getTemplatePath(), file.isFileOverride()); }); }

yangjianben avatar Sep 30 '22 02:09 yangjianben

@yangjianben 感谢建议,但是目前不考虑。你可以分两次生成解决这个问题,一个生成改实体类名的,一次生成自定义类

lanjerry avatar Sep 30 '22 09:09 lanjerry