Spring整合MyBatis批量插入

来源:互联网 发布:淘宝联盟显示已收货 编辑:程序博客网 时间:2024/05/16 09:20

之前一直是用的for循环单条插入,后来业务量增大到几万 多的时候,会出现Connection is null的报错,自己也是头一次遇到这种业务情景,记录以下,以后懂得多了会继续更新这篇文章。

结合帖子: 论坛

配置文件

最后得出是配置文件的问题:
连接超时时间
druid.removeAbandonedTimeout=7200
如果你的项目需要运行时间很长,那么这个属性值就需要修改

批量插入

对于大批量插入需要分次。

for (int i = 1; i <= tagsGenerateModel.getTagsCounts(); i++) {                serialNumber ++;                String selfGrowthNo = serialNumberServiceAccess.genSerialNumber(SysConstant.TAGS_BATCH_QR_CODE_TENANTID, "pqms:tags:qrcode"+tagsId);                /** 生成单元识别代码 */                String unitCode = tagsServiceComp.genUnitIdentiCode(tags, pesticideProduct, selfGrowthNo);                //创建二维码                QrCode qrCode = new QrCode();                qrCode.setId(UuidUtils.newid());                qrCode.setEnterpriseId(tagsGenerateModel.getEnterpriseId());                qrCode.setQrCode(unitCode);                qrCode.setScanCount(0);                qrCode.setPesticideProductId(tagsGenerateModel.getProductId());                qrCode.setProductBatchId("");                qrCode.setQrCodeBatchId(tags.getId());                qrCode.setStatus(0); // 状态 0 正常 1 召回  2 作废                qrCode.setQrCodePath("");                qrCode.setCreateTime(new Date());                qrCode.setUpdateTime(new Date());                qrCode.setSerialNumber(serialNumber);                qrCode.setIsDownload("0"); // 是否下载过 0 未下载过  1 下载过                qrCode.setIsActive(String.valueOf(tagsGenerateModel.getActiveMethod())); // 是否激活 0-激活,1-未激活                qrCodes.add(qrCode);                unitCodeLst.add(unitCode);                if (i % 1000 == 0) {//如果当前次数为1000整数倍,则为一个批次,进行批量插入                    qrCodeDao.batchInsert(qrCodes);                    qrCodes.clear();//清空list,准备下一次存储                    long endTime = System.currentTimeMillis();                    logger.info("**第" + (i / 1000) + "次插入1000条数据费时:" + (endTime - startTime) + "毫秒\r\n");                    startTime = System.currentTimeMillis();                }                if (i == tagsGenerateModel.getTagsCounts() && qrCodes.size() > 0) {//如果全部数据 或者 剩余数据                     qrCodeDao.batchInsert(qrCodes);                    qrCodes = null;                    long endTime = System.currentTimeMillis();                    logger.info("**最后的" + (i % 1000) + "条数据费时:" + (endTime - startTime) + "毫秒\r\n");                }            }
<insert id="batchInsert" parameterType="java.util.List">        insert into PQMS_QR_CODE (id, enterprise_id, qr_code,        scan_count,        pesticide_product_id, product_batch_id,        qr_code_batch_id, status,        qr_code_path,        create_time, update_time, serial_number,        is_download,        is_active)        values        <foreach collection="list" item="item" index="index"            separator=",">            (#{item.id,jdbcType=VARCHAR}, #{item.enterpriseId,jdbcType=VARCHAR},            #{item.qrCode,jdbcType=VARCHAR},            #{item.scanCount,jdbcType=INTEGER}, #{item.pesticideProductId,jdbcType=VARCHAR},            #{item.productBatchId,jdbcType=VARCHAR},            #{item.qrCodeBatchId,jdbcType=VARCHAR}, #{item.status,jdbcType=INTEGER},            #{item.qrCodePath,jdbcType=VARCHAR},            #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP},            #{item.serialNumber,jdbcType=INTEGER},            #{item.isDownload,jdbcType=CHAR}, #{item.isActive,jdbcType=CHAR})        </foreach>    </insert>
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星w899 w2020三星手机 w2018三星手机 三星w系列为什么贵 三星w799手机报价及图片 w2014 w2016 三星w2016图片 三星翻盖手机w2016 三星 w2013 w2013 三星w系列 三星w2016多少钱 三星w2015多少钱 w999 三星w系列手机 w2015 三星 w2019 sm-w2016 三星w2016+ 三星w299 三星w589 三星w399 三星w579 2013 2013手机 三星x6 三星x9手机多少钱 三星35x0aa 三星900x3k 三星x208 三星x10笔记本 三星x5手机多少钱 三星300e4x 三星x06屏幕 三星x8 三星x9 三星乡 三星云账户登录 三星云服务 手机登入三星云服务