一、無法生成注釋或生成的注釋是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) }