Certainly! Below is a utility class for handling filtering, sorting, and pagination in a Spring Boot project using raw SQL queries. This class will help you dynamically build SQL queries with WHERE, ORDER BY, and LIMIT/OFFSET clauses.
Key points:
- It does not rely on JPA
Specification.
- It builds SQL fragments for filtering, sorting, and pagination.
- You can integrate it with
JdbcTemplate or any other raw SQL execution method.
- It supports simple filtering with
AND conditions.
- Sorting supports multiple fields.
- Pagination uses
LIMIT and OFFSET.
Example Util Class: SqlQueryUtil
package com.example.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
public class SqlQueryUtil {
/**
* Builds the WHERE clause based on the filter map.
*
* @param filters Map of column -> value for filtering (equals condition)
* @param params List to collect parameters for prepared statement
* @return WHERE clause string (empty if no filters)
*/
public static String buildWhereClause(Map<String, Object> filters, List<Object> params) {
if (filters == null || filters.isEmpty()) {
return "";
}
StringJoiner whereClause = new StringJoiner(" AND ", " WHERE ", "");
for (Map.Entry<String, Object> entry : filters.entrySet()) {
whereClause.add(entry.getKey() + " = ?");
params.add(entry.getValue());
}
return whereClause.toString();
}
/**
* Builds the ORDER BY clause based on the sort map.
*
* @param sort Map of column -> direction ("ASC" or "DESC")
* @return ORDER BY clause string (empty if no sorting)
*/
public static String buildOrderByClause(Map<String, String> sort) {
if (sort == null || sort.isEmpty()) {
return "";
}
StringJoiner orderByClause = new StringJoiner(", ", " ORDER BY ", "");
for (Map.Entry<String, String> entry : sort.entrySet()) {
String direction = entry.getValue().equalsIgnoreCase("DESC") ? "DESC" : "ASC";
orderByClause.add(entry.getKey() + " " + direction);
}
return orderByClause.toString();
}
/**
* Builds the LIMIT OFFSET clause for pagination.
*
* @param page zero-based page index
* @param pageSize number of records per page
* @param params List to collect parameters for prepared statement
* @return LIMIT OFFSET clause string
*/
public static String buildPaginationClause(int page, int pageSize, List<Object> params) {
int offset = page * pageSize;
params.add(pageSize);
params.add(offset);
return " LIMIT ? OFFSET ? ";
}
/**
* Builds the full SQL query with filtering, sorting, and pagination.
*
* @param baseQuery base SQL query without WHERE, ORDER BY, LIMIT
* @param filters filter map
* @param sort sort map
* @param page zero-based page index
* @param pageSize page size
* @param params list to collect parameters for prepared statement
* @return full SQL query string
*/
public static String buildQuery(String baseQuery,
Map<String, Object> filters,
Map<String, String> sort,
int page,
int pageSize,
List<Object> params) {
StringBuilder query = new StringBuilder(baseQuery);
// Add WHERE clause
query.append(buildWhereClause(filters, params));
// Add ORDER BY clause
query.append(buildOrderByClause(sort));
// Add pagination clause
query.append(buildPaginationClause(page, pageSize, params));
return query.toString();
}
}
How to use it in your repository/service:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.*;
@Repository
public class UserRepository {
private final JdbcTemplate jdbcTemplate;
public UserRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Map<String, Object>> findUsers(Map<String, Object> filters,
Map<String, String> sort,
int page,
int pageSize) {
String baseQuery = "SELECT * FROM users";
List<Object> params = new ArrayList<>();
String sql = SqlQueryUtil.buildQuery(baseQuery, filters, sort, page, pageSize, params);
return jdbcTemplate.queryForList(sql, params.toArray());
}
}
Notes:
- This example assumes simple equality filters (
column = ?). You can extend it to support other operators (LIKE, >, <, etc.) by changing the filter map structure or adding a filter object.
- Always validate and sanitize column names and directions if they come from user input to avoid SQL injection.
- This utility is designed for simple use cases. For complex queries, consider using a query builder library or JPA Criteria API.
If you want, I can help you extend this to support more complex filtering or dynamic operators!