MyBatis二级缓存开启之后并没有效果,求助。

ilovelizehui 2017-08-04 03:02:44
config.xml已开启
<setting name="cacheEnabled" value="true"/>

mapper.xml 已开启
<cache ></cache>

select语句
<select id="getById" resultType="User" useCache="true">
select * from m_user where id=#{id}
</select>

测试语句
public void testTwo() {
int begin,end;
SqlSession session1=SessionHelper.getSession();
UserMapper uMapper1=session1.getMapper(UserMapper.class);
begin=(int) System.currentTimeMillis();
User user1=uMapper1.getById(1);
end=(int) System.currentTimeMillis();
int x1=end-begin;
System.out.println("------------>"+user1.toString());
session1.close();

SqlSession session2=SessionHelper.getSession();
UserMapper uMapper2=session2.getMapper(UserMapper.class);
begin=(int) System.currentTimeMillis();
User user2=uMapper2.getById(1);
int x2=begin-end;
System.out.println("------------>"+user2.toString());
System.out.println(x1+"间隔事件对比"+x2);
session2.close();
}

log输出

DEBUG - Openning JDBC Connection
DEBUG - Created connection 31209237.
DEBUG - ooo Using Connection [com.mysql.cj.jdbc.ConnectionImpl@1dc3715]
DEBUG - ==> Preparing: select * from m_user where id=?
DEBUG - ==> Parameters: 1(Integer)
------------>id:1 username:update password:test
DEBUG - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1dc3715]
DEBUG - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1dc3715]
DEBUG - Returned connection 31209237 to pool.
DEBUG - Find JAR URL: file:/E:/EclipseEEWorkspace/MyBatisPractice1/bin/com/clei/domain
DEBUG - Not a JAR: file:/E:/EclipseEEWorkspace/MyBatisPractice1/bin/com/clei/domain
DEBUG - Reader entry: Course.class
DEBUG - Reader entry: Husband.class
DEBUG - Reader entry: Student.class
DEBUG - Reader entry: User.class
DEBUG - Reader entry: Wife.class
DEBUG - Listing file:/E:/EclipseEEWorkspace/MyBatisPractice1/bin/com/clei/domain
DEBUG - Find JAR URL: file:/E:/EclipseEEWorkspace/MyBatisPractice1/bin/com/clei/domain/Course.class
DEBUG - Cache Hit Ratio [com.clei.mapper.UserMapper]: 0.0
这还有一大段没复制过来。
DEBUG - Openning JDBC Connection
DEBUG - Created connection 19037581.
DEBUG - ooo Using Connection [com.mysql.cj.jdbc.ConnectionImpl@1227d8d]
DEBUG - ==> Preparing: select * from m_user where id=?
DEBUG - ==> Parameters: 1(Integer)
------------>id:1 username:update password:test
390间隔事件对比78
DEBUG - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1227d8d]
DEBUG - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1227d8d]
DEBUG - Returned connection 19037581 to pool.





...全文
718 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
头顶的风 2019-11-13
  • 打赏
  • 举报
回复
非常感谢楼主的分享,只需使用同一个SqlSessionFactory就能解决二级缓存失效的问题。
qq_28086033 2017-08-04
  • 打赏
  • 举报
回复
SessionHelper.getSession()的问题。工具类里每次都重新创建了一个sessionFactory,然后再用这个sessionFactory返回的session。改过之后让SessionHelper只有一个seesionFactory就行
「已注销」 2017-08-04
  • 打赏
  • 举报
回复
问题解决了。 因为SessionHelper.getSession()的问题。 SessionHelper类里每次都重新创建了一个sessionFactory,然后再用这个sessionFactory返回的session。 改过之后让SessionHelper只有一个seesionFactory就行。。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧