一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Sql Server - MyBatis MapperProvider MessageFormat拼接批量SQL語句執行報錯的原因分析及解決辦法

MyBatis MapperProvider MessageFormat拼接批量SQL語句執行報錯的原因分析及解決辦法

2020-04-30 14:42MSSQL教程網 Sql Server

這篇文章主要介紹了MyBatis MapperProvider MessageFormat拼接批量SQL語句執行報錯的原因分析及解決辦法的相關資料,需要的朋友可以參考下

最近在項目中有這么一段代碼:下載服務器基礎業務數據進行本地批量插入操作,因項目中使用mybatis進行持久化操作,故直接考慮使用mybatis的批量插入功能。

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

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

2.對應MapperProvider中函數片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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層

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

程序上線的時候沒有發生問題,在業務量猛增的時候,大約同時執行500條以上的時候程序就開始報錯:

?
1
2
3
4
5
6
7
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進去:

問題根源:

?
1
2
3
4
5
6
7
8
9
10
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為:

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲成年网站在线观看 | 亚洲天天综合网 | 91久久青青青国产免费 | wwwav视频 | 闺蜜的样子小说安沁在线阅读 | 国产亚洲精aa在线观看香蕉 | 青青青手机在线视频 | 国产亚洲精aa在线观看不卡 | 5g影院成人 | 久久国产精品人妻中文 | 精品久久99麻豆蜜桃666 | chinesexxxxhd人妖| 跪在老师脚下吃丝袜脚 | xxxxyoujizz护士| 国产一区视频在线免费观看 | 全黄h全肉细节文在线观看 全彩成人18h漫画 | 紧身短裙女教师波多野 | 涩色网| 99在线观看视频 | ai换脸明星造梦工厂忘忧草 | 亚洲 欧美 国产 综合 播放 | 欧美╳bbbb | 四虎影视国产精品婷婷 | 国产成人高清精品免费5388密 | 双子母性本能在线 | 单亲乱l仑在线观看免费观看 | 2021日本三级理论影院 | 2022最新国产在线不卡a | 精品午夜久久福利大片免费 | 精品国产日韩亚洲一区在线 | 人人做人人爽人人爱 | 毛茸茸的大逼 | 欧美第十页 | 成人国产一区二区 | heyzo1754北岛玲在线视频 | 日本高清免费不卡在线播放 | 国产日韩精品一区二区在线观看播放 | 日本色频 | 憋尿调教绝望之岛 | bbc japanese黑人强行 | 九九热在线视频观看这里只有精品 |