首页 技术 正文
技术 2022年11月19日
0 收藏 955 点赞 4,922 浏览 1581 个字

1、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”。

2、示例:

  dao:

   int batchInsert(List<Coupon> coupons);

  Mapper:

<insert id="batchInsertMiCoupon" parameterType="java.util.List">
INSERT INTO
mi_coupon
(
xxx
)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.xxx,jdbcType=VARCHAR}
)
</foreach>
</insert>

另外需要注意,批量插入List集合的大小不要超过500,200为宜,否则插入也会很慢,过多的话甚至一条都不能入库且没有报错信息。

3、mybatis 批量入库参数一个为集合,一个为字符串的情况

// Map组装参数
Map<String, Object> param = new HashMap<>(4);
param.put("subProductIdsList", subProductIdsList);
param.put("comProductId", comProductId);
// dao
@Override
public int batchInsert(Map<String, Object> param) {
return this.getSqlSessionTemplate().insert(NAMESPACE + ".batchInsert", param);
}
// Mapper.xml
<insert id="batchInsert">
insert into xxx(com_product_id, sub_product_id)
values
<foreach collection="subProductIdsList" item="item" index="index" separator=",">
(
#{comProductId},
#{item,jdbcType=VARCHAR}
)
</foreach>
</insert>

批量更新

    <!--批量更新分集视频状态为审核中-->
<update id="batchUpdateAuditStatus" parameterType="java.util.List">
update cp_video
set
audit_status='1'
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item,jdbcType=BIGINT}
</foreach>
</update>

END

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862