当前使用版本(必填,否则不予处理)

mybatis-plus-boot-starter:3.3.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

前提:

开启 count 的 join 优化

@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

查询语句中,主表(A)LEFT JOIN多张表(BCD),在WHERE语句中,包含 B 表的 B1 字段。

一:

如果在未传入B1字段,JsqlParserCountOptimize会自动优化成 SELECT COUNT(1) FROM A WHERE A.a1=? 去除了所有JOIN的表,

二:

如果传入了B1字段,语句将不会优化。 具体源代码为:

/* 如果 join 里包含 ?(代表有入参) 或者 where 条件里包含使用 join 的表的字段作条件,就不移除 join */
if (onExpressionS.contains(StringPool.QUESTION_MARK) || whereS.contains(str)) {
    canRemoveJoin = false;
    break;
}

三:

能否根据where条件中包含的入参进行优化,移除不包含入参的join? 考虑某一个join的on语句可能关联到其他的join,这部分的逻辑可能会比较复杂。。

重现步骤(如果有就写完整)

一:

语句1:

select * from tab_a a
left join tab_b b on a.aid = b.bid
left join tab_c c on a.aid = c.cid
left join tab_d d on a.aid = d.cid
where a.aid=?

优化后: select count(1) from tab_a a

二:

语句2:

select * from tab_a a
left join tab_b b on a.aid = b.bid
left join tab_c c on a.aid = c.cid
left join tab_d d on a.aid = d.cid
where a.aid=? and b.bid=?

优化后:

SELECT COUNT(1) FROM (
select * from tab_a a
left join tab_b b on a.aid = b.bid
left join tab_c c on a.aid = c.cid
left join tab_d d on a.aid = d.cid
where a.aid=? and b.bid=?
) TOTAL 

期望优化

select count(1) from tab_a a
left join tab_b b on a.aid = b.bid
where a.aid=? and b.bid=?

三:

语句3:

select * from tab_a a
left join tab_b b on a.aid = b.bid and b.bcid = c.bcid
left join tab_c c on a.aid = c.cid and b.bcid = c.bcid
left join tab_d d on a.aid = d.cid
where a.aid=? and b.bid=?

期望优化

select count(1) from tab_a a
left join tab_b b on a.aid = b.bid and b.bcid = c.bcid
left join tab_c c on a.aid = c.cid and b.bcid = c.bcid
where a.aid=? and b.bid=?

报错信息

Comment From: miemieYaho

目前只能简单优化如 一 , 二和三建议自己的count

Comment From: cwh0704

目前只能简单优化如 一 , 二和三建议自己的count

好的。谢谢

Comment From: 18312597394

请问各位大佬们,mybatis-plus是哪个版本,为什么我的找不到setCountSqlParser()这个方法