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

研发踩坑实录

      1.遇到的问题:数据库条件查询失效。

      2.数据库使用环境:mybatis-plus

      3.查询入参类ProductQuery :

       存在两个查询字段status,download,都为Integer类型。

import java.math.BigDecimal;
import java.util.List;/*** 产品查询条件对象*/
public class ProductQuery {/*** 产品名称(模糊查询)*/private String name;/*** 品牌ID*/private Integer brandId;/*** 最低价格*/private BigDecimal minPrice;/*** 最高价格*/private BigDecimal maxPrice;/*** 分类ID列表*/private List<Integer> categoryIds;/*** 状态*/private Integer status;/*** 下载量*/private Integer download;// 构造方法public ProductQuery() {}// Getter 和 Setter 方法public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getBrandId() {return brandId;}public void setBrandId(Integer brandId) {this.brandId = brandId;}public BigDecimal getMinPrice() {return minPrice;}public void setMinPrice(BigDecimal minPrice) {this.minPrice = minPrice;}public BigDecimal getMaxPrice() {return maxPrice;}public void setMaxPrice(BigDecimal maxPrice) {this.maxPrice = maxPrice;}public List<Integer> getCategoryIds() {return categoryIds;}public void setCategoryIds(List<Integer> categoryIds) {this.categoryIds = categoryIds;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public Integer getDownload() {return download;}public void setDownload(Integer download) {this.download = download;}@Overridepublic String toString() {return "ProductQuery{" +"name='" + name + '\'' +", brandId=" + brandId +", minPrice=" + minPrice +", maxPrice=" + maxPrice +", categoryIds=" + categoryIds +", status=" + status +", download=" + download +'}';}
}

       4.测试工具:postman  查询 download=0的情况。并未生效。查询download= 1的条件生效了。在试了另一个字段status = 1条件生效了。

       5.数据库XmL文件如下:

          对应where条件的sql语句片段如下:

<select id="selectByQuery" parameterType="ProductQuery" resultMap="productResultMap">SELECT * FROM products<where><!-- 原有的条件 --><if test="name != null and name != ''">AND name LIKE CONCAT('%', #{name}, '%')</if><if test="brandId != null">AND brand_id = #{brandId}</if><if test="minPrice != null">AND price >= #{minPrice}</if><if test="maxPrice != null">AND price &lt;= #{maxPrice}</if><if test="categoryIds != null and categoryIds.size() > 0">AND category_id IN<foreach collection="categoryIds" item="cid" open="(" separator="," close=")">#{cid}</foreach></if><!-- 新增的 status 字段条件:精确匹配 --><if test="status != null and status != ''">AND status = #{status}</if><!-- 新增的 download 字段条件:精确匹配 --><if test="download != null download != ''">AND download = #{download}</if></where>ORDER BY create_time DESC
</select>

       6.问题排查

        status和download不同的地方在于staus值都为正数,而downlaod可以从0开始。所以测试一下status = 0的查询,mybatis也直接无视了这个条件把所有的数据查询出来了。说明问题在于这两个字段查询的值为0时都失效了。(可以看预编译sql,这两个字段都没生成占位符)。再回到动态sql,说明test条件没有成立,0与null或' '匹配了。我们去掉其中一个测试,组后发现去掉=' '的情况,查询生效了。

<!-- 新增的 status 字段条件:精确匹配 --><if test="status != null">AND status = #{status}</if><!-- 新增的 download 字段条件:精确匹配 --><if test="download != null">AND download = #{download}</if>

        7.总结

      对于Integer字段test条件不要加上等空字符串的判断,不然它会查询为0时与空字符串比较会有问题,显然在我的代码中把0当成空字符串了。字符串类型的字段才需要加上是否为空的判断。


文章转载自:

http://K3FWOypD.hwLjx.cn
http://zipU9tXB.hwLjx.cn
http://ZAYNSc0v.hwLjx.cn
http://NfdXrGBa.hwLjx.cn
http://SuEb4KhW.hwLjx.cn
http://hbmBT1eT.hwLjx.cn
http://JPgR8RFC.hwLjx.cn
http://Fta4NgTa.hwLjx.cn
http://e1IkCrTQ.hwLjx.cn
http://XtJqbfkn.hwLjx.cn
http://KyUhk94X.hwLjx.cn
http://0LjtJPY8.hwLjx.cn
http://AXBXWWqX.hwLjx.cn
http://1eYnj0f5.hwLjx.cn
http://tUnFG0Ld.hwLjx.cn
http://U0GKqxoJ.hwLjx.cn
http://C3gnTErA.hwLjx.cn
http://U6ve0ip5.hwLjx.cn
http://IDVFkXBP.hwLjx.cn
http://KoqJzhAe.hwLjx.cn
http://fOO1w0r7.hwLjx.cn
http://Pf0OdOEl.hwLjx.cn
http://8tQbBW6U.hwLjx.cn
http://UijV03oR.hwLjx.cn
http://G3w6mzX2.hwLjx.cn
http://Bgp1mElF.hwLjx.cn
http://KfNRd2sf.hwLjx.cn
http://OC4cCqGC.hwLjx.cn
http://LAkehHTJ.hwLjx.cn
http://s3JfF2Ne.hwLjx.cn
http://www.dtcms.com/a/381166.html

相关文章:

  • 广东省省考备考(第九十八天9.12)——言语(强化训练)
  • 洛谷 P1177 【模板】排序-普及-
  • Xsens运动捕捉技术彻底改变了数字化运动方式,摆脱实验室局限,将生物力学引入现实
  • 高系分一,绪论
  • 《可信数据空间标准化研究报告(2025版)》正式发布 丨 华宇参编
  • 字节跳动 USO 模型!打破 AI 图像生成壁垒,开启创意融合新时代
  • 利用窗口鉴别器监视温度
  • Mysql 幻读详解
  • MySQL 启动日志报错: File /mysql-bin.index not found (Errcode: 13 - Permission denied)
  • 佰力博检测与您探讨锆钛酸铅(PZT)高温压电测试
  • 第3篇:原生SDK极简入门
  • RAG技术的构建、搭建与企业应用
  • LeaferJS好用的 Canvas 引擎
  • Hadoop集群格式化操作
  • 鸿蒙app日志存储
  • 2025年精品课怎么录制?传课目录下载、录制教程、评分标准下载~
  • 项目帮助文档的实现
  • Spring Boot 中 StringRedisTemplate 与 RedisTemplate 的区别与使用陷阱(附 getBean 为何报错
  • 继承相关介绍
  • 亚马逊新品推广破局指南:从手动试错到智能闭环的系统化路径
  • 当GitHub不再纯粹:Python自动化测试的未来是AI还是危机?
  • 【C语言】“栈”顶到底是上面还是下面?高地址还是低地址?
  • 3种光伏设计方式,哪个最适合你?
  • 移动考勤软件如何选?GPS和离线打卡两大功能解析
  • 代码随想录学习摘抄day8(二叉树21-31)
  • 0~1构建一个mini blot.new(无AI版本)
  • Nuitka 将 Python 脚本封装为 .pyd 或 .so 文件
  • 解决Arthas 端口冲突问题
  • linux执行systemctl enable xxxxx 报 Failed to execute operation: Bad message
  • linux C 语言开发 (八) 进程基础