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

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

http://www.xdnf.cn/news/12038.html

相关文章:

  • 配置GDAL使用工具
  • window 显示驱动开发-提供视频解码功能(二)
  • 工业自动化DeviceNET从站转Ethernet/IP主站网关赋能冶金行业工业机器人高效运行
  • 网络编程之网络基础
  • VS Code 打开ipynb(还不会)运行python
  • 微服务面试资料1
  • CppCon 2015 学习:Benchmarking C++ Code
  • 【vibe coding解决100个问题】开发CRM管理系统, Augment/windsurf/bolt.new哪家强?
  • AtCoder-abc407_e解析
  • 【Blender Texture】【游戏开发】高质感 Blender 4K 材质资源推荐合集 —— 提升场景真实感与美术表现力
  • Vue跨层级通信
  • 2025-0604学习记录17——文献阅读与分享(2)
  • Anaconda全平台安装指南
  • PostgreSQL-安装-win10、win11安装pgsql16.1和timescaledb2.13.0(绿色免安装版本)
  • 开源库 API 化平台 (ALLBEAPI) - 让优秀工具触手可及!
  • 实验设计如何拯救我的 CEI VSR 28G 设计
  • ubuntu下libguestfs-tools
  • 电力系统时间同步系统之二
  • 我的概要设计模板(以图书管理系统为例)
  • [Css]等腰梯形
  • 如何在IDE中通过Spark操作Hive
  • Ant Design动态增加表单项
  • 使用Prometheus+Grafana+Alertmanager+Webhook-dingtalk搭建监控平台
  • simulink这边重新第二次仿真时,直接UE5崩溃,然后simulink没有响应
  • AReaL-boba²:开源异步强化学习训练系统的革命性突破
  • 【C/C++】进一步介绍idl编码
  • RAG系统中的Re-ranking引擎选择指南
  • BERT vs Rasa 如何选择 Hugging Face 与 Rasa 的区别 模型和智能体的区别
  • 前端面试总结
  • 【从0-1的HTML】第3篇:html引入css的3种方式