實體類中int型變數為0,mybatis配置檔案判斷為空,解決辦法
阿新 • • 發佈:2019-02-03
再實體類中,定義一個int型別的欄位,容易出現預設值的情況,建議解決辦法,將int改為Integer,那麼變數可為空
如:
public class Pojo { int a; Integer b; public int getA() { return a; } public void setA(int a) { this.a = a; } public Integer getB() { return b; } public void setB(Integer b) { this.b = b; } @Override public String toString() { return "Pojo [a=" + a + ", b=" + b + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + a; result = prime * result + ((b == null) ? 0 : b.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pojo other = (Pojo) obj; if (a != other.a) return false; if (b == null) { if (other.b != null) return false; } else if (!b.equals(other.b)) return false; return true; } }
測試類
public class TestMain {
public static void main(String[] args) {
Pojo pojo = new Pojo();
System.out.format("Intger和int的區別%s", pojo.toString());
}
}
輸出結果
Intger和int的區別Pojo [a=0, b=null]
因為java中int的類變數預設值為0,物件的預設值為null,在方法中因為int的預設值沒有,int為基本型別,而Integer為物件型別,因此出現差異,所以定義變數時需要給初始值。
在mybaits中自動生成配置問檔案容易出現下面情況
<if test="queryObj.userId!=null">
and
user_id like concat('%',#{queryObj.userId},'%')
</if>
如果,使用int定義變數將不能如此判斷,可將int改為Integer