iBatis.Net系列(十)-范围查询

情况一:查询类似 id>13 and id<15 这样的语句
<select id="SelectStudentRange" parameterClass="Hashtable" resultClass="Student">
select * from student
<dynamic prepend="where">
<isGreaterEqual prepend="and" property="id1" compareValue="0" >
<![CDATA[id>=#id1#]]>
</isGreaterEqual>
<isGreaterEqual prepend="and" property="id2" compareValue="0">
<![CDATA[id<=#id2#]]>
</isGreaterEqual>
</dynamic>
</select>
参数使用了Hashtable,
因为像"<",">"这样的符号在xml文档中是有意义的,所以使用了<![CDATA[ sql语句写在这里 ]]>这样的语句包起来转义
Student类中的方法
public IList<Student> SelectRange(Hashtable ht)
{
return mapper.QueryForList<Student>("SelectStudentRange", ht);
}
调用
ht.Add("id1", 13);
ht.Add("id2", 15);
IList<Student> s1 = new Student().SelectRange(ht); //select * from student where id>13 and id<15

情况二:查询类似 id in(13,14) 这样的语句
<select id="SelectStudentRange1" resultClass="Student">
select * from student
<dynamic prepend="where">
    <iterate open="id in(" close=")" conjunction=",">
    #[]#
   </iterate>
</dynamic>
</select>
<iterate>节点是一个递归语法,递归的开始用open,结束用close,中间用,相隔
#[]# 就代表数组的每一项了
Student类中的方法
public IList<Student> SelectRange1(int[] ids)
{
return mapper.QueryForList<Student>("SelectStudentRange1", ids);
}
调用
IList<Student> s = new Student().SelectRange1(new int[] { 13, 14 }); //select * from student where id in(13,14)

转载于:https://www.cnblogs.com/wangblogs/p/4525559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值