Skip to content

Consider adding a smart count generator for QueryExpressionDSL? #990

@chastro3

Description

@chastro3

Description:

Currently, when implementing pagination with MyBatis Dynamic SQL, returning a PageData with total count is not very elegant.
Developers often need to write a utility method to construct both the actual select query and a corresponding count query.

It would be very helpful to provide a method to generate a smart count DSL from a given QueryExpressionDSL, which would make PageData queries cleaner and more concise.

The desired behavior is similar to PageHelper’s DefaultCountSqlParser#getSmartCountSql
,
which automatically rewrites simple queries into count(*) and wraps complex queries as subqueries for counting.

Benefits:

No external parser dependencies (JSQLParser not required; unlike PageHelper).
Safe and type-checked.
Consistent syntax with existing DSL.
Performance-optimized count queries automatically.

Example usage:

QueryExpressionDSL<SelectModel> dsl = select(UserTable.id, UserTable.name)
    .from(UserTable)
    .where(UserTable.status, isEqualTo(1));

SelectStatementProvider countProvider = SqlBuilder.smartCountFrom(dsl);

long total = userMapper.count(countProvider);

This feature would allow pagination tools to automatically compute totals without duplicating query logic, improving code clarity and safety.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions