閱讀1012 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ibatis中動態語句的iterate標簽

 

例子一

 

查詢條件dto

public class queryCondition
{
 private String[] stuIds;
 private String name;
}


查詢sqlMap

<select parameterClass="cn.xy.queryCondition" resultClass="cn.xy.Student">
 select id,name from student
 <dynamic prepend="where">
  <isNotNull property="stuIds" prepend="and">
   <iterate property="stuIds" open="id in (" close=")" conjunction=",">
    #stuIds[]#
   </iterate>
  </isNotNull>
  <isNotNull property="name" prepend="and">
   name like '%$name$%'
  </isNotNull>
 </dynamic>
</select>


在查詢條件中有一個數組stuIds,在動態標簽中進行遍曆,看每一個student的id是否在該數組中。

發出的語句 select id,name from student where  id in ( ? , ?) ...  

 

 

例子二 

查詢條件dto

public class queryCondition
{
 private List<Student> lst;
 private String name;
}


查詢sqlMap

<select parameterClass="cn.xy.queryCondition" resultClass="cn.xy.Student">
 select id,name from student
 <dynamic prepend="where">
  <isNotNull property="lst" prepend="and">
   <iterate property="lst" open=" (" close=")" conjunction="or">
    id = #lst[].id#
   </iterate>
  </isNotNull>
  <isNotNull property="name" prepend="and">
   name like '%$name$%'
  </isNotNull>
 </dynamic>
</select>

 

發出的語句 select id,name from student where  (id = ?   or   id = ?)...

最後更新:2017-04-02 15:15:03

  上一篇:go Hadoop,有所為而有所不為
  下一篇:go ibatis動態語句中的prepend