ibatis中动态语句的iterate标签

简介:

 

例子一

 

查询条件dto

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


查询sqlMap

<select id="selectStu" 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 id="selectStu" 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 = ?)...

目录
相关文章
|
22天前
|
SQL XML Java
Mybatis中选择语句的使用:<choose>标签、分区排序 Row_num() over ()函数的使用呢
Mybatis中选择语句的使用:<choose>标签、分区排序 Row_num() over ()函数的使用呢
13 0
|
1月前
|
SQL Java 关系型数据库
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
16 0
|
8月前
|
SQL Java 数据库连接
深入解析 MyBatis 中的 <foreach>标签:优雅处理批量操作与动态 SQL
在当今的Java应用程序开发中,数据库操作是一个不可或缺的部分。MyBatis作为一款颇受欢迎的持久层框架,为我们提供了一种优雅而高效的方式来管理数据库操作。在MyBatis的众多特性中,`&lt;foreach&gt;`标签无疑是一个强大的工具,它使得在SQL语句中进行动态循环迭代变得轻而易举。本文将带您深入探索MyBatis中的`&lt;foreach&gt;`标签,揭示其背后的原理和用法。
134 0
|
11月前
|
SQL Java 数据库连接
MyBatis动态SQL中if、where、trim、choose、when、otherwise、foreach标签及sql标签范例
MyBatis动态SQL中if、where、trim、choose、when、otherwise、foreach标签及sql标签范例
98 0
|
SQL Java 数据库连接
Mybaits结果集之集合,Javabean中嵌套List的解决方案
Mybaits结果集之集合,Javabean中嵌套List的解决方案
199 0
Mybaits结果集之集合,Javabean中嵌套List的解决方案
重构——38以卫语句取代嵌套条件表达式(Replace Nested Conditional with Guard Clause)
以卫语句取代嵌套条件表达式(Replace Nested Conditional with Guard Clause):函数中的条件逻辑使人难以看清正常的执行路径;使用卫语句表现所有特殊情况
3744 0