分享好友 数据库首页 频道列表

MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法

SQL Server  2016-01-28 11:220

最近在项目中有这么一段代码:下载服务器基础业务数据进行本地批量插入操作,因项目中使用mybatis进行持久化操作,故直接考虑使用mybatis的批量插入功能。

1.以下是Mapper接口的部分代码

public interface PrintMapper
{
@InsertProvider(type = PrintMapperProvider.class,method = "insertAllLotWithVehicleCode4H2") void insertAllLotWithVehicleCode(List<LotWithVehicleCodeBO> lotWithVehicleCodes);
}

2.对应MapperProvider中函数片段

public String insertAllLotWithVehicleCode4H2(Map<String,List<LotWithVehicleCodeBO>> map)
{
List<LotWithVehicleCodeBO> lotWithVehicleCodeBOs = map.get("list");

StringBuilder sb = new StringBuilder("INSERT INTO MTC_LOT_WITH_VEHICLE_CODE (LOT_CODE,PRODUCT_VEHICLE_CODE) VALUES ");

MessageFormat messageFormat = new MessageFormat("(" +
"#'{'list[{0}].lotCode }," +
"#'{'list[{0}].productVehicleCode }" +
")"); int size = lotWithVehicleCodeBOs.size(); for (int i = 0; i < size; i++)
{
sb.append(messageFormat.format(new Object[]{i})); 
if (i < size - 1) sb.append(",");
} 
return sb.toString();
}

3.service层

@Transactionalpublic void synchLotWithVehicleCodeToLocalDB(List<LotWithVehicleCodeBO> lotWithVehicleCodeBOs)
{ if(null != lotWithVehicleCodeBOs && lotWithVehicleCodeBOs.size()>0)
{
printMapper.insertAllLotWithVehicleCode(lotWithVehicleCodeBOs);
}
}

程序上线的时候没有发生问题,在业务量猛增的时候,大约同时执行500条以上的时候程序就开始报错:

Caused by: org.apache.ibatis.builder.BuilderException: Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}
at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping(SqlSourceBuilder.java:89)
at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken(SqlSourceBuilder.java:43)
at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:25)
at org.apache.ibatis.builder.SqlSourceBuilder.parse(SqlSourceBuilder.java:24)
at org.apache.ibatis.builder.annotation.ProviderSqlSource.createSqlSource(ProviderSqlSource.java:57)
... 61 more

异常已指明SQL语句构建问题,DEBUG进去:

问题根源:

MessageFormat messageFormat = new MessageFormat("(" +
"#'{'list[{0}].lotCode }," +
"#'{'list[{0}].productVehicleCode }," +
")");
int size = lotWithVehicleCodeBOs.size();
for (int i = 0; i < size; i++)
{
   sb.append(messageFormat.format(new Object[]{i})); 
  if (i<size-1) sb.append(",");
}

当size达到3位数以上时构建出的message为:

(#{list[1,000].lotCode },#{list[1,000].productVehicleCode })

解决办法:messageFormat.format(new Object[]{i+""}

查看更多关于【SQL Server】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记
注:本文主要用来记录oracle和mysql数据库在使用mybatis的情况下批量增、删、改(更新)的常用写法一、批量插入1、oracle写法:insert         insert into b_dbgl_zaixcs (zaixcsid, mingc, pingsyid, xinxid, fujid, jieg, pingfjg, pingf, zhuangt, s

0评论2023-02-10590

解决Oracle+Mybatis批量插入报错:SQL 命令未正确结束
Mybatis批量插入需要foreach元素。foreach元素有以下主要属性: (1)item:集合中每一个元素进行迭代时的别名。(2)index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置。(3)collection:根据传入的参数值确定。(4)open:表示该语句以什么开

0评论2023-02-10901

mysql在spring+mybatis中出现value '0000-00-00 00:00:00' can not be represented as java.sql.Time
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp当日期字段在数据库中可为空,并且其默认值为“0000-00-0000:00:00”,在mysql中作为一个特殊值存在。但是java日期类型不认可转换抛出以上异常。解决方法:

0评论2023-02-10816

MyBatis Mapper.xml文件中 MySql的SQL语句比较大小
lt;        小于号gt;      大于号amp;    和apos;    ’单引号quot;    " 双引号           MyBatis 中的 # 和 $ 的区别如下: #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传

0评论2023-02-09501

mybatis 控制台打印出来的sql 执行结果为空 但是将sql放到mysql执行有数据
mybatis中的sql如下select airln_Cd airlnCd,city_coordinate_j cityCoordinateJ,city_coordinate_w cityCoordinateWfrom airportinfonew where iATA=#{iATA} 我在请求的时候是这样加的参数 :/airportFlight/findAirportFlight/?iATA='WDS'  在参数上加了个

0评论2023-02-09640

MyBatis实践之动态SQL及关联查询
MyBatis,大家都知道,半自动的ORM框架,原来叫ibatis,后来好像是10年apache软件基金组织把它托管给了goole code,就重新命名了MyBatis,功能相对以前更强大了。本文给大家介绍MyBatis实践之动态SQL及关联查询,对mybatis动态sql相关知识感兴趣的朋友一起学习吧

0评论2016-04-2895

深入浅析mybatis oracle BLOB类型字段保存与读取
本文给大家浅析mybatis oracle blob类型字段的保存与读取,blob字段是指二进制大对象,用来存储大量文本数据。感兴趣的朋友一起学习吧

1评论2015-11-09524

oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
最近接了一个项目,其中项目需求,有一个非常纠结的问题,由于业务的关系,DB的数据表无法确定,在使用过程中字段可能会增加,这样在insert时给我造成了很大的困扰。接下来,通过本篇文章给大家介绍oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert

0评论2015-11-09399

mybatis的动态sql详解(精)
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下

0评论2015-10-10163

更多推荐