Mybatis-plus en condiciones de consulta supera los 1000 informes de error

Mybatis-plus en condiciones de consulta supera los 1000 informes de error

ilustrar

El número de opciones que IN puede contener en la base de datos es el siguiente:

 mysql数据库:in语句中参数个数是不限制的。不过对整段sql语句的长度有了限制,
 
	8.0之前的版本默认4M,最大可设置为1G;
	8.0版本之后默认64M,最大可设置1G   (设置max_allowed_packet修改默认大小)。
	
 Oracle数据库:9i(最多256条)、10g(最多1000条)、 11g(最多1000条)

Mybatis-plus en condiciones superiores a 1000 y reporta una solución de error:

Adoptamos el esquema de intercepción en condiciones, como select*from test where nombre en ( ) o nombre en ( ), de la siguiente manera:

1. Cree una clase de herramienta para la interceptación de parámetros

package com.spsolution.hera.parent.jdbc.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xhb 2022-01-07
 **/

public class MybatisParameterUtils {
    
    


    public static <T, F> void cutInParameter(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.in(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().in(column, objects);
            }
        });
    }

    public static <T, F> void cutNotInParameter(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.notIn(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().notIn(column, objects);
            }
        });
    }


    public static <T, F> void cutInParameter(LambdaQueryChainWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.in(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().in(column, objects);
            }
        });
    }

    public static <T, F> void cutNotInParameter(LambdaQueryChainWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.notIn(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().notIn(column, objects);
            }
        });
    }

    public static <T, F> void cutInParameter(LambdaUpdateWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.in(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().in(column, objects);
            }
        });
    }

    public static <T, F> void cutNotInParameter(LambdaUpdateWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.notIn(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().notIn(column, objects);
            }
        });
    }


    public static <T, F> void cutInParameter(LambdaUpdateChainWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.in(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().in(column, objects);
            }
        });
    }

    public static <T, F> void cutNotInParameter(LambdaUpdateChainWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws Exception {
    
    
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
    
    
            throw new Exception("参数错误");
        } else if (newList.size() == 1) {
    
    
            wrapper.notIn(column, newList.get(0));
            return;
        }

        wrapper.and(i -> {
    
    
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
    
    
                i.or().notIn(column, objects);
            }
        });
    }


    public static <F> List<List<F>> splitList(List<F> list, int groupSize) {
    
    
        int length = list.size();
        // 计算可以分成多少组
        int num = (length + groupSize - 1) / groupSize;
        List<List<F>> newList = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
    
    
            // 开始位置
            int fromIndex = i * groupSize;
            // 结束位置
            int toIndex = Math.min((i + 1) * groupSize, length);
            newList.add(list.subList(fromIndex, toIndex));
        }
        return newList;
    }
}

2. Referenciado en el método

		//条件构造器
 		LambdaQueryWrapper<TXxytEnterpriseInfo> queryWrapper = new LambdaQueryWrapper<>();
        
		//普通字段查询        
		queryWrapper.eq(!StringUtils.isEmpty(enterForm.getHylbdm()),TXxytEnterpriseInfo::getHylbdm,enterForm.getHylbdm());
        
        //调用工具类重新拼装in条件               (条件wrapper,            get方法的方法引用,    参数list)                               
        MybatisParameterUtils.cutInParameter(queryWrapper,TXxytEnterpriseInfo::getOrgId, orgIdList);
        
        List<TXxytEnterpriseInfo> list = this.list(queryWrapper);

----------------------- Hasta aquí la solución perfecta ---------------------- - -

Supongo que te gusta

Origin blog.csdn.net/weixin_41377835/article/details/125330472
Recomendado
Clasificación