千家信息网

lambdaQueryWrapper多条件嵌套查询方法是什么

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,这篇文章主要讲解了"lambdaQueryWrapper多条件嵌套查询方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"lambdaQueryW
千家信息网最后更新 2025年01月31日lambdaQueryWrapper多条件嵌套查询方法是什么

这篇文章主要讲解了"lambdaQueryWrapper多条件嵌套查询方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"lambdaQueryWrapper多条件嵌套查询方法是什么"吧!

    lambdaQueryWrapper多条件嵌套查询

    需求:根据条件获取一段时期内按照年份和周存储的数据

    表结构如下

    userNetTypemoduleNamecityNamesubjectCnamesubjectEnamepvuvyearweek
    11江苏省死神专题sszt10070201951
    11江苏省海贼王专题hzwzt20080201952
    11江苏省火影忍者专题hyrzzt30090202001
    /**     * @param pageNum     当前页     * @param pageSize    每页条数     * @param moduleName  产品     * @param userNetType 运营商     * @param cityName    城市名称     * @param beginTime   开始周的任意一天日期(例如:2019-12-20)     * @param endTime     结束周的任意一天日期(例如:2020-01-20)     * @param keyWord     查询条件(专题英文名或者中文名)     * @return     */        //开始年份        String beginYear = null;        //结束年份        String endYear = null;        //开始周数        String beginWeek = null;        //结束周数        String endWeek = null;        /*这部分内容忽略,调用了其他的方法,         反正就是为了获取开始日期所在的年份、周数以及结束日期所在的年份、周数*/        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime)) {            DateTime dateTime = DateUtil.lastWeek();            //格式化日期,结果:yyyyMMdd            beginTime = DateUtil.formatDate(dateTime);            beginYear = TimeUtils.getDateOfYearWeek(beginTime).get("year");            endYear = beginYear;            beginWeek = TimeUtils.getDateOfYearWeek(beginTime).get("week");            endWeek = beginWeek;        } else {            beginYear = TimeUtils.getDateOfYearWeek(beginTime).get("year");            endYear = TimeUtils.getDateOfYearWeek(endTime).get("year");            beginWeek = TimeUtils.getDateOfYearWeek(beginTime).get("week");            endWeek = TimeUtils.getDateOfYearWeek(endTime).get("week");        }        Page page = new Page<>(pageNum, pageSize);        LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery();        if (beginYear.equals(endYear)) {            queryWrapper.eq(DwSubjectDataInfoWw::getYear, beginYear);            queryWrapper.between(DwSubjectDataInfoWw::getWeek, beginWeek, endWeek);        } else {            //因为Java8 Lambda表达式中最终变量问题,重新赋值一个参数解决            String year1 = beginYear;            String year2 = endYear;            String week1 = beginWeek;            String week2 = endWeek;            queryWrapper.and(wrapper -> wrapper.and(wrapper1 -> wrapper1.eq(DwSubjectDataInfoWw::getYear, year1).ge(DwSubjectDataInfoWw::getWeek, week1))                    .or(wrapper2 -> wrapper2.eq(DwSubjectDataInfoWw::getYear, year2).le(DwSubjectDataInfoWw::getWeek, week2)));        }        queryWrapper.orderByDesc(DwSubjectDataInfoWw::getYear);        queryWrapper.orderByDesc(DwSubjectDataInfoWw::getWeek);        if (StrUtil.isNotEmpty(cityName)) {            queryWrapper.eq(DwSubjectDataInfoWw::getCityName, cityName);        }        if (StrUtil.isNotEmpty(userNetType)) {            queryWrapper.eq(DwSubjectDataInfoWw::getUserNetType, userNetType);        }        if (StrUtil.isNotEmpty(moduleName)) {            queryWrapper.eq(DwSubjectDataInfoWw::getModuleName, moduleName);        }        //搜索条件可以是专题中文名或英文名        if (StrUtil.isNotEmpty(keyWord)) {            queryWrapper.and(wrapper -> wrapper.like(DwSubjectDataInfoWw::getSubjectCname, keyWord).or().like(DwSubjectDataInfoWw::getSubjectEname, keyWord));        }        try {            Page list = dwSubjectDataInfoWwService.page(page, queryWrapper);            return AjaxResult.success(list);        } catch (Exception e) {            logger.error("获取分周专题数据列表错误,错误信息为:", e);            return AjaxResult.error();        }

    下面是根据条件生成的SQL语句

    WHERE    (        (            (year = ? AND week >= ?)            OR (year = ? AND week <= ?)        )        AND city_name = ?        AND user_net_type = ?        AND module_name = ?        AND (            subject_cname LIKE ?            OR subject_ename LIKE ?        )    )ORDER BY    year DESC,    week DESC

    LambdaQueryWrapper 常用条件

    感谢各位的阅读,以上就是"lambdaQueryWrapper多条件嵌套查询方法是什么"的内容了,经过本文的学习后,相信大家对lambdaQueryWrapper多条件嵌套查询方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

    0