閱讀940 返回首頁    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-03 16:49:30

  上一篇:go jdk1.5新特性5之枚舉之枚舉類型的應用
  下一篇:go Java基礎知識——SDK、JDK、JRE、JVM、JDT、CDT等之間的區別與聯係