spring+mybatis实现csv批量上传

阅读更多

简单的一个小例子而已,测试了下9W多条数据导入如果不做批量插入很费时,后改为批量插入在6秒左右:

上传jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>



    
    
    Insert title here
    



springMVC字节流输入上传文件






springMVC包装类上传文件

 action部分,每1000条执行一次

 /**
     * 更新csv数据
     * @return
     */
    @RequestMapping("/updatePayFeeData")
    @ResponseBody
    public Map updatePayFeeData(HttpServletRequest request,HttpServletResponse response){
    	Map map = new HashMap();
		 //创建一个通用的多部分解析器  
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        //判断 request 是否有文件上传,即多部分请求  
        int pre = (int) System.currentTimeMillis();  
        if(multipartResolver.isMultipart(request)){  
            //转换成多部分request    
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            //取得request中的所有文件名  
            Iterator iter = multiRequest.getFileNames();  
            while(iter.hasNext()){  
                try {
	                	MultipartFile file = multiRequest.getFile(iter.next());
	                	InputStream is=file.getInputStream();
	                	CsvUtil test = new CsvUtil(is);
	                	List> csvList = test.readCSVFile();
	                	if (!csvList.isEmpty()) {
//	                		int resNum=payFeeService.deleteData();
	                		payFeeService.deleteData();
//	                		int j=csvList.size();
	                		List feeEntitiesList=new ArrayList();
	                		for (List list : csvList) {
	                			PayFeeEntity payFeeEntity =new PayFeeEntity();
	                			payFeeEntity.setYearMon(list.get(0));
	                			payFeeEntity.setIdno(list.get(1));
	                			payFeeEntity.setMonthPay(Integer.valueOf(list.get(2)));
	                			payFeeEntity.setTotlePayfee(Long.valueOf(list.get(3)));
	                			feeEntitiesList.add(payFeeEntity);
	                			if (feeEntitiesList.size()==1000) {
//	                				int resultNum=payFeeService.insertPayFee(payFeeEntity);
	                				int resultNum=payFeeService.addStudentPath(feeEntitiesList);
	                				feeEntitiesList.clear();
								}
	                		}
	                		if (feeEntitiesList.size()>0) {
//                				int resultNum=payFeeService.insertPayFee(payFeeEntity);
                				int resultNum=payFeeService.addStudentPath(feeEntitiesList);
                				feeEntitiesList.clear();
							}
	                		 int finaltime = (int) System.currentTimeMillis();  
	                         System.out.println(finaltime - pre);  
	                		map.put("code",Constant.getCode(Constant.SUCCESS));
	                		map.put("result","success");
	                		return map;
						}else {
							map.put("code",Constant.getCode(Constant.EMP_FILE));
							map.put("result","false");
							return map;
						}
        		} catch (IOException e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        			map.put("code",Constant.getCode(Constant.FILE_FAIL));
        			map.put("msg",e.getMessage());
        			map.put("result","fail");
        			return map;
        		} catch (Exception e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        			map.put("code",Constant.getCode(Constant.FILE_FAIL));
        			map.put("msg",e.getMessage());
        			map.put("result","fail");
        			return map;
        		}
            }
        }
        map.put("code",Constant.getCode(Constant.FILE_FAIL));
		map.put("result","fail");
        return map;
		
	}

 mapper.xml




	
		
		
		
		
		
	
	
		id, year_mon, idno, month_pay, totle_payfee
	
	
	
		insert into pay_fee_info(year_mon, idno,
		month_pay, totle_payfee)
		values
		  
       		 (#{item.yearMon,jdbcType=VARCHAR},#{item.idno,jdbcType=VARCHAR},#{item.monthPay,jdbcType=INTEGER},#{item.totlePayfee,jdbcType=BIGINT})  
    	  
		

 mapper.java

int addStudentPath(List payFeeList);

 

 

你可能感兴趣的:(spring,mybatis,批量,上传)