首先,二级缓存默认不开启!
要配置 <setting name="cacheEnabled" value="true"/>
在MyBatis中:一级缓存会维护一个HashMap,key为hashcode+statementId+sql语句,Value为查询出来的结果集映射成的java对象。作用域是SqlSession
二级缓存是namespace级的,还需要在配置文件处配置缓存<cache/>
在配置了分布查询
1 <select id="selectEmployees" resultMap="emps"> 2 select id,last_name,gender,descr,did from employee 3 </select> 4 5 <resultMap type="com.atguigu.bean.Employee" id="emps"> 6 <id column="id" property="id"/> 7 <result column="last_name" property="lastName"/> 8 <result column="gender" property="gender"/> 9 <result column="descr" property="descr"/> 10 11 <association property="dept" select="com.atguigu.mapper.DepartmentMapper.selectDepartmentById" 12 column="{id=did}"></association> 13 </resultMap>
和延时加载后
1 <setting name="lazyLoadingEnabled" value="true"/> 2 <setting name="aggressiveLazyLoading" value="false"/>
此时刷新查询结果页面会报错
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 1 type Exception report 2 2 3 3 message javax.el.ELException: Error reading 'dept' on type com.atguigu.bean.Employee_$$_jvst6ff_0 4 4 5 5 description The server encountered an internal error that prevented it from fulfilling this request. 6 6 7 7 exception 8 8 9 9 org.apache.jasper.JasperException: javax.el.ELException: Error reading 'dept' on type com.atguigu.bean.Employee_$$_jvst6ff_0 10 10 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:561) 11 11 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477) 12 12 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 13 13 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 14 14 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 15 15 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 16 16 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209) 17 17 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266) 18 18 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225) 19 19 org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012) 20 20 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 21 21 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 22 22 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) 23 23 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822) 24 24 javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 25 25 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) 26 26 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 27 27 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 28 28 org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 29 29 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 30 30 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 31 31 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 32 32 root cause 33 33 34 34 javax.el.ELException: Error reading 'dept' on type com.atguigu.bean.Employee_$$_jvst6ff_0 35 35 javax.el.BeanELResolver.getValue(BeanELResolver.java:110) 36 36 org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104) 37 37 org.apache.el.parser.AstValue.getValue(AstValue.java:184) 38 38 org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) 39 39 org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:950) 40 40 org.apache.jsp.pages.list_jsp._jspx_meth_c_005fforEach_005f0(list_jsp.java:212) 41 41 org.apache.jsp.pages.list_jsp._jspService(list_jsp.java:124) 42 42 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 43 43 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 44 44 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) 45 45 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 46 46 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 47 47 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 48 48 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 49 49 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209) 50 50 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266) 51 51 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225) 52 52 org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012) 53 53 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 54 54 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 55 55 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) 56 56 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822) 57 57 javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 58 58 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) 59 59 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 60 60 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 61 61 org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 62 62 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 63 63 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 64 64 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 65 65 root cause 66 66 67 67 org.apache.ibatis.executor.ExecutorException: Cannot get Configuration as configuration factory was not set. 68 68 org.apache.ibatis.executor.loader.ResultLoaderMap$LoadPair.getConfiguration(ResultLoaderMap.java:220) 69 69 org.apache.ibatis.executor.loader.ResultLoaderMap$LoadPair.load(ResultLoaderMap.java:191) 70 70 org.apache.ibatis.executor.loader.AbstractEnhancedDeserializationProxy.invoke(AbstractEnhancedDeserializationProxy.java:78) 71 71 org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedDeserializationProxyImpl.invoke(JavassistProxyFactory.java:186) 72 72 com.atguigu.bean.Employee_$$_jvst6ff_0.getDept(Employee_$$_jvst6ff_0.java) 73 73 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 74 74 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 75 75 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 76 76 java.lang.reflect.Method.invoke(Method.java:498) 77 77 javax.el.BeanELResolver.getValue(BeanELResolver.java:99) 78 78 org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104) 79 79 org.apache.el.parser.AstValue.getValue(AstValue.java:184) 80 80 org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) 81 81 org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:950) 82 82 org.apache.jsp.pages.list_jsp._jspx_meth_c_005fforEach_005f0(list_jsp.java:212) 83 83 org.apache.jsp.pages.list_jsp._jspService(list_jsp.java:124) 84 84 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 85 85 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 86 86 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) 87 87 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 88 88 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 89 89 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 90 90 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 91 91 org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209) 92 92 org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266) 93 93 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225) 94 94 org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012) 95 95 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 96 96 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 97 97 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) 98 98 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822) 99 99 javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 100 100 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) 101 101 javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 102 102 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 103 103 org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 104 104 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 105 105 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 106 106 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 107 107 note The full stack trace of the root cause is available in the Apache Tomcat/7.0.79 logs.
这可能是由于第一次查询将结果保存在了二级缓存中,在第二次查询时,由于设置了延时加载,在缓存中只查到了Employee对应的属性,但对于第二步却无法一次性而出现问题。
解决办法是关闭二级缓存。
(问别人的时候还以为二级缓存默认开启真鸡儿丢人)