当前位置: 首页 > news >正文

短租网网站开发 项目背景app推广平台

短租网网站开发 项目背景,app推广平台,如何评判网站建设岗位,网站建设常用视频格式在之前的优惠券兑换码需求中,涉及批量写入问题,其中有一个关键的连接配置参数非常重要——rewriteBatchedStatements,当该值配置为true时,Statement将可能对批量插入sql进行重写。 何谓重写?原来提交的批量执行语句&a…

在之前的优惠券兑换码需求中,涉及批量写入问题,其中有一个关键的连接配置参数非常重要——rewriteBatchedStatements,当该值配置为true时,Statement将可能对批量插入sql进行重写。

何谓重写?原来提交的批量执行语句(100条)如下:

INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES ('1','0',100,1);
INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES ('2','0',100,1);
...
INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES ('100','0',100,1);

重写之后的sql语句(1条)如下:

INSERT INTO dh_redeem_code (code, status, coupon_id, batch_id) VALUES 
('1','0',100,1),
('2','0',100,1),
...
('100','0',100,1);

本文将从源码层面来对该参数的作用进行分析。笔者采用的mysql数据库驱动依赖库为mysql-connector-java:8.0.30。

首先,我们来看一段ClientPreparedStatement.executeBatchInternal()方法的代码,这是数据库驱动内部执行批量sql的方法。

if (!this.batchHasPlainStatements && this.rewriteBatchedStatements.getValue()) {if (getQueryInfo().isRewritableWithMultiValuesClause()) {return executeBatchWithMultiValuesClause(batchTimeout);}if (!this.batchHasPlainStatements && this.query.getBatchedArgs() != null&& this.query.getBatchedArgs().size() > 3 /* cost of option setting rt-wise */) {return executePreparedBatchAsMultiStatement(batchTimeout);}
}

关于batchHasPlainStatements属性,如果批处理使用 Statement.addBatch(String) 显示添加sql语句,则该属性值为true,否则为false。原文如下:

/*** Does the batch (if any) contain "plain" statements added by Statement.addBatch(String)?* If so, we can't re-write it to use multi-value or multi-queries.*/
protected boolean batchHasPlainStatements = false;

第一行代码的意思:如果没有使用 Statement.addBatch(String) 显示添加sql语句,并且rewriteBatchedStatements参数值为true,则有可能对批量语句进行重写。

继续往下看,isRewritableWithMultiValuesClause这个属性值为true,则执行executeBatchWithMultiValuesClause方法,该方法实现对sql语句的重写。

对于isRewritableWithMultiValuesClause属性的赋值,QueryInfo类的构造函数中

public QueryInfo(String sql, Session session, String encoding) {// Check if the statement has potential to be rewritten as a multi-values clause statement, i.e., if it is an INSERT or REPLACE statement and// 'rewriteBatchedStatements' is enabled.boolean rewritableAsMultiValues = (isInsert || isReplace) && rewriteBatchedStatements;...        //这里有很长很长很长一段代码this.isRewritableWithMultiValuesClause = rewritableAsMultiValues;
}

从第4行代码看出重写的必要条件:rewriteBatchedStatements值为true,而且必须是插入操作或者REPLACE操作。

再来看看sql语句重写函数executeBatchWithMultiValuesClause(batchTimeout)代码:

batchedStatement = /* FIXME -if we ever care about folks proxying our JdbcConnection */prepareBatchedInsertSQL(locallyScopedConn, numValuesPerBatch);timeoutTask = startQueryTimer(batchedStatement, batchTimeout);numberToExecuteAsMultiValue = numBatchedArgs < numValuesPerBatch ? numBatchedArgs : numBatchedArgs / numValuesPerBatch;int numberArgsToExecute = numberToExecuteAsMultiValue * numValuesPerBatch;for (int i = 0; i < numberArgsToExecute; i++) {if (i != 0 && i % numValuesPerBatch == 0) {try {updateCountRunningTotal += batchedStatement.executeLargeUpdate();} catch (SQLException ex) {sqlEx = handleExceptionForBatch(batchCounter - 1, numValuesPerBatch, updateCounts, ex);}getBatchedGeneratedKeys(batchedStatement);batchedStatement.clearParameters();batchedParamIndex = 1;}batchedParamIndex = setOneBatchedParameterSet(batchedStatement, batchedParamIndex, this.query.getBatchedArgs().get(batchCounter++));
}try {updateCountRunningTotal += batchedStatement.executeLargeUpdate();
} catch (SQLException ex) {sqlEx = handleExceptionForBatch(batchCounter - 1, numValuesPerBatch, updateCounts, ex);
}getBatchedGeneratedKeys(batchedStatement);

上述代码第一行将根据批量sql数量和插入的字段数量,构建带有占位符的sql。随后的语句遍历批量sql,使用实际的插入参数值替换占位符,从而形成一条可执行的批量插入语句。

自此,关于rewriteBatchedStatements的源码分析结束,如果需要数据库驱动重写批量插入,只需要正常使用MybatisPlus的saveBatch方法,并在数据库连接中加上rewriteBatchedStatements=true的配置信息即可。

http://www.khdw.cn/news/64649.html

相关文章:

  • 常州专业网站建设公司咨询百度快照功能
  • 网上有哪些接单做效果图的网站某网站seo策划方案
  • 长沙网络建站网上seo研究
  • 关于做网站的ppt营销策划方案模板
  • 做营销网站哪家好个人做seo怎么赚钱
  • 青岛 网站建设平台推广公众平台营销
  • 网站模板用什么软件做免费的行情软件app网站
  • 建设网站程序下载徐州网站建设方案优化
  • 厦门百度整站优化服务外链发布软件
  • 喜欢做网站的行业市场营销毕业论文5000字
  • 韩国风格网站php源码网络宣传的好处
  • 如何查找做网站的服务商线下推广方法有哪些
  • wordpress 简单企业主题下载地址seo排名方案
  • 横琴注册公司代理百度seo优化规则
  • wordpress编辑器排行seo搜索引擎优化公司
  • 大连装修网站推广seo搜索引擎优化薪资水平
  • 张家港江阴网站设计seo网站排名优化案例
  • 郑州徐州最新seo排名优化是什么意思
  • phpcms v9 网站建设入门宁波seo推广优化公司
  • 网页视频提取seo网站优化公司
  • 公司做网站哪个好关键词优化的建议
  • 网站中二级导航栏怎么做万网域名管理平台
  • phpcms 手机网站后台windows优化大师下载安装
  • 无锡在线制作网站河南关键词排名顾问
  • 合肥网站推广公司seo快速排名上首页
  • 网站招聘栏怎么做咸阳网站建设公司
  • 网站优化过度的表现今日最新新闻
  • 网站的搭建需要多少钱网络营销的基本内容有哪些
  • 黔江网站建设google 网站推广
  • 大型网站建设教程北海百度seo