【问题标题】:Strange questions about comparing the Integer result of Spring DAO关于比较Spring DAO的Integer结果的奇怪问题
【发布时间】:2017-09-24 08:26:21
【问题描述】:

我有一个奇怪的问题。

支持实体:

public class Sup implements Serializable {
    private static final long serialVersionUID = 5898846627874143178L;
    private Integer state;

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }
}

代码1:

 @Override
public void test() {
        for (int i = 0; i < 10; i++) {
            //Just a simple query sql,the result is always the same
            //and the state of 'sup' is always 0;
            Sup sup = supDao.querySupByid(40017);
            Integer state = sup.getState();
            System.out.println("my state:" + state);
            if (state!=Integer.valueOf(0)) {
                System.out.println("     ["+ i + "]>>>>0!=0");
            }
        }
}

querySupByid 方法:

@Select("SELECT state FROM TBL_SUP WHERE ID=#{supId} ")
Sup querySupByid(@Param("supId")Integer sup);

部分结果如下:

  • 我的状态:0
  • 我的状态:0
  • [1]>>>>0!=0
  • 我的状态:0
  • [2]>>>>0!=0
  • 我的状态:0
  • [3]>>>>0!=0

....

我预期的结果是“我的状态:0”的 10 个。 正如你所看到的,大多数结果都认为第一个循环都是错误的(state!=Integer.valueOf(0) 因为状态总是 0 所以这里应该总是假的),接下来我改变了一些这样的代码

代码2:

@Override
public void test() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Sup sup = supDao.querySupByid(40017);
            Integer state = sup.getState();
            System.out.println("my state:" + state);
            TimeUnit.SECONDS.sleep(5);
            if (state!=Integer.valueOf(0)) {
                System.out.println("     ["+ i + "]>>>>0!=0");
            }
        }
}

在每个循环中我暂停 5 秒,结果变成了这样:

  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0
  • 我的状态:0

显然我终于得到了正确的结果; 接下来我再次更改代码;

代码3:

@Override
public void test() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Sup sup = new Sup();//supDao.querySupByid(40017);
            sup.setState(0);
            Integer state = sup.getState();
            System.out.println("my state:" + state);
            if (state!=Integer.valueOf(0)) {
                System.out.println("     ["+ i + "]>>>>0!=0");
            }
        }
}

在Code3中结果也是正确的,我再次更改了代码:

代码4:

@Override
public void test() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Sup sup =supDao.querySupByid(40017);
            Integer state = sup.getState();
            System.out.println("my state:" + state);
            if (!state.equals(Integer.valueOf(0))) {
                System.out.println("     ["+ i + "]>>>>0!=0");
            }
        }
}

与 Code3 的结果相同,但我无法找出合理的解释。

我发现,结果是缓存造成的;第一个结果已经序列化到缓存,后面从缓存中得到的结果已经反序列化了。所以后面的结果不是“==”前面的结果,什么愚蠢的错误

【问题讨论】:

  • “如您所见,大多数结果都是错误的” - 不,我不明白为什么该结果是错误的,您也没有解释什么是“好”的结果。您正在获取 10 次相同的实体,并且所有这些实体的状态都是相同的,在您的情况下为 0。我看不出它在迭代中会有什么不同。
  • 比较盒装图元时,始终使用equals,而不是==!=。后者测试对象身份。
  • 解释在于sup.getState()supDao.querySupByid(40017)的实现。我们需要看到这一点。

标签: java spring integer equals dao


【解决方案1】:

不要将Integer 对象与==!= 进行比较。这些运算符将检查两个对象是否相同,而不是它们是否具有相同的值,并且不能保证valueOf 对于给定值总是返回相同的Integer 对象。始终将 equals 用于 Integer 对象。

【讨论】:

    【解决方案2】:

    为了比较,请使用Integer.compare(int,int)0 的结果意味着相等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多