mybatis相同的sql查询第二次查不出结果问题

2022-07-18,,,

目录
  • 相同的sql查询第二次查不出结果问题
    • 问题分析
    • 问题探索
    • mybatis缓存介绍
    • 问题原因
    • 解决方案
  • mybatis条件查询容易遇见的错误
    • 一不小心就容易出现这个错误
    • 两种解决方法

相同的sql查询第二次查不出结果问题

问题分析

今天在做公司业务,大致是用户传来订单号(买卡)和手机号对其进行卡绑定,其中如果已经操作过则返回操作后的状态,问题出现在如果是项目启动第一次访问,会正常返回结果,第二次访问时会报找不到卡信息的错误,但是代码是同一个,查询语句也一样,为什么会出现这种情况?找遍百度,未果,自己摸索着寻找问题。

问题探索

首先对查询方法进行了排除,未果;然后寻找代码中问题,发现遍历卡信息(可找到多张卡)时,对于已经操作过的会进行remove()操作,注释掉这行,程序有结果,但是会重新更新卡信息,不是需要的结果;最后发现是对list进行remove()操作时都会出错,于是联想到了mybatis的缓存。

mybatis缓存介绍

一级缓存:即session缓存,作用域为 session,当 session flush 或 close 之后,该session中的所有 cache 就将清空,默认开启。注意:集成spring(使用mybatis-spring)时:

每次查询spring会重新创建sqlsession,所以一级缓存是不生效的;

而当开启事务时,spring会使用同一个sqlsession做查询,所以这个情况下一级缓存是生效的。

二级缓存:即全局缓存,其作用域为 mapper(namespace),默认关闭。

问题原因

上述提到了mybatis的缓存机制,查看项目配置后发现问题在于第一次查询到结果会放到缓存中,程序对查到的结果list进行了remove操作,所以缓存中的list会发生变化,第二次查询时会从缓存中将操作过的list查找出来(mybatis返回的实体类的内存地址是相同的),故而产生了我们不需要的结果,之前项目中使用的是与spring集成的,使用的session是sqlsessiontemplate,这里是默认关闭了一级缓存,而今天项目中没有与spring集成,创建session使用的是sqlsessionfactory的opensession()方法,这里查找时默认会先从缓存中查询,综上,第二次我们查到的只是缓存中的数据[2]。

解决方案

既然第二次会从缓存中读数据,不可能修改项目配置关闭一级缓存,所以可以通过刷新缓存来达到我们所需要的目的

方案一:通过sqlsessionutils.getsqlsession(sqlsessionfactory).clearcache()方法刷新缓存

方案二:在mapper.xml对应的查找语句中添加flushcache="true"

<select id="selectcardinfobyorderid" resulttype="xxx" parametertype="java.util.map" flushcache="true"></select>

mybatis条件查询容易遇见的错误

在使用mybatis的条件查询时,

一不小心就容易出现这个错误

19-dec-2017 16:04:38.742 严重 [http-nio-8090-exec-6] org.apache.catalina.core.standardwrappervalve.invoke servlet.service() for servlet [springmvc] in context with path [] threw exception [request processing failed; nested exception is org.mybatis.spring.mybatissystemexception: nested exception is org.apache.ibatis.reflection.reflectionexception: there is no getter for property named 'endoscopetype' in 'class java.lang.string'] with root cause org.apache.ibatis.reflection.reflectionexception: there is no getter for property named 'endoscopetype' in 'class java.lang.string'at org.apache.ibatis.reflection.reflector.getgetinvoker(reflector.java:380)at org.apache.ibatis.reflection.metaclass.getgetinvoker(metaclass.java:170)at org.apache.ibatis.reflection.wrapper.beanwrapper.getbeanproperty(beanwrapper.java:152)at org.apache.ibatis.reflection.wrapper.beanwrapper.get(beanwrapper.java:48)

这是mapper.xml文件:

<select id="findbyendoscopetype" resultmap="baseresultmap" parametertype="java.lang.string">
    select *
    from endoscope
    <where>
        <if test="endoscopetype != null">
            endoscope_type = #{endoscopetype,jdbctype=varchar}
        </if>
    </where>
</select>

出错的原因是因为加上 <if> 标签时,endoscope属性没有包含在string endoscopetype中

两种解决方法

1.将<if>标签去掉;

2.传入参数放在对象中传进来;

<select id="findbyendoscopetype" resultmap="baseresultmap" parametertype="com.iel.endoscope.entity.endoscope">
    select *
    from endoscope
    <where>
        <if test="endoscopetype != null">
            endoscope_type = #{endoscopetype,jdbctype=varchar}
        </if>
    </where>
</select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

《mybatis相同的sql查询第二次查不出结果问题.doc》

下载本文的Word格式文档,以方便收藏与打印。