一、包含子查询的分页查询异常
- 异常表现:
总数量查询是错误的子查询sql;
提示参数位置异常;
- 异常原因:
没写countQuery;
countQuery的sql编写不完整,参数引用与value的sql不一致;
- 解决方案:
在@Query中编写countQuery=”
正确示例:
/*** 直播分页查询* @param searchTxt 搜索内容* @param startDate 开始时间* @param endDate 结束时间* @param pageable 分页参数* @author chaiin* @date 2020/06/18 15:11:00*/@Query(value = "select ls.id, " +" res.url AS picOrPreviewVideoUrl, " +" ls.live_url AS liveUrl, " +" ls.title, " +" concat(ls.start_time,'') AS liveTime, " +" concat(bc.campus_name, '', bc.building_name, '', bc.classroom_name) AS classroomName, " +" au.user_name AS anchorName, " +" ifnull(rr.ftp_url, rr.url) replayUrl, " +" case ls.status " +" when 1 then '未开始' " +" when 2 then '直播中' " +" when 3 then '已结束' " +" when 4 then '已取消' " +" end AS status, " +" case ls.status " +" when 1 then 2 " +" when 2 then 1 " +" else ls.status " +" end AS statusSort, " +" if(ls.power = 2, 1, 0) AS power " +" from " +" ( " +" select * from live_seeding " +" where is_deleted = 0 " +" and status in (1, 2, 3) " +" and state = 2 " +" and IF(?2!=null, start_time between ?2 and ?3, '1=1' ) " +" ) ls " +" left join resources res on ls.resource_id = res.id " +" left join authority_user au on ls.user_id = au.id " +" left join base_classroom bc on ls.room_id = bc.id " +" left join recording_replay rr on ls.id = rr.live_id " +" where IF(" +" ?1!='', " +" (locate(?1, ls.title) > 0 " +" or locate(?1, ls.course_remark) > 0 " +" or locate(?1, au.user_name) > 0 " +" or locate(?1, concat(bc.campus_name, '', bc.building_name, '', bc.classroom_name)) > 0), " +" '1=1'" +" ) " +" order by statusSort asc, liveTime desc",countQuery = "select count(*)" +" from " +" ( " +" select * from live_seeding " +" where is_deleted = 0 " +" and status in (1, 2, 3) " +" and state = 2 " +" and IF(?2!=null, start_time between ?2 and ?3, '1=1' ) " +" ) ls " +" left join resources res on ls.resource_id = res.id " +" left join authority_user au on ls.user_id = au.id " +" left join base_classroom bc on ls.room_id = bc.id " +" left join recording_replay rr on ls.id = rr.live_id " +" where IF(" +" ?1!='', " +" (locate(?1, ls.title) > 0 " +" or locate(?1, ls.course_remark) > 0 " +" or locate(?1, au.user_name) > 0 " +" or locate(?1, concat(bc.campus_name, '', bc.building_name, '', bc.classroom_name)) > 0), " +" '1=1'" +" ) ",nativeQuery = true)// 时间条件错误/* Page<Map<String,Object>> pageLivePaly(String searchTxt, Date startDate, Date endDate, Pageable pageable); */// 时间条件正确Page<Map<String,Object>> pageLivePaly(String searchTxt, String startDate, String endDate, Pageable pageable);
二、查询操作时间条件无效
时间条件应使用String类型
