lambdaQueryWrapper多條件嵌套查詢方式

lambdaQueryWrapper多條件嵌套查詢

需求:根據條件獲取一段時期內按照年份和周存儲的數據

表結構如下

userNetType moduleName cityName subjectCname subjectEname pv uv year week
1 1 江蘇省 死神專題 sszt 100 70 2019 51
1 1 江蘇省 海賊王專題 hzwzt 200 80 2019 52
1 1 江蘇省 火影忍者專題 hyrzzt 300 90 2020 01
/**
     * @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<DwSubjectDataInfoWw> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DwSubjectDataInfoWw> queryWrapper = Wrappers.<DwSubjectDataInfoWw>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<DwSubjectDataInfoWw> 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 常用條件

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: