当前位置:Gxlcms > mssql > MyBatisMapperProviderMessageFormat拼接批量SQL语句执行报错的原因分析及解决办法

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

时间:2021-07-01 10:21:17 帮助过:37人阅读

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

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

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

2.对应MapperProvider中函数片段

  1. public String insertAllLotWithVehicleCode4H2(Map<String,List<LotWithVehicleCodeBO>> map)
  2. {
  3. List<LotWithVehicleCodeBO> lotWithVehicleCodeBOs = map.get("list");
  4. StringBuilder sb = new StringBuilder("INSERT INTO MTC_LOT_WITH_VEHICLE_CODE (LOT_CODE,PRODUCT_VEHICLE_CODE) VALUES ");
  5. MessageFormat messageFormat = new MessageFormat("(" +
  6. "#'{'list[{0}].lotCode }," +
  7. "#'{'list[{0}].productVehicleCode }" +
  8. ")"); int size = lotWithVehicleCodeBOs.size(); for (int i = 0; i < size; i++)
  9. {
  10. sb.append(messageFormat.format(new Object[]{i}));
  11. if (i < size - 1) sb.append(",");
  12. }
  13. return sb.toString();
  14. }

3.service层

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

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

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

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

问题根源:

  1. MessageFormat messageFormat = new MessageFormat("(" +
  2. "#'{'list[{0}].lotCode }," +
  3. "#'{'list[{0}].productVehicleCode }," +
  4. ")");
  5. int size = lotWithVehicleCodeBOs.size();
  6. for (int i = 0; i < size; i++)
  7. {
  8.    sb.append(messageFormat.format(new Object[]{i}));
  9.   if (i<size-1) sb.append(",");
  10. }

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

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

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

您可能感兴趣的文章:

  • Java的MyBatis框架中Mapper映射配置的使用及原理解析
  • MyBatis实践之DAO与Mapper
  • Mybatis增删改查mapper文件写法详解
  • 使用Mybatis Generator结合Ant脚本快速自动生成Model、Mapper等文件的方法
  • MyBatis Mapper代理使用方法详解
  • 详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
  • Mybatis中SqlMapper配置的扩展与应用详细介绍(1)
  • mybatis的动态sql详解(精)
  • oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
  • Mybatis实现Mapper动态代理方式详解

人气教程排行