18

I'm using iBATIS to create select statements. Now I would like to implement the following SQL statement with iBATIS:

SELECT * FROM table WHERE col1 IN ('value1', 'value2');

With the following approach, the statement is not prepared correctly and no result returns:

SELECT * FROM table WHERE col1 IN #listOfValues#;

iBATIS seems to restructure this list and tries to interpret it as a string.

How can I use the IN clause correctly?

guerda
  • 23,388
  • 27
  • 97
  • 146

6 Answers6

32

Here's a blog post that answers your question:

iBatis: Support for Array or List Parameter with SQL IN Keyword

<select id="select-test" resultMap="MyTableResult" parameterClass="list">
select * from my_table where col_1 in
  <iterate open="(" close=")" conjunction=",">
   #[]#
  </iterate>
</select>

And in Java you should pass in a java.util.List. E.g.

List<String> list = new ArrayList<String>(3);
list.add("1");
list.add("2");
list.add("3");
List objs = sqlMapClient.queryForList("select-test",list);
vsingh
  • 6,365
  • 3
  • 53
  • 57
jitter
  • 53,475
  • 11
  • 111
  • 124
  • all this xml makes me want to vomit, but thx for the info very helpful for a project where I have to use ibatis – Rick Dec 09 '11 at 20:04
  • 3
    I can't believe I just upvoted an iBatis question/answer. When will this project be over, and I can stop using iBatis? – Steve Duitsman Dec 05 '12 at 15:49
  • 1
    Nah, iBatis is fantastic. At least you get to write SQL instead of some other abomination. – WW. Dec 30 '14 at 00:59
  • Could you please read my similar post, thanks !: http://stackoverflow.com/questions/28343006/how-to-create-an-sql-in-clause-in-ibatis-2-for-an-sql-update-or-sql-select-state –  Feb 05 '15 at 14:09
12

How about

<select id="foo" parameterClass="Quuxly" resultClass="Flobitz">
    select * from table
    <dynamic prepend="where col1 in ">
        <iterate property="list_of_values" open="('" close="')" conjunction=",  ">
            #list_of_values[]#
        </iterate>
    </dynamic>
</select>
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
4

Or:

<select id="select-test" resultMap="MyTableResult" parameterClass="list"> 
 select * from table where
 <iterate property="list" conjunction="OR">
  col1 = #list[]#
 </iterate>
</select>
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
bastola
  • 41
  • 1
0
<select id="select-test" parameterClass="list"resultMap="YourResultMap">
     select * from table where col_1 IN 
     <iterate open="(" close=")" conjunction=",">
      #[]#
    </iterate>
</select>
Jack
  • 10,943
  • 13
  • 50
  • 65
  • 1
    Care to comment your code? If there is another accepted answer from 2 years ago with 9 upvotes, what does your answer adds that important that can be a usefull answer? – Yaroslav Oct 05 '12 at 10:00
0

An old question, but for the users of MyBatis, the syntax is a bit different:

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

Refer to the guide in here.

Itaypk
  • 1,103
  • 10
  • 25
-1

You can use it like this:

<select id="select-test" resultMap="MyTableResult" >
select * from my_table where col_1 in
 $listOfValues$
</select>

use the $ in the IN statement.

iamxhu
  • 134
  • 5
  • this generate something like [aaa,abb,acc] can you suggest how can i change this default `[]` into `()` because oracle doesn't support `[]` – Saif Aug 27 '14 at 05:35