开发时遇到一个问题,我的需求需要根据order by 后之后的结果来做条件筛选 然后符合才会返回;但是mybatis在通过分页代码查询自动把count方法去掉order by 内容导致结果不对
原代码:
public PageResult pageInfo(TQtInterfaceDto param) {
PageInfo<TQtInterfaceVo> pageInfo = PageHelper.startPage(param.getPageNum(), param.getPageSize()).doSelectPageInfo(() -> tQtInterfaceMapper.selectList(param));
return new PageResult(pageInfo.getList(), pageInfo.getPageNum(), (int) pageInfo.getTotal(), pageInfo.getPageSize());
}
SELECT * FROM (SELECT * FROM
(SELECT T1.id as assemblyId , T1.room_id as roomId, T1.file_version as fileVersion, T1.`status`, T1.dept_id as deptId, T1.style,
T2.id, T1.cn_name AS name, T2.type, T2.size, T3.user_name AS createBy, T1.create_time as createTime, T1.del_flag as delFlag,
CONCAT_WS('/', GROUP_CONCAT(T5.cn_name ORDER BY FIND_IN_SET(T5.id, T4.ancestors) SEPARATOR '/'), T4.cn_name) AS location
FROM t_qt_assembly T1
LEFT JOIN t_qt_interface T2 ON T2.id = T1.qt_interface_id AND T2.del_flag = 'false'
LEFT JOIN sys_user T3 ON T3.id = T1.create_by
LEFT JOIN t_iot_locations T4 ON T4.id = T1.room_id
LEFT JOIN t_iot_locations T5 ON FIND_IN_SET( T5.id, T4.ancestors )
WHERE T1.del_flag = 'false' AND T1.is_show = '1' GROUP BY T1.id
ORDER BY T1.status DESC ,T1.create_time DESC) NoGroupTable
GROUP BY id) RESULT
<where>
<if test="searchTerm != null and searchTerm != ''">
AND (name like concat('%', #{searchTerm}, '%') OR location like concat('%', #{searchTerm}, '%') OR createBy like concat('%', #{searchTerm}, '%') )
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="type != null">
AND type = #{type}
</if>
<if test="size != null and size != ''">
AND size = #{size}
</if>
</where>
ORDER BY ${orderByClause} ${sort}
解决方案:在sql上加一行
/keep orderby/
/*keep orderby*/
SELECT * FROM (SELECT * FROM
(SELECT T1.id as assemblyId , T1.room_id as roomId, T1.file_version as fileVersion, T1.`status`, T1.dept_id as deptId, T1.style,
T2.id, T1.cn_name AS name, T2.type, T2.size, T3.user_name AS createBy, T1.create_time as createTime, T1.del_flag as delFlag,
CONCAT_WS('/', GROUP_CONCAT(T5.cn_name ORDER BY FIND_IN_SET(T5.id, T4.ancestors) SEPARATOR '/'), T4.cn_name) AS location
FROM t_qt_assembly T1
LEFT JOIN t_qt_interface T2 ON T2.id = T1.qt_interface_id AND T2.del_flag = 'false'
LEFT JOIN sys_user T3 ON T3.id = T1.create_by
LEFT JOIN t_iot_locations T4 ON T4.id = T1.room_id
LEFT JOIN t_iot_locations T5 ON FIND_IN_SET( T5.id, T4.ancestors )
WHERE T1.del_flag = 'false' AND T1.is_show = '1' GROUP BY T1.id
ORDER BY T1.status DESC ,T1.create_time DESC) NoGroupTable
GROUP BY id) RESULT
<where>
<if test="searchTerm != null and searchTerm != ''">
AND (name like concat('%', #{searchTerm}, '%') OR location like concat('%', #{searchTerm}, '%') OR createBy like concat('%', #{searchTerm}, '%') )
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="type != null">
AND type = #{type}
</if>
<if test="size != null and size != ''">
AND size = #{size}
</if>
</where>
ORDER BY ${orderByClause} ${sort}
成功解决问题