Lists in MyBatis 'IN' clause [duplicate] Lists in MyBatis 'IN' clause [duplicate] xml xml

Lists in MyBatis 'IN' clause [duplicate]


The value specified by the item attribute should be used inside the foreach tag, when used with Lists. Use as below :

    <foreach item="sId" collection="stripperIds" separator="," open="(" close=")">        #{sId}    </foreach>

The index attibute is not mandatory, when using a List. Refer the MyBatis docs section for more info, or check out the DTD - http://mybatis.org/dtd/mybatis-3-mapper.dtd for more info about the parameters :

    <!ELEMENT foreach (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>    <!ATTLIST foreach    collection CDATA #REQUIRED    item CDATA #IMPLIED    index CDATA #IMPLIED    open CDATA #IMPLIED    close CDATA #IMPLIED    separator CDATA #IMPLIED    >

Also, lists of objects can be accessed in foreach as below. You would typically use this for INSERT/UPDATE statements :

Sample bean :

public class StripperBean {    public StripperBean(int stripperID, String stripperName, String realName) {        this.stripperID = stripperID;        this.stripperName = stripperName;        this.realName = realName;    }    private int stripperID;     private String stripperName;    private String realName;            public int getStripperID() {        return stripperID;    }    public void setStripperID(int stripperID) {        this.stripperID = stripperID;    }    public String getStripperName() {        return stripperName;    }    public void setStripperName(String stripperName) {        this.stripperName = stripperName;    }    public String getRealName() {        return realName;    }    public void setRealName(String realName) {        this.realName = realName;    }       }

In your implementation :

    Map<String, Object> input = new HashMap<>();    input.put("club", club);    List<StripperBean> strippers = new ArrayList<>();    strippers.add(new StripperBean(1,"Ashley", "Jean Grey"));    strippers.add(new StripperBean(2,"Candice","Diana Prince"));    strippers.add(new StripperBean(3,"Cristal","Lara Croft"));            input.put("strippers", strippers);    return stripClubMapper.saveStripperDetails(input);

In the mapper xml :

    <insert id="saveStripperDetails">        INSERT INTO EXOTIC_DANCERS (STRIPPER_ID, STAGE_NAME, REAL_NAME)        VALUES        <foreach item="stripper" collection="input" separator=",">            (#{stripper.stripperID},            #{stripper.stripperName},            #{stripper.realName})        </foreach>    </select>

Nice question BTW :)


Using annotation should be easier

@Select({        "<script>", "select", " * ", "FROM TABLE",        "WHERE  CONDITION IN  " +        "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> #{item} </foreach>" +        "</script>"  })         @Results({ })        List<POJO> selectByKeys(@Param("list") List<String> ids);


Your xml should be like this:

<foreach item="item" index="index" collection="stripperIds" open="(" separator="," close=")">    #{item}</foreach>

When using a Map (or Collection of Map.Entry objects), index will be the key object and item will be the value object.

You can reference here for the details. You will have a solid understanding about the attributes.