当前位置: 代码迷 >> 综合 >> Mybatis 动态SQL批量插入(有则更新无则插入)
  详细解决方案

Mybatis 动态SQL批量插入(有则更新无则插入)

热度:4   发布时间:2024-01-17 22:35:27.0

mysql动态批量插入(有则更新无则插入)

在之前的文章中已经写过批量插入,这个再专门写一遍;

<insert id="batchAddRecomprodInto" parameterType="java.util.List" >replace into td_cc_recomprod(act_id, pro_id, start_date,pro_nm, pro_explain, link_ref,show_order, pic_location, end_date,update_staff_id, update_date, remark)VALUES<foreach collection="list" item="item" separator=",">(#{item.actId,jdbcType=BIGINT}, #{item.proId,jdbcType=VARCHAR}, #{item.startDate,jdbcType=TIMESTAMP},#{item.proNm,jdbcType=VARCHAR}, #{item.proExplain,jdbcType=VARCHAR}, #{item.linkRef,jdbcType=VARCHAR},#{item.showOrder,jdbcType=INTEGER}, #{item.picLocation,jdbcType=VARCHAR}, #{item.endDate,jdbcType=TIMESTAMP},#{item.updateStaffId,jdbcType=VARCHAR}, #{item.updateDate,jdbcType=TIMESTAMP}, #{item.remark,jdbcType=VARCHAR})</foreach></insert>

 

 

item:为数组每个元素的名称,名称随意定义,之后每条数据从便利的集合里面取

 

有时候报找不到参数的,可能是循环写的不对,或者参数取值写的有问题,或者参数类型写错了,测试类很好写,这里也贴一下把:

 

Mapper文件:

 

int batchAddRecomprodInto(List<TdCcRecomprod> record);

测试类就不写了,

pojo放在list里面,调一下就行了;

主要想说一下参数怎么用:

item.actId,

别写错了,Mybatis 有时候报错不准确,改起来很麻烦;

 

 

 

 

 

 

 

 

Caused by: org.apache.ibatis.binding.BindingException: Parameter 'actId' not found. Available parameters are [collection, list]

 

比如有时候会报上面这个错,基本上就是这样,取参数的时候方法错了;

  相关解决方案