使用实体类或Map类型的List作为mybatis参数

案例一:保存List<Stock>数据

Inventory(盘库指令)中有仓库列表属性List<Stock>以及盘库编码属性invCode,其中Stock中有id、stockCode、stockName、bscCode属性,

向数据库中保存盘库指令对应的仓库列表关系时,映射文件内容如下,其中该方法接收的参数为Inventory实体类

<insert id="saveInvStockList">
        INSERT INTO qk_yw_pk_stock(pkcode,stockid,stockcode,stockname,rwjxscode,bsccode)
        <foreach collection="stockList" item="stock" separator="union all">
            SELECT
            #{invCode},
            #{stock.id},
            #{stock.stockCode},
            #{stock.stockName},
            #{stock.rwjxsCode},
            #{stock.bscCode}
            FROM dual
        </foreach>
    </insert>

 

案例二:使用List<Map>传递数据更新车辆信息

应用背景为:接口接收List<Map>类型的数据,Map中存放的是车辆信息,根据车辆信息中的id更新数据库中的车辆信息数据。

直接贴上映射文件的方法的代码,该方法接收的参数为List<Map>数据:效果就是每一个Map都对应执行一个UPDATE语句,UPDATE语句之间用分号分隔(在同一mybatis方法中执行多个sql语句,需要将jdbc配置中加上allowMultiQueries = true,如下:

url: jdbc:mysql://10.2.29.146:3306/ms_inventory?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&allowMultiQueries=true)

<update id="updateTrucks" parameterType="Map">
        <foreach collection="trucks" item="truck" separator=";">
            UPDATE qk_yw_pk_trucks SET
            <if test="truck.containsKey('truckid')">
                truckid = #{truck.truckid},
            </if>
            <if test="truck.containsKey('clzt')">
                clzt = #{truck.clzt},
            </if>
            <if test="truck.containsKey('xsdid')">
                xsdid = #{truck.xsdid},
            </if>
            <if test="truck.containsKey('xsdh')">
                xsdh = #{truck.xsdh},
            </if>
            <if test="truck.containsKey('xszt')">
                xszt = #{truck.xszt},
            </if>
            <if test="truck.containsKey('cxh')">
                cxh = #{truck.cxh},
            </if>
            <if test="truck.containsKey('ywms')">
                ywms = #{truck.ywms},
            </if>
            <if test="truck.containsKey('stockid')">
                stockid = #{truck.stockid},
            </if>
            <if test="truck.containsKey('stockcode')">
                stockcode = #{truck.stockcode},
            </if>
            <if test="truck.containsKey('stockname')">
                stockname = #{truck.stockname},
            </if>
            <if test="truck.containsKey('bscid')">
                bscid = #{truck.bscid},
            </if>
            <if test="truck.containsKey('bsccode')">
                bsccode = #{truck.bsccode},
            </if>
            <if test="truck.containsKey('bscname')">
                bscname = #{truck.bscname},
            </if>
            <if test="truck.containsKey('rwjxsid')">
                rwjxsid = #{truck.rwjxsid},
            </if>
            <if test="truck.containsKey('rwjxscode')">
                rwjxscode = #{truck.rwjxscode},
            </if>
            <if test="truck.containsKey('rwjxsjc')">
                rwjxsjc = #{truck.rwjxsjc},
            </if>
            <if test="truck.containsKey('truck_create_date')">
                <choose>
                    <when test="truck.truck_create_date ==''">
                        truck_create_date = null,
                    </when>
                    <otherwise>
                        truck_create_date = #{truck.truck_create_date},
                    </otherwise>
                </choose>
            </if>
            <if test="truck.containsKey('truck_jnck_date')">
                <choose>
                    <when test="truck.truck_jnck_date ==''">
                        truck_jnck_date = null,
                    </when>
                    <otherwise>
                        truck_jnck_date = #{truck.truck_jnck_date},
                    </otherwise>
                </choose>
            </if>
            <if test="truck.containsKey('truck_zsck_date')">
                <choose>
                    <when test="truck.truck_zsck_date ==''">
                        truck_zsck_date = null,
                    </when>
                    <otherwise>
                        truck_zsck_date = #{truck.truck_zsck_date},
                    </otherwise>
                </choose>
            </if>
            <if test="truck.containsKey('truck_md_date')">
                <choose>
                    <when test="truck.truck_md_date ==''">
                        truck_md_date = null,
                    </when>
                    <otherwise>
                        truck_md_date = #{truck.truck_md_date},
                    </otherwise>
                </choose>
            </if>
            <if test="truck.containsKey('truck_sx_date')">
                <choose>
                    <when test="truck.truck_sx_date ==''">
                        truck_sx_date = null,
                    </when>
                    <otherwise>
                        truck_sx_date = #{truck.truck_sx_date},
                    </otherwise>
                </choose>
            </if>
            <if test="truck.containsKey('del_flag')">
                del_flag = #{truck.del_flag},
            </if>
            remarks = remarks
            WHERE cjh = #{truck.cjh}
        </foreach>
    </update>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值