关于 mapper.xml 中 sql使用 in 执行无效的原因

1.SQL

xml 复制代码
	<select id="getList" resultType="com.xxx.xxx.front.response.ConvertList">
        select
        	*
        from
        	store_product
        where
        	1=1
        <if test="cateIdList != null">
            and cate_id in
            <foreach collection="cateIdList" item="cateIdList" index="index" open="(" separator="," close=")">
                #{cateIdList}
            </foreach>
        </if>
    </select>

2.最终执行的sql拼接如下

  and cate_id in ('753','753')

3.解决方案

  in ('753','753') 这样的sql在数据库执行之后是查不到任何数据的,即便cate_id本身的确是字符串也是一样。in()不能比较字符,如果要比较字符还是得使用like或者or。所以只能将入参改成 Integer 最终拼接为 in (753,753) 才能查出数据

相关推荐
予昊41 分钟前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_962203511 小时前
【SpringAI入门】初识SpringAI
java
weixin_461408581 小时前
Mybatis-flex小记
java·开发语言·mybatis
2601_963282771 小时前
工程项目、政企采购为什么优先选择对讲机批量采购?
数据库
Doraemomo1 小时前
SQLite数据库
数据库·sqlite
2601_962177132 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
云贝教育-郑老师2 小时前
MySQL 的“黑匣子“:Performance Schema 把数据库内部变成一张可查的表
数据库·mysql
それども2 小时前
IDEA接入Claude完整流程
java·ai·intellij-idea
小鹿的周先生2 小时前
Spring-AI-第2篇-ChatClient 实战:使用 DeepSeek 完成第一次 AI 对话
java·人工智能·spring
悲且狂3 小时前
SpringBoot项目改造注意事项(旧项目框架复用)
java·spring boot·后端