mybatis in查詢條件過長的解決方案
更新時(shí)間:2021年10月09日 17:01:24 作者:遙遙晚風(fēng)點(diǎn)點(diǎn)
這篇文章主要介紹了mybatis in查詢條件過長的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
mybatis in查詢條件過長的解決
方法1:分次查詢,將參數(shù)且分割成多個(gè)短的查詢后合并
代碼:
int splitNum =(int) Math.ceil( (float) ids.length/999); //切片數(shù)量 List<String> itemIdList = new ArrayList<>(Arrays.asList(ids)); List<List<String>> splitList = averageAssign(itemIdList, splitNum); for (List<String> list : splitList) { param.put("itemIds",list); List<Map<Object, Object>> itemStatisticsList = iProcessExtMapper.getItemStatisticsList(param); result.addAll(itemStatisticsList); }
將list分成N等分方法方法:
public static <T> List<List<T>> averageAssign(List<T> source,int n){ List<List<T>> result=new ArrayList<List<T>>(); int remaider=source.size()%n; //(先計(jì)算出余數(shù)) int number=source.size()/n; //然后是商 int offset=0;//偏移量 for(int i=0;i<n;i++){ List<T> value=null; if(remaider>0){ value=source.subList(i*number+offset, (i+1)*number+offset+1); remaider--; offset++; }else{ value=source.subList(i*number+offset, (i+1)*number+offset); } result.add(value); } return result; }
方法2:xml文件中編寫sql
i.id in <foreach collection="itemIds" index="index" item="item" open="(" close=")"> <if test="index != 0"> <choose> <when test="index % 1000 == 999"> ) OR ID IN( </when> <otherwise>,</otherwise> </choose> </if> #{item} </foreach>
sql邏輯:
ID IN(ids[0],ids[1]+...+ids[998])OR ID IN (ids[999],ids[1000],...ids[max])
mybatis大于1000的in查詢的解決
之前公司一位同事寫的方法:
<select id="getByDirIds" parameterType="string" resultMap="dirDocLinkMap"> SELECT <include refid="columns"/> FROM KM_DIR_DOC_LINK T WHERE T.DIR_ID IN <foreach collection="array" index="index" open="(" close=")" item="item" separator=","> <if test="(index % 1000) == 999">NULL) OR T.DIR_ID IN (</if>#{item} </foreach> </select>
但是隨著數(shù)據(jù)量增加,發(fā)現(xiàn)大于2000這種方法會(huì)報(bào)錯(cuò);
論證如下
解決辦法
<foreach collection="array" item="item" index="index" open="(" close=")" separator=","> <if test="(index % 999) == 998"> NULL ) OR DOC.ID IN (</if>#{item} </foreach>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
maven如何打包動(dòng)態(tài)環(huán)境變量(包括啟動(dòng)腳本)
這篇文章主要介紹了maven如何打包動(dòng)態(tài)環(huán)境變量(包括啟動(dòng)腳本)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Java ThreadPoolExecutor的參數(shù)深入理解
這篇文章主要介紹了Java ThreadPoolExecutor的參數(shù)深入理解的相關(guān)資料,需要的朋友可以參考下2017-03-03SpringBoot JVM參數(shù)調(diào)優(yōu)方式
這篇文章主要介紹了SpringBoot JVM參數(shù)調(diào)優(yōu)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java中使用print、printf、println的示例及區(qū)別
Java?的輸出方式一般有這三種,print、println、printf,它們都是?java.long?包里的System類中的方法,本文重點(diǎn)給大家介紹Java中使用print、printf、println的示例,需要的朋友可以參考下2023-05-05