mybatis批量查询,批量新增,批量查询,批量更新
一、批量查询接口//参数里面的字符串由","进行拼接而成List<PartyOrganization> getOrgByListId(@Param("listOrgId") String listOrgId);xml<select id="getOrgByListId" resultType="com.safesoft.domain.partybuilding.entity.Pa
解析:
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 collection="areaCodeList" index="index" item="item" open="(" close=")" separator=",">
#{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>
批量更新另一种写法:
注意点:批量更新的时候,连接地址上必须加上allowMultiQueries=true
<update id="updateSignStatusByMemberList" >
<foreach collection="list" item="item" separator=";">
update
`t_xfdj_activity_map_member `
set
`name` = #{item.name},
`age` = #{item.age}
where
id = #{item.id}
</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>

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)