How to pass an Integer Array to IN clause in MyBatis

前端 未结 4 1341
旧巷少年郎
旧巷少年郎 2020-12-01 05:39

There is a query in my Mybatis containing an IN clause which is basically a set of Id\'s ( Integers)

I am now stuck on how can I pass an Integer array to this IN cla

4条回答
  •  被撕碎了的回忆
    2020-12-01 06:13

    YES, you can do that using annotations.

    If you're using postgresql, you can do like in this post.

    If you're using MySQL try this changes in your code sample:

    Mybatis Method using Annotations

    @Select(SEL_QUERY)
        @Results(value = {@Result(property="id",column="ID")})
        List getIds(@Param("usrIds") String usrIds);
    

    Query (using MySQL)

    select distinct ID from table a where FIND_IN_SET( a.id, #{usrIds}) <> 0
    

    Method call

    Integer[] arr = new Integer[2];
    arr[0] = 1;
    arr[1] = 2;
    
    String usrIds= "";
    for (int id : ids) {
        usrIds += id + ",";
    }
    
    mapper.getIds(usrIds) 
    

提交回复
热议问题