亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何對?Excel?表格中提取的數(shù)據(jù)進(jìn)行批量更新

 更新時(shí)間:2024年06月12日 14:25:33   作者:天縱云裳  
這篇文章主要介紹了如何對Excel表格中提取的數(shù)據(jù)進(jìn)行批量更新操作,本文通過示例代碼介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

書接上回,這篇文章主要來講述如何對 Excel 表格中提取的數(shù)據(jù)進(jìn)行批量更新操作

一.編寫查詢代碼

目的是根據(jù)工號, 將其對應(yīng)的 Flag 變?yōu)?1 ,那么 mapper 中我們可以這樣做

//Mapper層
    public int updateFlagByEmployeeID(@Param("ids") List<String> ids);
//XML文件中
    <update id="updateFlagByEmployeeID">
        update OA_JDY_Personnel_Info_Test set enableFlag = 1
        where employeeID in
        <foreach collection="ids" separator="," item="employeeID" open="(" close=")">
            #{employeeID}
        </foreach>
    </update>

二.編寫 Service 層代碼

我們從 String response = " " 往下看,這段代碼之前的在上篇博客解釋了,主要是進(jìn)行數(shù)據(jù)解析

?
    public String enableFlagUpdate(String filename) {
        //獲取 PersonSynData 類的 Class 對象。
        Class<PersonSynData> head = PersonSynData.class;
        List<PersonSynData> updateList = new ArrayList<>();
        ExcelReader excelReader = EasyExcel.read(filename, head, new AnalysisEventListener<PersonSynData>() {
            @Override
            public void invoke(PersonSynData personSynData, AnalysisContext analysisContext) {
                updateList.add(personSynData);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("Excel解析完成......");
            }
        }).build();
        //創(chuàng)建 sheet 對象,并且讀取Excel的第一個(gè)sheet(下表從0開始),也可以根據(jù) sheet 名稱獲取
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        //讀取 sheet 表格數(shù)據(jù),參數(shù)是可變參數(shù),也可以讀取多個(gè)sheet
        //這個(gè)操作會(huì)讀取 excel 表格的數(shù)據(jù)
        excelReader.read(readSheet);
        //需要自己關(guān)閉流操作,在讀取文件的時(shí)候會(huì)創(chuàng)建臨時(shí)文件,如果不關(guān)閉的話,會(huì)損耗磁盤
        excelReader.finish();
        String success = "";
        //創(chuàng)建更新集合
        List<String> employeeIDs = new ArrayList<>();
        for(int i = 0; i < updateList.size(); i++){
            //這里可以拿到表格內(nèi)的所有數(shù)據(jù),根據(jù)表格中的員工號,批量修改Flag
            String employeeID = updateList.get(i).getEmployeeID();
            employeeIDs.add(employeeID);
        }
        int totalUpdatedCount = 0;
        for (int i = 0; i < employeeIDs.size(); i += BATCH_SIZE) {
            List<String> batchIds = employeeIDs.subList(i, Math.min(i + BATCH_SIZE, employeeIDs.size()));
            int updatedCount = jdcloudDao.updateFlagByEmployeeID(batchIds);
            totalUpdatedCount += updatedCount;
        }
        //進(jìn)行批量更新 每次只更新 2008條
        //int count = jdcloudDao.updateFlagByEmployeeID(employeeIDs);
        return "更新成功的條數(shù):" + totalUpdatedCount + "<br>";
    }
?

 1. List<String> employeeIDs = new ArrayList<>(); 這段代碼在經(jīng)過下面的 for 循環(huán)后,里面存儲的是每一列的信息,包括 姓名,工號,F(xiàn)lag 

2. 下面的 totalUpdatedCount 是用來記錄更新成功的總條數(shù)

3.List<String> batchIds = employeeIDs.subList(i, Math.min(i + BATCH_SIZE, employeeIDs.size())); 最為關(guān)鍵的代碼就是這個(gè),它的意思是從結(jié)果集合 employeeIDs 中截取指定長度的元素集合,每次取的都是 i + BATCH_SIZE(2000)大小的元素。舉個(gè)例子,假如出文件中的數(shù)據(jù)有 4200 條

第一次:employeeIDs.subList(0, Math.min(2000, 4200));   截取了 0 ~ 2000的數(shù)據(jù)進(jìn)行更新

第二次:employeeIDs.subList(2000, Math.min(4000, 4200)); 截取了 2000~4000 的數(shù)據(jù)進(jìn)行更新

第三次:employeeIDs.subList(4000, Math.min(6000, 4200)); 截取了 4000 ~ 4200 的數(shù)據(jù)更新

其實(shí)最關(guān)鍵的就是第三次,這段代碼保證了如果最后一次的數(shù)據(jù)不夠  BATCH_SIZE(2000)那么會(huì)截取到末尾的位置,來保證不會(huì)越界。

三.為什么要進(jìn)行批量更新

在Mybatis和SqlServer數(shù)據(jù)源情況下,如果一次更新超過了 2100 條,會(huì)報(bào):The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.; nested exception isxxxx  的錯(cuò)誤

也就是它不支持一次更新太多的數(shù)據(jù),所以就有了 Service 層中的批量更新操作

到此這篇關(guān)于如何對 Excel 表格中提取的數(shù)據(jù)進(jìn)行批量更新的文章就介紹到這了,更多相關(guān)Excel 表格數(shù)據(jù)批量更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論