有时候我们springboot用的mybatis想实现in查询,它的mapper和xml怎么写呢,如下举个例子:
Mapper
public List<Map<String,Object>> getPRNByTopicids(@Param("ids")List<Long> ids, @Param("visible")String visible);
这里传入的是一个List。
XML
<select id="getPRNByTopicids" resultType="java.util.Map"> SELECT cast(id as char) as id,hot,replynum,pageview FROM topic <where> id in <foreach item="topicid" index="index" collection="ids" open="(" separator="," close=")"> #{topicid}</foreach> <if test="visible != ''"> AND visible=#{visible} </if> </where> </select>
这样就可以啦,主要是用foreach标签。
cast(id as char) as id 的原因是因为该id是int型有19位,直接回传给页面会确实精度,后面两位会变为0.