mybatis批量查询,批量新增,批量查询,批量更新

解析:

foreach的属性

  item:集合中元素迭代时的别名,必填

  index:在list和array中,index是元素的序号;在map中,index是元素的key,可选

  open:foreach代码的开始符号,一般是 ‘(’ 并和 ')' 合用,常用在in(),values()时,可选

  separator:元素之间的分隔符,可选

  close:foreach代码的关闭符号,一般是 ')'  并和 '('合用,常用在in(),values()时,可选

  collection:foreach迭代的对象,作为入参时,List对象默认用 list 代替,数组对象用 array代替。Map对象没有默认的键。

    同时可以在作为入参时使用@param("xxx")来设置键,这时,默认的list、array将会失效。

  官方说明:

  注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”

 

一、批量查询

接口:

//参数里面的字符串由","进行拼接而成  
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>

二、批量新增

接口:

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>

三、批量修改

接口:

 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>

四、批量删除(下面的删除只是一个update,改变一个状态,并非是delete)

接口:

 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>

猜你喜欢

转载自blog.csdn.net/qq_36138652/article/details/108846758
今日推荐