实体类中int型变量为0,mybatis配置文件判断为空,解决办法

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值