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

    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
    1. /**
    2. * @param pageNum 当前页
    3. * @param pageSize 每页条数
    4. * @param moduleName 产品
    5. * @param userNetType 运营商
    6. * @param cityName 城市名称
    7. * @param beginTime 开始周的任意一天日期(例如:2019-12-20)
    8. * @param endTime 结束周的任意一天日期(例如:2020-01-20)
    9. * @param keyWord 查询条件(专题英文名或者中文名)
    10. * @return
    11. */
    12. //开始年份
    13. String beginYear = null;
    14. //结束年份
    15. String endYear = null;
    16. //开始周数
    17. String beginWeek = null;
    18. //结束周数
    19. String endWeek = null;
    20. /*这部分内容忽略,调用了其他的方法,
    21. 反正就是为了获取开始日期所在的年份、周数以及结束日期所在的年份、周数*/
    22. if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime)) {
    23. DateTime dateTime = DateUtil.lastWeek();
    24. //格式化日期,结果:yyyyMMdd
    25. beginTime = DateUtil.formatDate(dateTime);
    26. beginYear = TimeUtils.getDateOfYearWeek(beginTime).get("year");
    27. endYear = beginYear;
    28. beginWeek = TimeUtils.getDateOfYearWeek(beginTime).get("week");
    29. endWeek = beginWeek;
    30. } else {
    31. beginYear = TimeUtils.getDateOfYearWeek(beginTime).get("year");
    32. endYear = TimeUtils.getDateOfYearWeek(endTime).get("year");
    33. beginWeek = TimeUtils.getDateOfYearWeek(beginTime).get("week");
    34. endWeek = TimeUtils.getDateOfYearWeek(endTime).get("week");
    35. }
    36. Page<DwSubjectDataInfoWw> page = new Page<>(pageNum, pageSize);
    37. LambdaQueryWrapper<DwSubjectDataInfoWw> queryWrapper = Wrappers.<DwSubjectDataInfoWw>lambdaQuery();
    38. if (beginYear.equals(endYear)) {
    39. queryWrapper.eq(DwSubjectDataInfoWw::getYear, beginYear);
    40. queryWrapper.between(DwSubjectDataInfoWw::getWeek, beginWeek, endWeek);
    41. } else {
    42. //因为Java8 Lambda表达式中最终变量问题,重新赋值一个参数解决
    43. String year1 = beginYear;
    44. String year2 = endYear;
    45. String week1 = beginWeek;
    46. String week2 = endWeek;
    47. queryWrapper.and(wrapper -> wrapper.and(wrapper1 -> wrapper1.eq(DwSubjectDataInfoWw::getYear, year1).ge(DwSubjectDataInfoWw::getWeek, week1))
    48. .or(wrapper2 -> wrapper2.eq(DwSubjectDataInfoWw::getYear, year2).le(DwSubjectDataInfoWw::getWeek, week2)));
    49. }
    50. queryWrapper.orderByDesc(DwSubjectDataInfoWw::getYear);
    51. queryWrapper.orderByDesc(DwSubjectDataInfoWw::getWeek);
    52. if (StrUtil.isNotEmpty(cityName)) {
    53. queryWrapper.eq(DwSubjectDataInfoWw::getCityName, cityName);
    54. }
    55. if (StrUtil.isNotEmpty(userNetType)) {
    56. queryWrapper.eq(DwSubjectDataInfoWw::getUserNetType, userNetType);
    57. }
    58. if (StrUtil.isNotEmpty(moduleName)) {
    59. queryWrapper.eq(DwSubjectDataInfoWw::getModuleName, moduleName);
    60. }
    61. //搜索条件可以是专题中文名或英文名
    62. if (StrUtil.isNotEmpty(keyWord)) {
    63. queryWrapper.and(wrapper -> wrapper.like(DwSubjectDataInfoWw::getSubjectCname, keyWord).or().like(DwSubjectDataInfoWw::getSubjectEname, keyWord));
    64. }
    65. try {
    66. Page<DwSubjectDataInfoWw> list = dwSubjectDataInfoWwService.page(page, queryWrapper);
    67. return AjaxResult.success(list);
    68. } catch (Exception e) {
    69. logger.error("获取分周专题数据列表错误,错误信息为:", e);
    70. return AjaxResult.error();
    71. }

    下面是生成的语句

    1. WHERE
    2. (
    3. (
    4. (year = ? AND week >= ?)
    5. OR (year = ? AND week <= ?)
    6. )
    7. AND city_name = ?
    8. AND user_net_type = ?
    9. AND module_name = ?
    10. AND (
    11. subject_cname LIKE ?
    12. OR subject_ename LIKE ?
    13. )
    14. )
    15. ORDER BY
    16. year DESC,
    17. week DESC