我的写法,一般使用注解的方式,

@Select("<script>"
        + "select id, name from STATION where id in"
        + "<foreach collection='idlist' item='item' index='index' open='(' separator=',' close=')'>"
        + "#{item}"
        + "</foreach>"
        + "</script>")
@Results(value = {@Result(column = "id",property = "id")})
List<StationDeleteVo> getNameByIdList(@Param("idlist") ArrayList<Integer> idlist);

亲测有效,适用mysql,其他数据库还没测试。

 

下面是参考的文章

 

.1 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list

  1. <select id="findByIdsMap" resultMap="BaseResultMap">  
  2.  Select  
  3.  <include refid="Base_Column_List" />  
  4.  from jria where ID in  
  5.  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
  6.   #{item}  
  7.  </foreach>  
  8. </select>  

2.in查询条件是枚举值时

默认下,使用select xxx where in(xx,xx)查询,返回结果是按主键排序的,如果要按in()中值的排列顺序,可以这样做:

 

select * from talbe where id in(3,2,4,1) ORDER BY FIND_IN_SET( id, '3,2,4,1') 

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐