zoukankan      html  css  js  c++  java
  • 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 = "[{
    " +
            "	"areaId": 1
    " +
            "}, {
    " +
            "	"areaId": 2
    " +
            "}]")
    @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 = "[{
    " +
            "	"areaId": 1
    " +
            "}, {
    " +
            "	"areaId": 2
    " +
            "}]")
    @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>
  • 相关阅读:
    PTA 1022 Digital Library (30分) 坑多需谨慎!!!
    PAT 1013 Battle Over Cities (25分) 图的连通分量+DFS
    PAT 1021 Deepest Root (25分) 从测试点3超时到满分再到代码优化
    Java面向对象-------多态总结
    C后端设计开发
    C后端设计开发
    C后端设计开发
    C后端设计开发
    C后端设计开发
    一个真正的客户端非阻塞的 connect
  • 原文地址:https://www.cnblogs.com/ooo0/p/14283174.html
Copyright © 2011-2022 走看看