阅读964 返回首页    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-03 20:19:46

  上一篇:go java线程学习4——线程同步之同步代码块
  下一篇:go Java中连接各种数据库