mybatis 、mybatis-plus in 大于1000 解决方法

mybatis 、mybatis-plus in 大于1000 解决方法

以下方法都是我开发过程中遇到问题时,在网上收集过来后直接使用或作出调整。

1、mapper.xml

 			<if test="dto.idList != null and dto.idList.size() > 0">
                and (t.id IN
                <foreach collection="dto.idList" index="index" open="(" close=")" item="item" >
                    <if test="index !=0">
                        <choose>
                            <when test="index % 1000 == 999">) OR t.id IN (</when>
                            <otherwise>,</otherwise>
                        </choose>
                    </if>
                    #{item}
                </foreach>
                )
            </if>

2、mybatis-plus

public class MybatisParameterUtils {

    public static <T, F> void cutInParameter(boolean condition, QueryWrapper<T> wrapper, String column, List<F> coll) {
        if (condition) {
            cutInParameter(wrapper, column, coll);
        }
    }

    public static <T, F> void cutInParameter(QueryWrapper<T> wrapper, String column, List<F> coll) {
        List<List<F>> newList = splitList(coll, 900);
        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 cutInParameter(boolean condition, LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) {
        if (condition) {
            cutInParameter(wrapper, column, coll);
        }
    }

    public static <T, F> void cutInParameter(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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) {
        List<List<F>> newList = splitList(coll, 900);
        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;
    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-PlusMyBatis的增强工具,提供了很多实用的功能,其中包括对关联查询的支持。MyBatis-Plus的关联查询主要通过Wrapper对象来实现,下面就来详细介绍一下MyBatis-Plus的关联查询的使用方法。 1. 基本概念 在使用MyBatis-Plus的关联查询之前,我们需要先了解一些基本概念。 1.1. 实体类 实体类是指与数据库表对应的Java类。在使用MyBatis-Plus的关联查询时,我们需要定义实体类,并在实体类中定义与其他表关联的属性。 1.2. Wrapper对象 Wrapper对象是MyBatis-Plus中用于构建SQL语句的查询条件对象,可以通过Wrapper对象实现关联查询。 1.3. QueryWrapper QueryWrapper是Wrapper的一种实现,用于构建查询条件。它可以通过链式调用的方式来构建查询条件,支持多种查询方式,包括等于、不等于、大于、小于、模糊查询等。 1.4. LambdaQueryWrapper LambdaQueryWrapper是QueryWrapper的一种升级版,它支持使用Lambda表达式来构建查询条件,使用起来更加简单、方便。 2. 关联查询的使用方法 了解了基本概念之后,下面就来介绍一下MyBatis-Plus的关联查询的使用方法。 2.1. 一对一关联查询 一对一关联查询是指两张表之间的关联关系是一对一的,例如用户表和身份证表之间的关联关系。 在MyBatis-Plus中,一对一关联查询可以通过selectJoin方法来实现。例如,查询用户表和身份证表中的所有数据,可以使用如下代码: ``` List<User> userList = userMapper.selectJoin(null); ``` 其中,userMapper是MyBatis的Mapper接口,User是用户表对应的实体类。在selectJoin方法中传入的参数为null,表示查询所有数据。 如果需要在查询时指定查询条件,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,例如: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("user_name", "张三"); List<User> userList = userMapper.selectJoin(wrapper); ``` 上述代码中,使用QueryWrapper构建了一个查询条件,查询条件为用户名为“张三”,然后调用selectJoin方法来查询数据。 2.2. 一对多关联查询 一对多关联查询是指两张表之间的关联关系是一对多的,例如用户表和订单表之间的关联关系。 在MyBatis-Plus中,一对多关联查询可以通过selectList方法来实现。例如,查询用户表和订单表中的所有数据,可以使用如下代码: ``` List<User> userList = userMapper.selectList(null); for (User user : userList) { List<Order> orderList = user.getOrderList(); // 处理订单数据 } ``` 在查询用户表数据时,通过getUserList方法获取到了用户对应的订单数据,然后可以对订单数据进行处理。 如果需要在查询时指定查询条件,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,例如: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("user_name", "张三"); List<User> userList = userMapper.selectList(wrapper); ``` 上述代码中,使用QueryWrapper构建了一个查询条件,查询条件为用户名为“张三”,然后调用selectList方法来查询数据。 2.3. 多对多关联查询 多对多关联查询是指两张表之间的关联关系是多对多的,例如学生表和课程表之间的关联关系。 在MyBatis-Plus中,多对多关联查询需要通过中间表来实现。假设学生表和课程表之间的关联关系是通过中间表“学生课程表”来实现的,那么学生表和课程表中的所有数据可以使用如下代码: ``` List<Student> studentList = studentMapper.selectList(null); for (Student student : studentList) { List<Course> courseList = student.getCourseList(); // 处理课程数据 } ``` 在查询学生表数据时,通过getCourseList方法获取到了学生所选的课程数据,然后可以对课程数据进行处理。 如果需要在查询时指定查询条件,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,例如: ``` QueryWrapper<Student> wrapper = new QueryWrapper<>(); wrapper.eq("student_name", "张三"); List<Student> studentList = studentMapper.selectList(wrapper); ``` 上述代码中,使用QueryWrapper构建了一个查询条件,查询条件为学生名为“张三”,然后调用selectList方法来查询数据。 3. 总结 MyBatis-Plus的关联查询主要通过Wrapper对象来实现,支持一对一、一对多和多对多关联查询。在使用关联查询时,需要定义实体类,并在实体类中定义与其他表关联的属性。同时,可以通过QueryWrapper或LambdaQueryWrapper来构建查询条件,在查询时指定查询条件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值