MyBatis <foreach>

 

foreach元素的属性主要有item,index,collection,open,separator,close。

  • item:集合中元素迭代时的别名,该参数为必选。
  • index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选
  • open:foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选
  • separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
  • close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
  • collection: 要做foreach的对象,作为入参时,List对象默认用"list"代替作为键,数组对象有"array"代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids".如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"

 

遍历List<Integer>:

@ApiOperation(value = "getByIdIn", notes = "[1,2]")
@RequestMapping(value = "/getByIdIn", method = RequestMethod.POST)
public List<TbArea> getByIdIn(@RequestBody List<Integer> ids) {
    return tbAreaMapper2.getByIdIn(ids);
}
List<TbArea> getByIdIn(@Param("ids") List<Integer> ids);
<select id="getByIdIn" resultType="com.xc.xcspringboot.model.area.TbArea">
    select *
    from tb_area
    WHERE area_id in
    <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

 

遍历List<对象>:

@ApiOperation(value = "getByIdIn2", notes = "[{\n" +
        "\t\"areaId\": 1\n" +
        "}, {\n" +
        "\t\"areaId\": 2\n" +
        "}]")
@RequestMapping(value = "/getByIdIn2", method = RequestMethod.POST)
public List<TbArea> getByIdIn2(@RequestBody List<TbArea> tbAreaList) {
    return tbAreaMapper2.getByIdIn2(tbAreaList);
}
List<TbArea> getByIdIn2(@Param("tbAreaList") List<TbArea> tbAreaList);
<select id="getByIdIn2" resultType="com.xc.xcspringboot.model.area.TbArea">
    select *
    from tb_area
    WHERE area_id in
    <foreach collection="tbAreaList" index="index" item="item" open="(" separator="," close=")">
        #{item.areaId}
    </foreach>
</select>

 

遍历数组:

@ApiOperation(value = "getByIdIn3", notes = "[1,2]")
@RequestMapping(value = "/getByIdIn3", method = RequestMethod.POST)
public List<TbArea> getByIdIn3(@RequestBody String[] ids) {
    return tbAreaMapper2.getByIdIn3(ids);
}
List<TbArea> getByIdIn3(@Param("ids") String[] ids);
<select id="getByIdIn3" resultType="com.xc.xcspringboot.model.area.TbArea">
    select *
    from tb_area
    WHERE area_id in
    <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

 

遍历map中的List<对象>:

@ApiOperation(value = "getByIdIn4", notes = "[{\n" +
        "\t\"areaId\": 1\n" +
        "}, {\n" +
        "\t\"areaId\": 2\n" +
        "}]")
@RequestMapping(value = "/getByIdIn4", method = RequestMethod.POST)
public List<TbArea> getByIdIn4(@RequestBody List<TbArea> tbAreaList) {
    Map<String, List<TbArea>> map = new HashMap();
    map.put("tbAreaList", tbAreaList);
    return tbAreaMapper2.getByIdIn4(map);
}
List<TbArea> getByIdIn4(@Param("map") Map<String, List<TbArea>> map);
<select id="getByIdIn4" resultType="com.xc.xcspringboot.model.area.TbArea">
    select *
    from tb_area
    WHERE area_id in
    <foreach collection="map.tbAreaList" index="index" item="item" open="(" separator="," close=")">
        #{item.areaId}
    </foreach>
</select>

 

遍历map取map的key:

@ApiOperation(value = "getByIdIn5", notes = "")
@RequestMapping(value = "/getByIdIn5", method = RequestMethod.POST)
public List<TbArea> getByIdIn5() {
    Map<String, String> map = new HashMap();
    map.put("1", "");
    map.put("2", "");
    return tbAreaMapper2.getByIdIn5(map);
}
List<TbArea> getByIdIn5(@Param("map") Map<String, String> map);
<select id="getByIdIn5" resultType="com.xc.xcspringboot.model.area.TbArea">
    select *
    from tb_area
    WHERE area_id in
    <foreach collection="map" index="index" item="item" open="(" separator="," close=")">
        #{index}
    </foreach>
</select>

 

posted @ 2021-01-15 17:17  草木物语  阅读(162)  评论(0编辑  收藏  举报