实体类中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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月17日 15时30分40秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
移动web开发之流式布局(一)
2019-04-26
移动web开发之流式布局(二)
2019-04-26
模块化开发规范
2019-04-26
webpack入门介绍(一)
2019-04-26
webpack入门介绍(二) --- loaders
2019-04-26
webpack入门介绍(三) ---plugins
2019-04-26
关于 Git 的基础知识可能你还不知道
2019-04-26
mysql-基础
2019-04-26
python3-basic
2019-04-26
测试方法(1)
2019-04-26
python3-django
2019-04-26
功能测试(1)
2019-04-26
安全性测试(1)
2019-04-26
html基础
2019-04-26
vi—终端中的编辑器
2019-04-26
Linux
2019-04-26
jmeter-性能测试基础
2019-04-26
unittest
2019-04-26
错误推断法-维护中
2019-04-26
AJAX教程
2019-04-26