这两天项目用到ibatis,碰到and or的联合查询,语句像这样的
select * from table where xxx = "xxx" and (xxx="xxx" or xx1="xx1")
google,baidu了都找不到解决的方法,就是不知道括号往哪加
后面看到ibatis的动态迭代iterate里有个open="(",close=")"
我就在<dynamic prepend="and" 后面加上 open="(",close=")">一试,果然SQL拼凑正确
下面贴出动态查询代码备忘:
<sql id="condition">
<dynamic prepend="and">
<isNotEmpty prepend="and" property="starttime">
<![CDATA[ starttime >= #starttime# ]]>
</isNotEmpty>
<isNotEmpty prepend="and" property="endtime">
<![CDATA[ starttime <= #endtime# ]]>
</isNotEmpty>
<isNotEmpty prepend="and" property="rentcompany">
<![CDATA[ rentcompany like '%$rentcompany$%' ]]>
</isNotEmpty>
</dynamic>
<dynamic prepend="and" open="(" close=")">
<isNotNull prepend="or" property="bilkcar">
<![CDATA[ bilkcar = 1 ]]>
</isNotNull>
<isNotNull prepend="or" property="guarantycar">
<![CDATA[ guarantycar = 1 ]]>
</isNotNull>
<isNotNull prepend="or" property="filchpart">
<![CDATA[ filchpart = 1 ]]>
</isNotNull>
<isNotNull prepend="or" property="causetrouble">
<![CDATA[ causetrouble = 1 ]]>
</isNotNull>
<isNotNull prepend="or" property="nohandle">
<![CDATA[ nohandle = 1 ]]>
</isNotNull>
<isNotNull prepend="or" property="inarrears">
<![CDATA[ inarrears = 1 ]]>
</isNotNull>
</dynamic>
</sql>
分享到:
相关推荐
ibatis and和or联合查询 .doc
一个Ibatis+Mysql的简单例子,有数据库文件。希望对初学者有所帮助!
操作数据库 iBATIS查询,java 和spring的配置方法
ibatis动态多条件组合查询 实例 说明
Ibatis多表查询
Ibatis多表查询,一个小小的多表查询实例教你如何用ibatis进行多表查询
Ibatis查询语句里,可以使用多表查询,返回多个表的值.doc
ibatis学习 ibatis总结 ibatis ibatis ibatis
Ibatis查询Id列表.doc
Ibatis复杂查询语句.doc
ibatis的多参数查询.doc
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
iBatis实战and源代码.zip
ibatis_动态查询条件详解及需要注意的地方
iBatis和Spring整合 iBatis和Spring整合
这个文件中包含了许多ibatis的查询语句,能够让你很清楚的知道,如何实现动态的查询。
ibatis做连接查询 .doc
ibatis教程_查询指定id的单个对象 ibatis教程_查询指定id的单个对象
ibatis多表查询开发过程(java)