关于xml里面的拼接动态sql,批量插入的,不知道那种是对的

   <insert id="batchInsertGoods" >
        insert into dt.ec.goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">
                goods_id,
            </if>
            <if test="platformNo != null">
                platform_no,
            </if>
            <if test="platformGoodsNo != null">
                platform_goods_no,
            </if>
            <if test="platformGoodsName != null">
                platform_goods_name,
            </if>
            <if test="styleNo != null">
                style_no,
            </if>
            <if test="styleName != null">
                style_name,
            </if>
            <if test="isStop != null">
                is_stop,
            </if>
            <if test="autoUpload != null">
                auto_upload,
            </if>
            <if test="uploadPercent != null">
                upload_percent,
            </if>
            <if test="shopPrice != null">
                shop_price,
            </if>
            <if test="safeNums != null">
                safe_nums,
            </if>
            <if test="onsellTime != null">
                onsell_time,
            </if>
            <if test="remark != null">
                remark,
            </if>
            create_time,
            update_time,
            <if test="status != null">
                status,
            </if>
            <if test="isDel != null">
                is_del,
            </if>
            <if test="hitsShelvesType != null">
                hits_shelves_type,
            </if>
            <if test="shopId != null">
                shop_id,
            </if>
            <if test="imageUrl != null">
                image_url,
            </if>
            <if test="tId != null">
                t_id,
            </if>
        </trim>
        values
        <foreach collection="simpleGoodsList" item="item" index="index">
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="goodsId != null">
                    #{item.goodsId,jdbcType=INTEGER},
                </if>
                <if test="platformNo != null">
                    #{item.platformNo,jdbcType=VARCHAR},
                </if>
                <if test="platformGoodsNo != null">
                    #{item.platformGoodsNo,jdbcType=VARCHAR},
                </if>
                <if test="platformGoodsName != null">
                    #{item.platformGoodsName,jdbcType=VARCHAR},
                </if>
                <if test="styleNo != null">
                    #{item.styleNo,jdbcType=VARCHAR},
                </if>
                <if test="styleName != null">
                    #{item.styleName,jdbcType=VARCHAR},
                </if>
                <if test="isStop != null">
                    #{item.isStop,jdbcType=TINYINT},
                </if>
                <if test="autoUpload != null">
                    #{item.autoUpload,jdbcType=TINYINT},
                </if>
                <if test="uploadPercent != null">
                    #{item.uploadPercent,jdbcType=DECIMAL},
                </if>
                <if test="shopPrice != null">
                    #{item.shopPrice,jdbcType=DECIMAL},
                </if>
                <if test="safeNums != null">
                    #{item.safeNums,jdbcType=INTEGER},
                </if>
                <if test="onsellTime != null">
                    #{item.onsellTime,jdbcType=TIMESTAMP},
                </if>
                <if test="remark != null">
                    #{item.remark,jdbcType=VARCHAR},
                </if>
                now(),
                now(),
                <if test="status != null">
                    #{item.status,jdbcType=INTEGER},
                </if>
                <if test="isDel != null">
                    #{item.isDel,jdbcType=TINYINT},
                </if>
                <if test="hitsShelvesType != null">
                    #{item.hitsShelvesType,jdbcType=TINYINT},
                </if>
                <if test="shopId != null">
                    #{item.shopId,jdbcType=BIGINT},
                </if>
                <if test="imageUrl != null">
                    #{item.imageUrl,jdbcType=VARCHAR},
                </if>
                <if test="tId != null">
                    #{item.tId,jdbcType=VARCHAR},
                </if>
            </trim>
        </foreach>
    </insert>

<insert id="insertBatch" parameterType="List">
INSERT INTO TStudent(name,age)
<foreach collection="list" item="item" index="index" open="("close=")"separator="union all">
SELECT #{item.name} as a, #{item.age} as b FROM DUAL
</foreach>
</insert>

自己先跑数据库查询 看生成的语句 这没办法看

不知道第一个对不对,太长了,没细看。但第二个是不对的。第二个可以这样写:批量插入
INSERT INTO TStudent(name,age)
values

(#{item.name}, #{item.age} )

你的第一种写法没问题。如果有问题也是不注意的语法错。

第一个对,第二个不对,少了values

第一种是正确的,没毛病

第一种正确、第二种错误


insert into T_REMOTERUSERMODEL


ID_,


CHANNEL_,


CNAME_,


DATETIME_,


DEGREE_,


DEVICEID_,


GATEWAYID_,


ISOPEN_,


MODE_,


SEARCHSUCCESS_,


USERID_,


VOLUME_,


WINDDIRECTION_,


WINDLEVEL_,


REMOTERMODELID_,


ASSOCIATEDEVICEID_,




#{id,jdbcType=NUMERIC},


#{channel,jdbcType=NUMERIC},


#{cname,jdbcType=VARCHAR},


#{datetime,jdbcType=VARCHAR},


#{degree,jdbcType=NUMERIC},


#{deviceid,jdbcType=NUMERIC},


#{gatewayid,jdbcType=NUMERIC},


#{isopen,jdbcType=NUMERIC},


#{mode,jdbcType=NUMERIC},


#{searchsuccess,jdbcType=NUMERIC},


#{userid,jdbcType=NUMERIC},


#{volume,jdbcType=NUMERIC},


#{winddirection,jdbcType=NUMERIC},


#{windlevel,jdbcType=NUMERIC},


#{remotermodelid,jdbcType=NUMERIC},


#{associatedeviceid,jdbcType=NUMERIC},


上一条写错了还不会删,以下——————————————————————————————————————————————


SELECT LAST_INSERT_ID()

    insert into pvmts_alarm_config( id,point_id,user_id,value_one,value_two,value_three,type,message,forbidden,create_date,update_date ) values
    <foreach collection="list" item="item" index="index" separator=",">
    (#{item.id,jdbcType=INTEGER},#{item.pointId,jdbcType=NUMERIC},#{item.userId,jdbcType=NUMERIC},#{item.valueOne,jdbcType=NUMERIC},#{item.valueTwo,jdbcType=NUMERIC},#{item.valueThree,jdbcType=NUMERIC},#{item.type,jdbcType=NUMERIC},#{item.message,jdbcType=VARCHAR},#{item.forbidden,jdbcType=NUMERIC},#{item.createDate},#{item.updateDate} )
    </foreach>
</insert>