当前位置: 首页 > news >正文

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按照预期执行。

相关文章:

  • AWS DocumentDB vs MongoDB:数据库的技术抉择
  • PostgreSQL的扩展 pg_buffercache
  • 第5篇《中间件负载均衡与连接池管理机制设计》
  • 银行用户评分规则 深度学习
  • Linux容器篇、第一章_01Linux系统下 Docker 安装与镜像部署全攻略
  • 分布式爬虫代理IP使用技巧
  • C++性能优化指南
  • go的工具库:github.com/expr-lang/expr
  • 突破数据孤岛:StarRocks联邦查询实战指南
  • 【发布实录】云原生+AI,助力企业全球化业务创新
  • Java设计模式:责任链模式
  • Linux 特殊权限位详解:SetUID, SetGID, Sticky Bit
  • 数据结构第一章
  • 【RAG优化】rag整体优化建议
  • [ Qt ] | 与系统相关的操作(二):键盘、定时器、窗口移动和大小
  • 跟着deepseek浅学分布式事务(2) - 两阶段提交(2PC)
  • yum更换阿里云的镜像源
  • 保险丝选型
  • 树莓派系列教程第九弹:Cpolar内网穿透搭建NAS
  • 云数据库选型指南:关系型 vs NoSQL vs NewSQL的企业决策
  • 网站前端代码有哪些问题/公司官网搭建
  • 济南润滑油网站制作/网站建设公司好
  • 视频制作软件有哪些/新乡seo推广
  • 安陆做网站公司/中山疫情最新消息
  • 黑龙江做网站找谁/网络优化工程师有前途吗
  • 网站面包屑导航设计特点/太原做网站的工作室