ibatis中数字字段使用in时,参数为字符串类型

在使用ibatis开发时,遇到了一个问题。
[code="java"]
select * from table_name where id in (#parm#)
[/code]
其中id字段为number类型,传入参数parm为字符串,例如“1,2,3,4,5,6,8,9”
显然,将sql拼接然后到plsql运行是没有问题的,但是,在ibatis中,运行程序,最后就会报ORA-01722: invalid number 错误
希望有高手可以指教下,谢谢!

select * from table_name where id in ($parm$)

要这么去写啊?

你原来那么写的结果是

select * from table_name where id in ('1,2,3,4,5,6,8,9')

这里不可以使用#号的

得用$ 符号接收参数
这样的话

select * from table_name where id in (1,2,3,4,5,6,8,9)

他把你的1,2,3,4,5,6,8,9当字符串“1,2,3,4,5,6,8,9”了,而不是简单的SQL语句拼接

第一种:传入参数仅有数组

select *
from MailInfo with (nolock)
where ID in

#[]#


调用
string[] strValue = new string[] { "1", "2", "3" };
Reader.QueryForList("WebApp_Ibatisnet.dao.GetEmailList_Test", strValue );

   第二种:传入参数有数组,且有其他数据
    <select id="GetEmailList_Test3" parameterClass="TestIn" resultClass="EmailInfo_">
        select  top(#Count#)*
        from MailInfo with (nolock)
        where ID in
        <iterate open="(" close=")" conjunction="," property="ArrValue" >
            #ArrValue[]#
        </iterate>
    </select>

调用
TestIn ti = new TestIn();
ti.Count = 1;
ti.ArrValue = strValue;
return Reader.QueryForList("WebApp_Ibatisnet.dao.GetEmailList_Test3", ti);
实体类:
public class TestIn
{
private int count;
public int Count
{
get { return count; }
set { count = value; }
}
private string[] arrValue;
public string[] ArrValue
{
get { return arrValue; }
set { arrValue = value; }
}
}

   第三种:in后面的数据确定,使用string传入
    <select id="GetEmailList_Test2" parameterClass="TestIn" resultClass="EmailInfo_">
        select *
        from MailInfo with (nolock)
        where ID in
        ($StrValue$)
    </select>

调用
Reader.QueryForList("WebApp_Ibatisnet.dao.GetEmailList_Test2", "1,2,3");

其他信息:
Iterate的属性:
prepend -可被覆盖的SQL语句组成部分,添加在语句的前面(可选)
property -类型为java.util.List的用于遍历的元素(必选)
open -整个遍历内容体开始的字符串,用于定义括号(可选)
close -整个遍历内容体结束的字符串,用于定义括号(可选)
conjunction -每次遍历内容之间的字符串,用于定义AND或OR(可选)
遍历类型为java.util.List的元素。

一种简单的方法:
select * from table_name
where instr(concat(',',#parm#,','),concat(',',id,',')) > 0

[code="SQL"]

select * from table_name

where id in

TO_NUMBER(<![CDATA[#idList[]#]]>)


[/code]

参数参考如下设置:
[code="java"]List idList = new ArrayList();
idList.add("1");
idList.add("2");
idList.add("3");
idList.add("4");
...[/code]