如何在 iBATIS 中使用 IN 子句?

发布于 2024-08-08 18:41:18 字数 361 浏览 2 评论 0原文

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

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

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

SELECT * FROM table WHERE col1 IN #listOfValues#;

iBATIS 似乎重新构造了此列表并尝试将其解释为字符串。

如何正确使用 IN 子句?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嗳卜坏 2024-08-15 18:41:18

这是一篇回答您问题的博客文章:

iBatis: Support for Array or List Parameter with SQL IN 关键字

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

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);
折戟 2024-08-15 18:41:18

怎么样

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

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>
﹏半生如梦愿梦如真 2024-08-15 18:41:18

或者:

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

Or:

<select id="select-test" resultMap="MyTableResult" parameterClass="list"> 
 select * from table where
 <iterate property="list" conjunction="OR">
  col1 = #list[]#
 </iterate>
</select>
拥抱没勇气 2024-08-15 18:41:18
<select id="select-test" parameterClass="list"resultMap="YourResultMap">
     select * from table where col_1 IN 
     <iterate open="(" close=")" conjunction=",">
      #[]#
    </iterate>
</select>
<select id="select-test" parameterClass="list"resultMap="YourResultMap">
     select * from table where col_1 IN 
     <iterate open="(" close=")" conjunction=",">
      #[]#
    </iterate>
</select>
不如归去 2024-08-15 18:41:18

一个老问题,但对于 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>

参考 指南在此处

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.

酒与心事 2024-08-15 18:41:18

您可以这样使用它:

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

在 IN 语句中使用 $。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文