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

案例一:保存List数据

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

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


        INSERT INTO qk_yw_pk_stock(pkcode,stockid,stockcode,stockname,rwjxscode,bsccode)
        
            SELECT
            #{invCode},
            #{stock.id},
            #{stock.stockCode},
            #{stock.stockName},
            #{stock.rwjxsCode},
            #{stock.bscCode}
            FROM dual
        
    

 

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

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

直接贴上映射文件的方法的代码,该方法接收的参数为List数据:效果就是每一个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 qk_yw_pk_trucks SET
            
                truckid = #{truck.truckid},
            
            
                clzt = #{truck.clzt},
            
            
                xsdid = #{truck.xsdid},
            
            
                xsdh = #{truck.xsdh},
            
            
                xszt = #{truck.xszt},
            
            
                cxh = #{truck.cxh},
            
            
                ywms = #{truck.ywms},
            
            
                stockid = #{truck.stockid},
            
            
                stockcode = #{truck.stockcode},
            
            
                stockname = #{truck.stockname},
            
            
                bscid = #{truck.bscid},
            
            
                bsccode = #{truck.bsccode},
            
            
                bscname = #{truck.bscname},
            
            
                rwjxsid = #{truck.rwjxsid},
            
            
                rwjxscode = #{truck.rwjxscode},
            
            
                rwjxsjc = #{truck.rwjxsjc},
            
            
                
                    
                        truck_create_date = null,
                    
                    
                        truck_create_date = #{truck.truck_create_date},
                    
                
            
            
                
                    
                        truck_jnck_date = null,
                    
                    
                        truck_jnck_date = #{truck.truck_jnck_date},
                    
                
            
            
                
                    
                        truck_zsck_date = null,
                    
                    
                        truck_zsck_date = #{truck.truck_zsck_date},
                    
                
            
            
                
                    
                        truck_md_date = null,
                    
                    
                        truck_md_date = #{truck.truck_md_date},
                    
                
            
            
                
                    
                        truck_sx_date = null,
                    
                    
                        truck_sx_date = #{truck.truck_sx_date},
                    
                
            
            
                del_flag = #{truck.del_flag},
            
            remarks = remarks
            WHERE cjh = #{truck.cjh}
        
    

 

你可能感兴趣的:(使用实体类或Map类型的List作为mybatis参数)