Mybatis動(dòng)態(tài)拼接sql提高插入速度實(shí)例
Mybatis動(dòng)態(tài)拼接sql提高插入速度
實(shí)驗(yàn)室的項(xiàng)目,Excel文件的數(shù)據(jù)入庫(kù),原始錄入語(yǔ)句如下:
void insertCaculatePlanData(@Param("excelName") String excelName, @Param("CaculatePlanColumnName") List<String>CaculatePlanColumnName, @Param("InsertIntoCaculatePlanData")List<Object> InsertIntoCaculatePlanData);
<insert id="insertCaculatePlanData"> insert into ${excelName} ( <foreach item="item" collection="CaculatePlanColumnName" separator=","> ${item} </foreach> ) values ( <foreach item="it" collection="InsertIntoCaculatePlanData" separator=","> #{it} </foreach> ) </insert>
其中,表名是參數(shù)、列名是參數(shù)、列值是參數(shù),每來(lái)一行數(shù)據(jù),就執(zhí)行插入一行的數(shù)據(jù)。
當(dāng)數(shù)據(jù)量少的時(shí)候,沒(méi)問(wèn)題,有效時(shí)間內(nèi)可能完成插入,但是當(dāng)數(shù)據(jù)量達(dá)到一定程度的時(shí)候,每次都一個(gè)sql插入,超時(shí)。
所以采用了拼接sql的方式加快速度:
void insertCaculatePlanDataBetter(@Param("excelName") String excelName, @Param("CaculatePlanColumnName") List<String>CaculatePlanColumnName, @Param("InsertIntoCaculatePlanData")List<List<Object>> InsertIntoCaculatePlanData);
<!--優(yōu)化的拼接插入語(yǔ)句--> <insert id="insertCaculatePlanDataBetter"> INSERT INTO ${excelName}( <foreach item="item" collection="CaculatePlanColumnName" separator=","> ${item} </foreach> ) VALUES <foreach item="o" collection="InsertIntoCaculatePlanData" separator=",">( <foreach item="itm" collection="o" separator=","> #{itm} </foreach> ) </foreach> </insert>
到此這篇關(guān)于Mybatis動(dòng)態(tài)拼接sql提高插入速度實(shí)例的文章就介紹到這了,更多相關(guān)Mybatis動(dòng)態(tài)sql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java與Mysql鎖相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Java與Mysql鎖相關(guān)知識(shí)總結(jié)的相關(guān)資料,需要的朋友可以參考下2023-04-04java導(dǎo)出pdf文件的詳細(xì)實(shí)現(xiàn)方法
這篇文章主要介紹了java導(dǎo)出pdf文件的詳細(xì)實(shí)現(xiàn)方法,包括制作模板、獲取中文字體文件、實(shí)現(xiàn)后端服務(wù)以及前端發(fā)起請(qǐng)求并生成下載鏈接,需要的朋友可以參考下2025-03-03SpringBoot實(shí)現(xiàn)多端口監(jiān)聽(tīng)的代碼示例
當(dāng)你需要在同一個(gè)Spring Boot應(yīng)用中,通過(guò)不同的端口來(lái)提供不同的服務(wù)或功能時(shí),就需要實(shí)現(xiàn)多端口監(jiān)聽(tīng),所以本文給大家介紹了SpringBoot實(shí)現(xiàn)多端口監(jiān)聽(tīng)的方法示例,并有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-09-09Swagger實(shí)現(xiàn)動(dòng)態(tài)條件注入與全局?jǐn)r截功能詳細(xì)流程
這篇文章主要介紹了Swagger實(shí)現(xiàn)動(dòng)態(tài)條件注入與全局?jǐn)r截功能詳細(xì)流程,Swagger 可以提供 API 操作的測(cè)試文檔,本文記錄 Swagger 使用過(guò)程中遇到的小問(wèn)題2023-01-01