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> 
  • 相关阅读:
    P1026 统计单词个数
    常见的DP优化类型
    中国空气质量在线监測分析平台
    Linux Kernel系列一:开篇和Kernel启动概要
    Android入门第八篇之GridView(九宫图)
    eclipse+webservice开发实例
    【java读书笔记】——java开篇宏观把控 + HelloWorld
    选择排序
    The encryption certificate of the relying party trust identified by thumbprint is not valid
    【传递正能量】2014年感动我的十大微视频
  • 原文地址:https://www.cnblogs.com/mengyao/p/4931523.html
Copyright © 2011-2022 走看看