Mybatis <foreach>语句
2014-09-17 17:53
369 查看
首次发现问题需要遍历map,找了很多资料发现,并没有很好的用foreach 解决 遍历map的案例。 所以把map中的key和value值 用list<>代替
为了解决遍历List数组问题,初次使用了Mybatis中的 <foreach> 来解决问题。
初步了解 <foreach>的一些属性,查找资料发现这个比较全面。
下面进行代码分析:
对于初学者而已,我仅仅使用了 item和collection。其他的一些参数可以选择不写。
xml端:
<select id="getMeterIDByList" resultMap="MeterIDMap" >
select distinct meterid from pro_meterandcom where 1=1
<foreach collection="list" item="items" >
and meterid in(select meterid from pro_meterandcom where comtypeid =#{items.comtypeid}
<if test=" items.protocolid != 0" >
and protocolid =#{items.protocolid}
</if>
)
</foreach>
</select>
<resultMap id="MeterIDMap" type="java.lang.Integer" >
<result column="METERID" property="meterid" jdbcType="DECIMAL" />
</resultMap>
java端:
List<Integer> getMeterIDByList(List<ProMeterAndComVO> param);
首先我传入的参数是 一个List<ProMeterAndComVO> 的形式, ProMeterAndComVO是我定义的一个类,里面有comtypeid和protocolid
2个属性。
在xml中collection="list"表示传入的参数是List,
item="items" 表示给List起一个别名 叫items. 所以通过items.comtypeid这种形式来访问 ProMeterAndComVO中的comtypeid属性。
然后
foreach 遍历 lists中的每一个元素。
这是一个简单的foreach来解决遍历list。
至于遍历map至今没有发现很好的方法。
为了解决遍历List数组问题,初次使用了Mybatis中的 <foreach> 来解决问题。
初步了解 <foreach>的一些属性,查找资料发现这个比较全面。
foreach属性
属性 | 描述 |
---|---|
item | 循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection | 要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。 当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子: 如果User有属性List ids。入参是User对象,那么这个collection = "ids" 如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id" 上面只是举例,具体collection等于什么,就看你想对那个元素做循环。 该参数为必选。 |
separator | 元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 |
open | foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。 |
close | foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。 |
index | 在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。 |
对于初学者而已,我仅仅使用了 item和collection。其他的一些参数可以选择不写。
xml端:
<select id="getMeterIDByList" resultMap="MeterIDMap" >
select distinct meterid from pro_meterandcom where 1=1
<foreach collection="list" item="items" >
and meterid in(select meterid from pro_meterandcom where comtypeid =#{items.comtypeid}
<if test=" items.protocolid != 0" >
and protocolid =#{items.protocolid}
</if>
)
</foreach>
</select>
<resultMap id="MeterIDMap" type="java.lang.Integer" >
<result column="METERID" property="meterid" jdbcType="DECIMAL" />
</resultMap>
java端:
List<Integer> getMeterIDByList(List<ProMeterAndComVO> param);
首先我传入的参数是 一个List<ProMeterAndComVO> 的形式, ProMeterAndComVO是我定义的一个类,里面有comtypeid和protocolid
2个属性。
在xml中collection="list"表示传入的参数是List,
item="items" 表示给List起一个别名 叫items. 所以通过items.comtypeid这种形式来访问 ProMeterAndComVO中的comtypeid属性。
然后
foreach 遍历 lists中的每一个元素。
这是一个简单的foreach来解决遍历list。
至于遍历map至今没有发现很好的方法。
相关文章推荐
- Mybatis的<where><foreach><set>等标签详解
- Mybatis的<where><foreach><set>等标签详解
- mybatis <foreach>标签
- MyBatis<forEach/>如何遍历Map参数里的值
- Mybatis的<where><foreach><set><trim>详解
- Mybatis的<where><foreach><set>等标签详解
- 求解:System.IEquatable<T>”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.IEquatable<T>”类型的变量
- Mybatis的<where><foreach><set>等标签详解
- mybatis <foreach>标签
- Mybatis的<where><foreach><set>等标签详解
- MyBatis 中 foreach 语句处理 List<Integer>类型
- Mybatis的<where><foreach><set>等标签详解
- <c:forEach varStatus="status">中 varStatus的属性简介
- <c:forEach>标签
- <c:forEach>标签的使用
- javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
- 数据库中SQL语句where 1=1及where 1<>1或1=0的妙用
- jsp小知识01----<c:forEach>
- <c:foreach>同时遍历两个List的方法
- <<展现C#>>第六章 控制语句(修订)