使用mybatis 操作mysql 传参为list列表类型
我的写法,一般使用注解的方式,@Select("<script>"+ "select id, name from STATION where id in"+ "<foreach collection='idlist' item='item' index='index' open='(' separator=',' close=')'>"+ "#{item}"+ "</
·
我的写法,一般使用注解的方式,
@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
- <select id="findByIdsMap" resultMap="BaseResultMap">
- Select
- <include refid="Base_Column_List" />
- from jria where ID in
- <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
- #{item}
- </foreach>
- </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')

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