佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MyBatis MapperProvider MessageFormat拼接批量SQL語句執(zhí)行報錯的原因分析及解決辦法

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

熱門標(biāo)簽:AI電銷 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站 Linux服務(wù)器 服務(wù)外包 呼叫中心市場需求 百度競價排名 鐵路電話系統(tǒng)

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

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

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

2.對應(yīng)MapperProvider中函數(shù)片段

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);
}
}

程序上線的時候沒有發(fā)生問題,在業(yè)務(wù)量猛增的時候,大約同時執(zhí)行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語句構(gòu)建問題,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(",");
}

當(dāng)size達(dá)到3位數(shù)以上時構(gòu)建出的message為:

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

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

您可能感興趣的文章:
  • Mybatis 中 Oracle 的拼接模糊查詢及用法詳解
  • MyBatis 動態(tài)拼接Sql字符串的問題
  • 解決myBatis中刪除條件的拼接問題

標(biāo)簽:黃山 衡水 蘭州 湖南 湘潭 仙桃 崇左 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MyBatis MapperProvider MessageFormat拼接批量SQL語句執(zhí)行報錯的原因分析及解決辦法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    寻甸| 巨鹿县| 长顺县| 方正县| 翁牛特旗| 成安县| 图片| 邹平县| 武强县| 慈利县| 安新县| 天门市| 昌江| 顺义区| 桐庐县| 治县。| 阳山县| 黄平县| 台湾省| 文安县| 康乐县| 关岭| 凤冈县| 毕节市| 思南县| 绥宁县| 荆州市| 浦城县| 无为县| 新平| 泌阳县| 西乌| 长沙县| 游戏| 梁山县| 巴马| 通辽市| 东兴市| 巴塘县| 紫阳县| 伽师县|