案例一:保存List<Stock>数据
Inventory(盘库指令)中有仓库列表属性List<Stock>以及盘库编码属性invCode,其中Stock中有id、stockCode、stockName、bscCode属性,
向数据库中保存盘库指令对应的仓库列表关系时,映射文件内容如下,其中该方法接收的参数为Inventory实体类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13<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)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108<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类型内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复