【问题标题】:How to use an IN clause in iBATIS?如何在 iBATIS 中使用 IN 子句?
【发布时间】:2009-10-28 14:59:06
【问题描述】:

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

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

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

SELECT * FROM table WHERE col1 IN #listOfValues#;

iBATIS 似乎对这个列表进行了重组,并试图将其解释为一个字符串。

如何正确使用IN子句?

【问题讨论】:

    标签: java sql ibatis


    【解决方案1】:

    这里有一篇博文可以回答您的问题:

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

    【讨论】:

    • 所有这些 xml 让我想呕吐,但是对于我必须使用 ibatis 的项目非常有帮助的信息
    • 我不敢相信我刚刚对 iBatis 问题/答案投了赞成票。这个项目什么时候结束,我可以停止使用 iBatis 了?
    • 不,iBatis 太棒了。至少你可以编写 SQL 而不是其他令人讨厌的东西。
    • 能否请您阅读我的类似帖子,谢谢!:stackoverflow.com/questions/28343006/…
    【解决方案2】:

    怎么样

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

    【讨论】:

    • 谢谢,你救了我的命:D
    【解决方案3】:

    或者:

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

    【讨论】:

      【解决方案4】:
      <select id="select-test" parameterClass="list"resultMap="YourResultMap">
           select * from table where col_1 IN 
           <iterate open="(" close=")" conjunction=",">
            #[]#
          </iterate>
      </select>
      

      【讨论】:

      • 想评论你的代码?如果 2 年前有另一个被接受的答案有 9 个赞,你的答案有什么重要的补充,可以成为有用的答案?
      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        你可以这样使用它:

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

        在 IN 语句中使用 $。

        【讨论】:

        • 这会生成类似 [aaa,abb,acc] 的东西,你能建议我如何将这个默认的 [] 更改为 (),因为 oracle 不支持 []
        猜你喜欢
        • 2018-06-08
        • 2016-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多