本项目使用的是JFinal3.1的版本,在使用Generator创建生成Model的时候,调用默认的Dao()方法,使用IDEA调试的时候,发现会出现
Method threw 'java.lang.RuntimeException' exception. Cannot evaluate com.feizhou.log.model.LogInfo.toString() 异常信息,
虽然不影响使用,但是作为一个严谨的程序猿,我决定改掉这个
首先BaseModelGenerator加上toString模板信息
作如下修改:
1:增加toString模板
protected String toStringTemplate = "\tpublic String toString() {%n" + "\t\treturn %s;%n" + "\t}%n%n";
2:修改genBaseModelContent方法
protected void genBaseModelContent(TableMeta tableMeta) { StringBuilder ret = new StringBuilder(); genPackage(ret); genImport(ret); genClassDefine(tableMeta, ret); StringBuilder retToStr = new StringBuilder(); for (ColumnMeta columnMeta : tableMeta.columnMetas) { genSetMethodName(columnMeta, ret); genGetMethodName(columnMeta, ret); String attrName = columnMeta.attrName; String getterMethodName = "get" + StrKit.firstCharToUpperCase(attrName); retToStr.append("\"" + attrName + ":\"" + " + " + "this." + getterMethodName + "()" + " + \"" + ", \"" + "+"); } String toString = String.format(toStringTemplate, retToStr.toString().substring(0, retToStr.toString().length() - 1)); ret.append(toString); ret.append(String.format("}%n")); tableMeta.baseModelContent = ret.toString(); }
3:输出BaseModel如下
package com.feizhou.log.model.po; import com.jfinal.plugin.activerecord.Model; import com.jfinal.plugin.activerecord.IBean; /** * Generated by JFinal, do not modify this file. */ @SuppressWarnings({"serial", "unchecked"}) public abstract class BaseLogUser<M extends BaseLogUser<M>> extends Model<M> implements IBean { public M setId(java.lang.Long id) { set("id", id); return (M)this; } public java.lang.Long getId() { return get("id"); } public M setUserName(java.lang.String userName) { set("user_name", userName); return (M)this; } public java.lang.String getUserName() { return get("user_name"); } public M setPassword(java.lang.String password) { set("password", password); return (M)this; } public java.lang.String getPassword() { return get("password"); } public String toString() { return "id:" + this.getId() + ", "+"userName:" + this.getUserName() + ", "+"password:" + this.getPassword() + ", "; } }