如何在iBATIS中使用IN子句?

问题描述 投票:17回答:6

我正在使用iBATIS来创建select语句。现在我想用iBATIS实现以下SQL语句:

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

使用以下方法,语句未正确准备且没有返回结果:

SELECT * FROM table WHERE col1 IN #listOfValues#;

iBATIS似乎重组了这个列表并尝试将其解释为字符串。

如何正确使用IN子句?

java sql ibatis
6个回答
32
投票

这是一篇回答你问题的博文:

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>

在Java中,您应该传入java.util.List。例如。

List<String> list = new ArrayList<String>(3);
list.add("1");
list.add("2");
list.add("3");
List objs = sqlMapClient.queryForList("select-test",list);

12
投票

怎么样

<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>

4
投票

要么:

<select id="select-test" resultMap="MyTableResult" parameterClass="list"> 
 select * from table where
 <iterate property="list" conjunction="OR">
  col1 = #list[]#
 </iterate>
</select>

0
投票
<select id="select-test" parameterClass="list"resultMap="YourResultMap">
     select * from table where col_1 IN 
     <iterate open="(" close=")" conjunction=",">
      #[]#
    </iterate>
</select>

0
投票

一个老问题,但对于MyBatis的用户,语法有点不同:

<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>

请参阅guide in here


-1
投票

你可以像这样使用它:

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

在IN语句中使用$。

© www.soinside.com 2019 - 2024. All rights reserved.