How to use an IN clause in iBATIS?
Asked Answered
C

6

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?

Cosmology answered 28/10, 2009 at 14:59 Comment(0)
O
33

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);
Ondrea answered 28/10, 2009 at 15:8 Comment(4)
all this xml makes me want to vomit, but thx for the info very helpful for a project where I have to use ibatisAzov
I can't believe I just upvoted an iBatis question/answer. When will this project be over, and I can stop using iBatis?Ferric
Nah, iBatis is fantastic. At least you get to write SQL instead of some other abomination.Rush
Could you please read my similar post, thanks !: #28343506Cymric
A
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>
Afeard answered 28/10, 2009 at 15:6 Comment(0)
O
4

Or:

<select id="select-test" resultMap="MyTableResult" parameterClass="list"> 
 select * from table where
 <iterate property="list" conjunction="OR">
  col1 = #list[]#
 </iterate>
</select>
Osbourn answered 29/4, 2010 at 20:10 Comment(0)
O
0
<select id="select-test" parameterClass="list"resultMap="YourResultMap">
     select * from table where col_1 IN 
     <iterate open="(" close=")" conjunction=",">
      #[]#
    </iterate>
</select>
Olshausen answered 12/9, 2012 at 20:10 Comment(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?Gregory
M
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.

Minerva answered 22/12, 2017 at 16:57 Comment(0)
L
-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.

Lavonlavona answered 23/6, 2010 at 2:32 Comment(1)
this generate something like [aaa,abb,acc] can you suggest how can i change this default [] into () because oracle doesn't support []Portecochere

© 2022 - 2024 — McMap. All rights reserved.