欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > mybatis 参数绑定错误示范(1)

mybatis 参数绑定错误示范(1)

2025/6/7 7:14:32 来源:https://blog.csdn.net/u011189668/article/details/148421542  浏览:    关键词:mybatis 参数绑定错误示范(1)

采用xml形式的mybatis

错误示例:

server伪代码为:

            Map<String, Object> findMapNew = MapUtil.<String, Object>builder().put("applyUnit", appUnit).put("planYear", year  != null ? year : -1).put("code", code).build();List<YearPlan> planList= planDao.checkByMap(findMapNew);

xml

selectt.*from plan_year t<where>t.del_flag = 0 and t.status = #{status}<if test="applyUnit != null and applyUnit != '' ">and t.apply_unit = #{applyUnit}</if>            <if test="planYear != null ">and t.plan_Year = #{planYear}</if><if test="code != null and code != '' ">and t.code = #{code}</if>    </where>         

此时,映射的最终sql如下:

selectt.*from plan_year twheret.del_flag = 0 and t.status = '111111222222'  ## 单位idand t.apply_unit = 2025	## 年份and t.plan_Year = 'AABBCCDD'	## CODE

最终报错类型错误

解决方案:
修改xml:
xml

selectt.*from plan_year t<where>t.del_flag = 0 <if test="status != null ">and t.status = #{status}</if>           <if test="applyUnit != null and applyUnit != '' ">and t.apply_unit = #{applyUnit}</if>            <if test="planYear != null ">and t.plan_Year = #{planYear}</if><if test="code != null and code != '' ">and t.code = #{code}</if></where>           

最终执行正确的sql如下:

selectt.*from plan_year twheret.del_flag = 0 and t.apply_unit = '111111222222'  ## 单位idand t.plan_Year = 2025	## 年份and t.CODE = 'AABBCCDD'	## CODE

原因分析

由于第一次没有对status参数进行判空,导致mybatis在替换参数时用了顺序执行的原则,导致错误产生。
后面修修改了这个错误,进行所有参数进行了判空,正确替换了在server层定义的参数。最终sql按照预期执行。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词