ibatisand和or联合查询
最近遇到拼接ibatis中的sql语句的问题,想要得到的sql如下形式:
select * from table where xxx = "xxx" and (xxx="xxx" or xx1="xx1")
开始对于括号怎么加也确实不太明白,后面参考别人的博客
http://lyfei022.blog.163.com/blog/static/82558312009313104138320/才改正确
?
<select id="condition">
? ? ? ??<![CDATA[
select * from table where 1=1
]]>
??????? <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>
??? </select>
?
<dynamic prepend="and">这个and不能少