场景: 需要返回List<map>集合,且其中存在需要collection 进行部分集合数据进行汇总,因为pageHelper默认是在sql语句最后加入limit语句,就会导致最终的查询结果少于分页数量
解决:
第一步:重写helperDialect类继承MySqlDialect
import com.github.pagehelper.Page;
import com.github.pagehelper.dialect.helper.MySqlDialect;
import org.apache.ibatis.cache.CacheKey;
import java.util.regex.Pattern;
/**
* @author LengChen
* @version 1.0
* @date 2022-03-29
*/
public class MySqlDialectCustom extends MySqlDialect {
@Override
public String getPageSql(String sql, Page page, CacheKey pageKey) {
Pattern compileFixed = Pattern.compile("/\\*fixed\\*/");
String compileLimit = "/\\*limit\\*/";
if (compileFixed.matcher(sql).find()) {
if (page.getStartRow() == 0) {
sql = sql.replaceFirst(compileLimit, " LIMIT ? ");
} else {
sql = sql.replaceFirst(compileLimit, " LIMIT ?, ? ");
}
return sql;
}
return super.getPageSql(sql, page, pageKey);
}
}
第二步:配置pagehelper.helperDialect
pagehelper:
helperDialect: com.ruoyi.framework.datasource.MySqlDialectCustom
第三步:sql编写
/*fixed*/为标记此sql需要自定义limit
/*limit*/为真正需要填写limit语句的位置
<select id="selectRecruitRangeListMap" resultMap="rangeListMap">
/*fixed*/
SELECT
r.range_id,r.range_name,r.range_num,r.batch_num,r.setting_id,d1.dept_id,d1.dept_name
FROM
(
SELECT
dept_id,
dept_name
FROM
sys_dept d
WHERE
FIND_IN_SET(
d.dept_id,
'100,200,201,202,203'
)
/*limit*/
) d1
LEFT JOIN recruit_range r ON d1.dept_id = r.dept_id AND r.setting_id = '2022' AND r.del_flag = '0'
</select>
|