mybatis batch query, batch add, batch query, batch update

Analysis:

Properties of foreach

  item: alias of elements in the collection when iterating, required

  index: In list and array, index is the sequence number of the element; in map, index is the key of the element, optional

  open: the start symbol of the foreach code, generally used together with'(' and')', commonly used in in(), values(), optional

  separator: the separator between elements, optional

  close: the close symbol of the foreach code, usually')' and combined with'(', commonly used in in(), values(), optional

  collection: The object of foreach iteration. When used as an input parameter, the List object is replaced by list by default, and the array object is replaced by array. The Map object has no default keys.

    At the same time, you can use @param("xxx") to set the key when used as an input parameter. At this time, the default list and array will be invalid.

  Official instructions:

  Note that you can pass a List instance or array as a parameter object to MyBatis. When you do this, MyBatis will automatically wrap it in a Map and use the name as the key. List instances will have "list" as the key, and array instances will have the key "array"

 

1. Batch query

interface:

//参数里面的字符串由","进行拼接而成  
List<PartyOrganization> getOrgByListId(@Param("listOrgId") String listOrgId);

xml:

 <select id="getOrgByListId" resultType="com.safesoft.domain.partybuilding.entity.PartyOrganization">
        select
        id as id,
        org_name as orgName,
        parent_org_code as parentOrgCode,
        org_code as orgCode,
        org_leavel_type as orgLeavelType from t_zhcx_dj_dw_org
        where
        delete_flag = 0 and  id in
        <foreach item="item" index="index" collection="listOrgId.split(',')" open="(" separator="," close=")">
            '${item}'
        </foreach>
    </select>

Two, batch add

interface:

Integer insertByAttachmentList(@Param("attachmentList") List<ActivityMapAttachment> attachmentList);

xml:

<insert id="insertByAttachmentList">
        insert into t_xfdj_activity_map_attachment
        (
        activity_id,attachment_id
        ) values
        <foreach collection="attachmentList" item="attachment" separator=",">
            (
            #{attachment.activityId},
            #{attachment.attachmentId}
            )
        </foreach>
    </insert>

Three, batch modification

interface:

 Integer updateSignStatusByMemberList(@Param("activityId") Long activityI,@Param("memberList") List<Long> memberList);

xml:

<update id="updateSignStatusByMemberList" parameterType="java.util.List">
        update t_xfdj_activity_map_member set
        sign_in_status = 1
        where activity_id=#{activityId} and member_id in
        <foreach collection="memberList" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

4. Batch delete (the delete below is just an update, change a state, not delete)

interface:

 Integer batchDelproductId(List<Integer> productIds);

xml:

<update id="batchDelproductId">
        update application_customer_product set is_delete=true
        where id in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>

    </update>

 

Guess you like

Origin blog.csdn.net/qq_36138652/article/details/108846758