mytatisplus,QueryWrapper复杂条件and和or拼接

第一种

条件1 and 条件2 and (条件3 or 条件4 or 条件5)
public void test(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 条件1 and 条件2 and (条件3 or 条件4 or 条件5)
        queryWrapper.eq("字段1", "值1");
        queryWrapper.eq("字段2", "值2");
        queryWrapper.and(qw -> qw.eq("字段3", "值3")
                .or()
                .eq("字段4", "值4")
                .or()
                .eq("字段5", "值5"));
        List userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }
条件1 and 条件2 and (条件3 or 条件4 or 条件5),连在一起
public void test(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 条件1 and 条件2 and (条件3 or 条件4 or 条件5),连在一起
        queryWrapper.eq("字段1", "值1")
                .eq("字段2", "值2")
                .and(qw -> qw.eq("字段3", "值3")
                        .or()
                        .eq("字段4", "值4")
                        .or()
                        .eq("字段5", "值5"));
        List userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }
条件1 and 条件2 and (条件3 or 条件4 or 条件5),条件345在一个集合里,需要循环取
public void test(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 条件1 and 条件2 and (条件3 or 条件4 or 条件5),条件345在一个集合里,需要循环取
        queryWrapper.eq("字段1", "值1");
        queryWrapper.eq("字段2", "值2");
        List list = Arrays.asList("1", "2", "3");
        queryWrapper.and(qw -> {
            for (String s : list) {
                qw.or(q -> q.eq("字段3", s));
            }
        });
        List userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }
条件1 and 条件2 and (条件3 or 条件4 or 条件5),条件345在一个集合里,需要循环取,连在一起
public void test(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 条件1 and 条件2 and (条件3 or 条件4 or 条件5),条件345在一个集合里,需要循环取,连在一起
        List list = Arrays.asList("1", "2", "3");
        queryWrapper.eq("字段1", "值1")
                .eq("字段2", "值2")
                .and(qw -> {
                    for (String s : list) {
                        qw.or(q -> q.eq("字段3", s));
                    }
                });
        List userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }

第二种

条件1 or 条件2 or (条件3 and 条件4 and 条件5)
public void test(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 条件1 or 条件2 or (条件3 and 条件4 and 条件5)
        queryWrapper.eq("字段1", "值1");
        queryWrapper.or(qw -> qw.eq("字段2", "值2"));
        queryWrapper.or(qw -> qw.eq("字段3", "值3").eq("字段4", "值4").eq("字段5", "值5"));
        List userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }
条件1 or 条件2 or (条件3 and 条件4 and 条件5),连在一起
public void test(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 条件1 or 条件2 or (条件3 and 条件4 and 条件5),连在一起
        queryWrapper.eq("字段1", "值1")
                .or(qw -> qw.eq("字段2", "值2"))
                .or(qw -> qw.eq("字段3", "值3").eq("字段4", "值4").eq("字段5", "值5"));
        List userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }

你可能感兴趣的:(java,数据库)