mybatis bulk import, all is not unusual to add (rollback)

public int importModel(MultipartFile file) throws Exception{
        String fileName = file.getOriginalFilename();
        String suffxName = fileName.substring(fileName.lastIndexOf(".",fileName.length()) );
        //System.out.println(fileName+",后缀名:"+suffxName);
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        final File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
         //批量处理
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
        commericalModelMapper = session.getMapper(CommericalModelMapper.class);
        // MultipartFile to File
        file.transferTo(excelFile);
        //判断excelp版本
        Workbook = null the Workbook; 
        IF (suffxName.equals ( "XLSX.")) { 
            Workbook new new XSSFWorkbook = (new new the FileInputStream (ExcelFile)); 
        } {the else 
            Workbook new new HSSFWorkbook = (new new the FileInputStream (ExcelFile)); 
        } 
        int COUNT = 0 ; 
        IF (Workbook = null!) { 
            // get the data in excel, is converted to the entity class 
            Sheet Sheet workbook.getSheetAt = (0); 
            List <CommericalModel> commericalModelList = CommericalModelResult.excelToModel (Sheet); 
            int = 1000 limitCount; 
            / / batch operation is added (with an unsuccessful rolled back) 
            IF (commericalModelList! = null && commericalModelList.size ()> 0) {
                for(int i = 0 ; i < commericalModelList.size();i++){
                    CommericalModel commericalModel = commericalModelList.get(i);
                    commericalModel.setCreateDate(new Date());
                    commericalModel.setUpdateDate(new Date());
                    commericalModel.setModelIsDelete((short)1);
                    commericalModelMapper.insert(commericalModel);
                    /*if(i != 0 && i%limitCount == 0){
                        //数量达到1000提交一次
                        session.commit();
                    } */
                    count++;
                }
                session.commit();
            }
        } 
        // delete temporary files 
        IF (excelFile.exists ()) { 
            excelFile.delete (); 
        } 
        return COUNT; 
    }

  

The session = sqlSessionFactory.openSession the SqlSession (ExecutorType.BATCH, to false); 
commericalModelMapper = session.getMapper (CommericalModelMapper.class);

commericalModelMapper.insert (commericalModel);
when the added use, introduced in batches, can be included in that line is wrong returns; successfully added the number of rows can be processed

Guess you like

Origin www.cnblogs.com/lazyli/p/11511110.html