MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法
16lz
2022-01-08
最近在项目中有这么一段代码:下载服务器基础业务数据进行本地批量插入操作,因项目中使用mybatis进行持久化操作,故直接考虑使用mybatis的批量插入功能。
1.以下是Mapper接口的部分代码
public interface PrintMapper{@InsertProvider(type = PrintMapperProvider.class,method = "insertAllLotWithVehicleCode4H2") void insertAllLotWithVehicleCode(List<LotWithVehicleCodeBO> lotWithVehicleCodes);}
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();}
@Transactionalpublic void synchLotWithVehicleCodeToLocalDB(List<LotWithVehicleCodeBO> lotWithVehicleCodeBOs){ if(null != lotWithVehicleCodeBOs && lotWithVehicleCodeBOs.size()>0){printMapper.insertAllLotWithVehicleCode(lotWithVehicleCodeBOs);}}
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
问题根源:
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(",");}
(#{list[1,000].lotCode },#{list[1,000].productVehicleCode })
更多相关文章
- Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
- Ionic 运行报错No resource identifier found for attribute 'ap
- Android,LIstView中的OnItemClick点击无效的解决办法
- 华为手机Android(安卓)Studio开发不显示Logcat解决办法
- 安卓9.0 http请求数据失败解决办法
- Android(安卓)Studio 导入包时报 Duplicate files copied in APK
- Android(安卓)启动Tomcat服务报错,端口占用解决方案
- Android(安卓)启动时闪一下黑屏问题的解决办法
- Android(安卓)项目混编flutter报错