Hibernate / MyBatis Interview questions
Explain how pagination is typically implemented in MyBatis?
MyBatis offers a built-in but limited mechanism, RowBounds, alongside more commonly used approaches that push pagination logic directly into the SQL itself or rely on a plugin to automate it, and understanding the trade-offs between them matters for building efficient, large-scale queries.
| Approach | How it Works | Efficiency |
| RowBounds | MyBatis fetches all rows, then skips/limits in memory | Inefficient for large offsets; not a true database-level LIMIT |
| Manual SQL LIMIT/OFFSET | Pagination written directly into the mapper's SQL | Efficient; pushes limiting to the database |
| Pagination plugin (e.g. PageHelper) | An interceptor automatically rewrites queries to add LIMIT/OFFSET | Efficient, with less manual SQL boilerplate per query |
RowBounds is generally discouraged for anything beyond small datasets specifically because MyBatis still retrieves the full result set from the database before applying the offset and limit in application memory — it's not translated into a database-level LIMIT/OFFSET clause, so it doesn't save any actual database work or network transfer for the skipped rows.
Writing LIMIT/OFFSET (or the equivalent syntax for a given database) directly into the mapper's SQL is the most straightforward efficient approach, while third-party pagination plugins like PageHelper use MyBatis's own plugin/interceptor mechanism to automatically rewrite a query and add the appropriate limiting clause, reducing the need to hand-write pagination logic into every paginated query individually.
More Related questions...