我是靠谱客的博主 悦耳花瓣,最近开发中收集的这篇文章主要介绍使用实体类或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>

 

最后

以上就是悦耳花瓣为你收集整理的使用实体类或Map类型的List作为mybatis参数的全部内容,希望文章能够帮你解决使用实体类或Map类型的List作为mybatis参数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部