Java开发中会出现一种情况,就是使用mybatis 的in语句的时候,in语句里面的数组大小不能大于1000.
可以通过以下方法解决:
对于一个List ids 大小大于1000的list而言,使用getSumArrayList方法,获取List<List> 对象
List<List<String>> idParams = shareEpdService.getSumArrayList(idParam);
Map<String, Object> mapNew = new HashMap<>();
mapNew.put("ids", idParams );
settlementCostMapper.submitDataByCondition(mapNew)
调用mybatis的方法,做如下处理:
<update id="submitDataByCondition">
UPDATE 表名 SET STATE=1 WHERE STATE IN (-1,0) and
(
????????????????<foreach collection="ids" item="idsItem" separtor="or">
id in
<foreach collection="idsItem" item="id" open="(" separator="," close=")">
#{id}
</foreach>
????</foreach>
??????????)
</update>
getSumArrayList方法如下:
public <T> ?List<List<T>> getSumArrayList(List<T> list){
? ? ? ? List<List<T>> objectlist = new ArrayList<>();
? ? ? ? int iSize = list.size()/1000;
? ? ? ? int iCount = list.size()%1000;
? ? ? ? for(int i=0;i<=iSize;i++){
? ? ? ? ? ? List<T> newObjList = new ArrayList<>();
? ? ? ? ? ? if(i==iSize){
? ? ? ? ? ? ? ? for(int j =i*1000;j<i*1000+iCount;j++ ){
? ? ? ? ? ? ? ? ? ? newObjList.add(list.get(j));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? for(int j =i*1000;j<(i+1)*1000;j++ ){
? ? ? ? ? ? ? ? ? ? newObjList.add(list.get(j));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(newObjList.size()>0){
? ? ? ? ? ? ? ? objectlist.add(newObjList);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return objectlist;
? ? }