在使用 mybaits 进行 in 查询时,传入String,如1,2,3,发现查询的结果并非我们想要的
这是因为#{}编译完自动加双引号“” 也就是变成in (“1,2,3”)
如果想要获得我们想要的结果,可以使用${},编译完是这样的 in (1,2,3)
例如,查询铃音库中多首铃音的总数量
<select id="getProgsResourceCount" resultType="java.lang.Long"
parameterType="com.progandresource.entity.ProgsResourceCond">
select count(ring_no)
from progandresmanage_ringinfo where valid_day > now()
<if test="ringNo != '' and ringNo != null">
and ring_no in (${ringNo})
</if>
</select>
如果传入参数是List或者Array,则直接用foreach即可
例如
<select id="getProgsResourceCount" resultType="java.lang.Long" parameterType="java.util.List">
select count(ring_no)
from progandresmanage_ringinfo where valid_day > now() and ring_no in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item, jdbcType=VARCHAR}
</foreach>
DEMO
请求:
接收参数变量:
XML: