zoukankan      html  css  js  c++  java
  • SpringMVC接收对象数组+MyBatis批量处理

    Controller类代码如下:

    @ResponseBody
        @RequestMapping(value="/ecg/addEcgs", method=RequestMethod.POST)
        public Map<String, Object> addEcgs(@RequestBody List<EcgBean> ecgBeans){
            Map<String, Object> modelMap = new HashMap<String, Object>();
            ResponseMsg responseMsg = new ResponseMsg();
            System.out.println(ecgBeans.toString());
            if (null == ecgBeans || ecgBeans.size() == 0) {
                responseMsg.set(2, "参数校验失败", null);
                modelMap.put("ResponseMsg", responseMsg);
                return modelMap;
            }
            if(ecgService.addEcgs(ecgBeans)){
                responseMsg.set(1, "批量添加成功", null);
                modelMap.put("ResponseMsg", responseMsg);
            } else {
                responseMsg.set(2, "批量添加失败", null);
                modelMap.put("ResponseMsg", responseMsg);
            }
            
            return modelMap;
        }

    Ajax代码如下:

    <script type="text/javascript">
            $(document).ready(function(){
                var arr = new Array();
                var data = {
                    oldPeopleId:1,
                    agencyId:1,
                    ecg:1,
                    inspectTime:111,
                    inspectStaffId:1,
                    entryTime:111,
                    entryStaffId:1,
                    scene:1,
                    approvalStatus:1,
                    createTime:111,
                    updateTime:111,
                    reserved:1
                        
                };
                arr.push(data);
                arr.push(data);
                alert(JSON.stringify(arr));
                $.ajax({  
                    type: 'POST',  
                    url: 'singndata/ecg/addEcgs',  
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',  
                    data: JSON.stringify(arr),  
                    success: function(data){
                        console.debug(data);
                    },  
                    error: function(err){  
                        console.debug(data); 
                    }  
                });  
                
            });
    
        </script>

    MyBatis配置文件SQL定义如下:

       <insert id="addEcgs" parameterType="java.util.List">
            INSERT INTO lefuyun.tbl_ecg (
                oldPeopleId, 
                agencyId, 
                ecg, 
                inspectTime, 
                inspectStaffId, 
                entryTime, 
                entryStaffId, 
                scene, 
                approvalStatus, 
                createTime, 
                updateTime, 
                reserved
            ) VALUES 
            <foreach collection="list" item="item" index="index" separator=",">
                (
                    #{item.oldPeopleId,jdbcType=NUMERIC},
                    #{item.agencyId,jdbcType=NUMERIC}, 
                    #{item.ecg,jdbcType=VARCHAR}, 
                    #{item.inspectTime,jdbcType=NUMERIC}, 
                    #{item.inspectStaffId,jdbcType=NUMERIC}, 
                    #{item.entryTime,jdbcType=NUMERIC}, 
                    #{item.entryStaffId,jdbcType=NUMERIC}, 
                    #{item.scene,jdbcType=NUMERIC}, 
                    #{item.approvalStatus,jdbcType=NUMERIC}, 
                    #{item.createTime,jdbcType=NUMERIC}, 
                    #{item.updateTime,jdbcType=NUMERIC},
                    #{item.reserved,jdbcType=VARCHAR}
                )
            </foreach>
       </insert> 
  • 相关阅读:
    python内置函数枚举 enumerate()
    python内置函数map的介绍
    什么是lambda函数
    python urllib库 加密及解析url中中文汉字
    python解决高并发思路
    后端文件保存的两种方式
    matplotlib基本用法
    自编码器
    数据增强
    卷积神经网络
  • 原文地址:https://www.cnblogs.com/mengyao/p/4931523.html
Copyright © 2011-2022 走看看