MyBatis:Error querying database. Cause: java.sql.SQLException: Operand should contain 1 column(s)

问题描述

用动态 SQL 的 foreach遍历一个集合,结果出现了下面的错误,我们可以看到 sql 语句中缺少了 in关键字,看了一下代码发现少写了 providerId in。

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: Operand should contain 1 column(s)
### The error may exist in mapper/BillMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select *         from smbms_bill          WHERE (                 ?             ,                 ?             )
### Cause: java.sql.SQLException: Operand should contain 1 column(s)

原代码

<!--    5)使用foreach获取指定供应商列表下的订单列表-->
    <select id="queryByForeach" resultType="com.smbms.pojo.Bill">
        select *
        from smbms_bill
        <where>

            <foreach collection="list" index="index" open="(" separator="," close=")" item="providerId">
                #{providerId}
            </foreach>
        </where>
    </select>

修改后的代码

<!--    5)使用foreach获取指定供应商列表下的订单列表-->
    <select id="queryByForeach" resultType="com.smbms.pojo.Bill">
        select *
        from smbms_bill
        <where>
            providerId in
            <foreach collection="list" index="index" open="(" separator="," close=")" item="providerId">
                #{providerId}
            </foreach>
        </where>
    </select>

运行结果

第二种情况

你可能多谢了一对括号在 in 的后面,把它删掉就可以了

<!--    5)使用foreach获取指定供应商列表下的订单列表-->
    <select id="queryByForeach" resultType="com.smbms.pojo.Bill">
        select *
        from smbms_bill
        <where>
            providerId in
            (<foreach collection="list" index="index" open="(" separator="," close=")" item="providerId">
                #{providerId}
            </foreach>)
        </where>
    </select>

猜你喜欢

转载自blog.csdn.net/lemonzjk/article/details/124533493