练习二级缓存的使用的时候遇到一些疑惑
一下是测试代码
@Test
public void testLv2Cache(){
SqlSession sqlSession=null;
try {
sqlSession=MyBatisUtils.openSession();
Goods goods=sqlSession.selectOne("goods.selectById",1603);
System.out.println(goods.hashCode());
} catch (Exception e) {
e.printStackTrace();
}finally {
MyBatisUtils.closeSession(sqlSession);
}
try {
sqlSession=MyBatisUtils.openSession();
Goods goods=sqlSession.selectOne("goods.selectById",1603);
System.out.println(goods.hashCode());
} catch (Exception e) {
e.printStackTrace();
}finally {
MyBatisUtils.closeSession(sqlSession);
}
}
第一运行的时候报错:Cause: java.io.NotSerializableException: com.boeh.mybatis.entity.Goods,Goods类没有序列化,于是对实体类Goods做了一定修改:public class Goods implements Serializable
,实现了 Serializable,
第二次执行程序时,执行成功了,但是goods前后的内存地址是不一样的,日志信息如下。而课上内存地址一致的结果,不理解为什么自己的运行结果内存地址不一致。
希望老师帮我解答
"C:\Program Files\Java\jdk1.8.0_211\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=55328:D:\IDEA\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar;D:\IDEA\IntelliJ IDEA 2019.1.3\plugins\junit\lib\junit-rt.jar;D:\IDEA\IntelliJ IDEA 2019.1.3\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\rt.jar;D:\IdeaProject\mybatis\target\test-classes;D:\IdeaProject\mybatis\target\classes;C:\Users\10220172\.m2\repository\org\mybatis\mybatis\3.5.1\mybatis-3.5.1.jar;C:\Users\10220172\.m2\repository\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar;C:\Users\10220172\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;C:\Users\10220172\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\10220172\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\10220172\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\10220172\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\10220172\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.boeh.mybatis.MybatisTest,testLv2Cache
[main] 15:57:12.012 DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
[main] 15:57:12.012 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[main] 15:57:12.012 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[main] 15:57:12.012 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[main] 15:57:12.012 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
[main] 15:57:12.012 DEBUG goods - Cache Hit Ratio [goods]: 0.0
[main] 15:57:12.012 DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
[main] 15:57:12.012 DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 1412794598.
[main] 15:57:12.012 DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@543588e6]
[main] 15:57:12.012 DEBUG goods.selectById - ==> Preparing: select * from babytun.t_goods where goods_id=?
[main] 15:57:12.012 DEBUG goods.selectById - ==> Parameters: 1603(Integer)
[main] 15:57:12.012 DEBUG goods.selectById - <== Total: 1
319558327
[main] 15:57:12.012 DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@543588e6]
[main] 15:57:12.012 DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@543588e6]
[main] 15:57:12.012 DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 1412794598 to pool.
[main] 15:57:12.012 DEBUG goods - Cache Hit Ratio [goods]: 0.5
33533830
EnumerateProviders catalog=0
EnumerateProviders totalPro=17
Process finished with exit code 0
正在回答
同学你好,当内存中不够用,需要内存中的对象保存到硬盘中时,或者对象需要使用套接字socket在网络中传输时,就需要序列化。所以当对象需要转换为可保持或传输的状态时,建议implement Serializable。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星