如果在使用批量插入的时候有多个list该怎么用一条sql搞定呢。可以给list拼接成一个map然后去循环List<Map>,如下操作
public void tenantAddApp(String tenantId,String appIds,String wxAppIds){
List<String> app = Arrays.asList(appIds.split(","));
List<String> wxApp = Arrays.asList(wxAppIds.split(","));
List<Map> mapList = new ArrayList<>();
for(int i =0 ;i<app.size(); i++){
Map map = new HashMap();
map.put("appId",app.get(i));
map.put("wxAppId",wxApp.get(i));
mapList.add(map);
}
tenantMapper.tenantAddApp(tenantId,mapList);
}
然后xml中直接去循环读取map指定的值
void tenantAddApp(@Param("tenantId") String tenantId, @Param("mapList")List<Map> mapList);
<insert id="tenantAddApp">
insert into oauth_tenant_applicaion values
<foreach collection="mapList" item="map" index="index" separator="," close=";">
(#{tenantId}, #{map.appId},#{map.wxAppId})
</foreach>
</insert>
这样就实现了批量插入多条数据
本文介绍了一种在数据库操作中实现批量插入多条记录的方法。通过将多个列表转换为一个Map列表,并利用XML文件中的foreach循环进行遍历,从而一次性完成所有数据的插入操作。
1638

被折叠的 条评论
为什么被折叠?



