freemarker檔案無法讀取java boolean型別資料
阿新 • • 發佈:2020-08-23
freemarker檔案無法讀取java boolean型別資料
用freemarker自動生成mapper.xml mybatis配置檔案 但是在 <#if params.isPkey> 一直報 異常
嚴重: Error executing FreeMarker template FreeMarker template error: For "#if" condition: Expected a boolean, but this has evaluated to a method+sequence (wrapper: f.e.b.SimpleMethodModel): ==> params.islikeQuery [in template "mapperxml.ftl" at line 8, column 22] ---- Tip: Maybe using obj.something() instead of obj.something will yield the desired value ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #if params.islikeQuery [in template "mapperxml.ftl" at line 8, column 17] ---- Java stack trace (for programmers): ----
程式碼如下:
<resultMap id="BaseResultMap" type=" ${entryClassPath?if_exists}.${entryName}"> <#list columnList as params> <#if params.isPkey> <id column="${params.fieldName}" property="${params.entryName}"/> <#else> <result column="${params.fieldName}" property="${params.entryName}"/> </#if> <result column="${params.fieldName}" property="${params.entryName}"/> </#list> </resultMap>
開始以為ftl不支援boolean型別 然後就用了下面的程式碼 發現還是報錯
${params.isPkey?string('true','false')}
後來經過反覆檢查 檢查實體物件封裝的時候發現 在set get 時 java會自動的將 isPkey 欄位會將生成set get方法時,
錯誤的程式碼:
public boolean isPkey() {
return isPkey;
}
public void setPkey(boolean isPkey) {
this.isPkey = isPkey;
}
發現 因為get set不規範,不能使用is 修改get方法就可以了
正確的程式碼:
public boolean getIsPkey() {
return isPkey;
}
public void setPkey(boolean isPkey) {
this.isPkey = isPkey;
}