再实体类中,定义一个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