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

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

热门标签:台湾外呼系统软件 疫情时期电话机器人 濮阳清丰400开头的电话申请 南通智能外呼系统怎么样 真3地图标注 乐昌电话机器人 地图标注跑线下市场 地图标注可以编辑地名吗 南京怎么申请400这种电话

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

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

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

2.对应MapperProvider中函数片段

public String insertAllLotWithVehicleCode4H2(MapString,ListLotWithVehicleCodeBO>> map)
{
ListLotWithVehicleCodeBO> 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(ListLotWithVehicleCodeBO> 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 (isize-1) sb.append(",");
}

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

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

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

您可能感兴趣的文章:
  • Mybatis 中 Oracle 的拼接模糊查询及用法详解
  • MyBatis 动态拼接Sql字符串的问题
  • 解决myBatis中删除条件的拼接问题

标签:南京 马鞍山 通辽 广安 阿里 陕西 福建 河北

巨人网络通讯声明:本文标题《MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法》,本文关键词  MyBatis,MapperProvider,MessageFormat,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法》相关的同类信息!
  • 本页收集关于MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法的相关信息资讯供网民参考!
  • 推荐文章