Commit 5faf6ea5449e9666b71e284d390f680bfc91ca8e
Committed by
GitHub
Merge pull request #1087 from SunJary/fix_codegen
fix: 字段中包含英文双引号时,生成的代码报错,将英文引号替换为中文引号,不改变原意
Showing
1 changed file
with
19 additions
and
2 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenBuilder.java
| ... | ... | @@ -117,8 +117,10 @@ public class CodegenBuilder { |
| 117 | 117 | table.setBusinessName(toCamelCase(subAfter(tableName, '_', false)).toLowerCase()); |
| 118 | 118 | // 驼峰 + 首字母大写;第一步,第一个 _ 前缀的后面,作为 class 名字;第二步,驼峰命名 |
| 119 | 119 | table.setClassName(upperFirst(toCamelCase(subAfter(tableName, '_', false)))); |
| 120 | + // 处理表注释中的特殊字符,避免生成的代码编译错误(先转义,再去掉"表"字) | |
| 121 | + String tableComment = sanitizeColumnComment(table.getTableComment()); | |
| 120 | 122 | // 去除结尾的表,作为类描述 |
| 121 | - table.setClassComment(StrUtil.removeSuffixIgnoreCase(table.getTableComment(), "表")); | |
| 123 | + table.setClassComment(StrUtil.removeSuffixIgnoreCase(tableComment, "表")); | |
| 122 | 124 | table.setTemplateType(CodegenTemplateTypeEnum.ONE.getType()); |
| 123 | 125 | } |
| 124 | 126 | |
| ... | ... | @@ -128,6 +130,8 @@ public class CodegenBuilder { |
| 128 | 130 | for (CodegenColumnDO column : columns) { |
| 129 | 131 | column.setTableId(tableId); |
| 130 | 132 | column.setOrdinalPosition(index++); |
| 133 | + // 处理字段注释中的特殊字符,避免生成的代码编译错误 | |
| 134 | + column.setColumnComment(sanitizeColumnComment(column.getColumnComment())); | |
| 131 | 135 | // 特殊处理:Byte => Integer |
| 132 | 136 | if (Byte.class.getSimpleName().equals(column.getJavaType())) { |
| 133 | 137 | column.setJavaType(Integer.class.getSimpleName()); |
| ... | ... | @@ -216,5 +220,18 @@ public class CodegenBuilder { |
| 216 | 220 | column.setExample(randomEle(new String[]{"你猜", "随便", "你说的对"})); |
| 217 | 221 | } |
| 218 | 222 | } |
| 219 | - | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * 清理字段注释中的特殊字符 | |
| 226 | + * 将英文引号替换为中文引号,避免生成的代码出现语法错误 | |
| 227 | + * | |
| 228 | + * @param comment 原始字段注释 | |
| 229 | + * @return 清理后的字段注释 | |
| 230 | + */ | |
| 231 | + private String sanitizeColumnComment(String comment) { | |
| 232 | + if (StrUtil.isEmpty(comment)) { | |
| 233 | + return comment; | |
| 234 | + } | |
| 235 | + return comment.replace("\"", "\u201c").replace("\"", "\u201d").replace("'", "\u2018").replace("'", "\u2019"); | |
| 236 | + } | |
| 220 | 237 | } | ... | ... |