MyBatis MapperProvider MessageFormat拼接批量SQL语句执行
网络编程 2021-07-05 13:43www.168986.cn编程入门
这篇文章主要介绍了MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法的相关资料,需要的朋友可以参考下
最近在项目中有这么一段代码:下载服务器基础业务数据进行本地批量插入操作,因项目中使用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: .apache.ibatis.builder.BuilderException: Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2} at .apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping(SqlSourceBuilder.java:89) at .apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken(SqlSourceBuilder.java:43) at .apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:25) at .apache.ibatis.builder.SqlSourceBuilder.parse(SqlSourceBuilder.java:24) at .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+""}
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程