一、无法生成注释或生成的注释是null
问题可能的原因:
1.没有从表里提取注释信息,修改def calcFields(table)方法即可
def calcFields(table) {DasUtil.getColumns(table).reduce([]) { fields, col ->def spec = Case.LOWER.apply(col.getDataType().getSpecification())def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.valuefields += [[column : col.getName(),name : javaName(col.getName(), false),comment: col.getComment(),type : typeStr,annos: ""]]}
2.注释判断部分有问题,以下提供正确的方法
fields.each() {// 修正拼写错误,并使用安全导航操作符和真值判断if (it.comment?.trim() != "") { // 使用 trim() 去除可能的空白字符out.println "\t/**"out.println "\t * ${it.comment}"out.println "\t */"}if (it.annos != "") {out.println " ${it.annos}"}out.println " private ${it.type} ${it.name};"
}
二、生成出的注释是乱码,IDEA提示可能是GBK字符
解决方案:指定生成文件的编码格式,修改def generate(table, dir) 不分
def generate(table, dir) {def className = javaName(table.getName(), true)def fields = calcFields(table)new File(dir, className + ".java").withPrintWriter("UTF-8") { out -> generate(out, className, fields) }