2009
-
05
-
21
Ibatis动态查询例子(#和$以及iterate等的用法)
最近做了很多动态的查询,尤其是排序,以及一些状态字段,所以就做了一个总的动态查询,以不变应万变,呵呵
ibatis 里面的sql代码:
- <select id="getTopics" resultClass="topic" parameterClass="map">
- <![CDATA[
- select * from p_Topic
- ]]>
- <dynamic prepend=" WHERE ">
- <isPropertyAvailable property="authorId">
- <isNotNull property="authorId" prepend=" and ">
- authorId=#authorId#
- </isNotNull>
- </isPropertyAvailable>
- <isPropertyAvailable property="marketId">
- <isNotNull property="marketId" prepend=" and ">
- marketId=#marketId#
- </isNotNull>
- </isPropertyAvailable>
- <isPropertyAvailable property="isDelete">
- <isNotNull property="isDelete" prepend=" and ">
- isDelete=#isDelete#
- </isNotNull>
- </isPropertyAvailable>
- <isPropertyAvailable property="isBest">
- <isNotNull property="isBest" prepend=" and ">
- isBest=#isBest#
- </isNotNull>
- </isPropertyAvailable>
- <isPropertyAvailable property="statusStr">
- <isNotNull property="statusStr" prepend=" and ">
- $statusStr$
- </isNotNull>
- </isPropertyAvailable>
- <isPropertyAvailable property="marketIdList">
- <isNotNull property="marketIdList" prepend=" and marketId in ">
- <iterate property="marketIdList" conjunction="," close=")" open="(">
- #marketIdList[]#
- </iterate>