实体类中int型变量为0,mybatis配置文件判断为空,解决办法
发布日期:2021-10-02 10:57:03 浏览次数:29 分类:技术文章

本文共 1316 字,大约阅读时间需要 4 分钟。

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

转载地址:https://blog.csdn.net/jiangyunlovec/article/details/78967322 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql 查看数据库中的存储过程,表,函数,以及创建的源码
下一篇:jdbc链接mysql数据库

发表评论

最新留言

不错!
[***.144.177.141]2024年04月17日 15时30分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章