首页 新闻 会员 周边

Mybatis二级缓存得到的对象hashCode相同,但使用==号得到的结果为false,使用二级缓存得到的对象是否相等?

0
[待解决问题]

SqlSessionFactory sqlSessionFactory = SqlSessionUtils.getSqlSessionFactory();

    SqlSession sqlSession = sqlSessionFactory.openSession();
    SqlSession sqlSession2 = sqlSessionFactory.openSession();


    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    User userById = mapper.findUserById(42);
    System.out.println(userById.hashCode()); //936580213
    sqlSession.close();

    UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
    User userById2 = mapper2.findUserById(42);
    System.out.println(userById.hashCode()); //936580213
    sqlSession2.close();

    System.out.println(userById == userById2); //false![](https://img2020.cnblogs.com/q/2086570/202007/2086570-20200711090739850-1635366982.png)
木禾兄的主页 木禾兄 | 初学一级 | 园豆:196
提问于:2020-07-11 09:06
< >
分享
所有回答(1)
0

要比较是否相同的话,要实现自己的hashCode()和equals()方法。
另外,还注意类加载器是不是相同。

何德海 | 园豆:481 (菜鸟二级) | 2020-07-13 14:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册