批量insert多条数据,支持list参数的字段多少是可变长度的,保证调用时候,list内的每一个元素属性是一致的,就可以使用。主要是结合xml模板,mapper接口的参数,能被mybatis取到参数,执行正确的sql语法。
方式一:
<!-- 批量新增 ,包含字段 receive_open_id ,map参数来构造灵活的sql--> <insert id="insertBatchForColumns" parameterType="map" keyProperty="id" keyColumn="id" useGeneratedKeys="true"> <!-- <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() AS id </selectKey> --> insert into t_cloudcrm_user_coupon_relate (open_id, merchant_code, <if test="receiveOpenId != null and receiveOpenId != ''"> receive_open_id, </if> coupon_id, task_id, coupon_state, coupon_fee, exp_time, crt_time, cur_date, get_type,eff_time, send_shop_name, send_state ) values <foreach collection="tCloudcrmUserCouponRelateList" item="item" index="index" separator=","> (#{item.openId,jdbcType=VARCHAR}, #{item.merchantCode,jdbcType=CHAR}, <if test="receiveOpenId != null and receiveOpenId != ''"> #{item.receiveOpenId,jdbcType=CHAR}, </if> #{item.couponId,jdbcType=INTEGER}, #{item.taskId,jdbcType=INTEGER}, #{item.couponState,jdbcType=CHAR}, #{item.couponFee,jdbcType=INTEGER}, #{item.expTime,jdbcType=TIMESTAMP}, #{item.crtTime,jdbcType=TIMESTAMP}, #{item.curDate,jdbcType=CHAR}, #{item.getType,jdbcType=CHAR}, #{item.effTime,jdbcType=TIMESTAMP}, <choose> <when test="item.sendShopName != null and item.sendShopName != ''">#{item.sendShopName,jdbcType=VARCHAR},</when> <otherwise>'',</otherwise> </choose> <choose> <w