Interface QueryBuilder<T>
- Type Parameters:
T- the entity type
public interface QueryBuilder<T>
Query builder for complex database queries.
Provides a fluent interface for building queries with multiple conditions, ordering, and pagination.
Example:
List<UserSetting> results = repository.query()
.where("user_id", 12345L)
.where("enabled", true)
.whereLike("setting_name", "theme%")
.orderBy("created_at", false) // descending
.limit(10)
.offset(0)
.list();
// Or get a single result
Optional<UserSetting> result = repository.query()
.where("user_id", 12345L)
.where("setting_name", "theme")
.findOne();
// Count
long count = repository.query()
.where("enabled", true)
.count();
-
Method Summary
Modifier and TypeMethodDescriptionlongcount()Count matching entities.intdelete()Delete all matching entities.booleanexists()Check if any matching entities exist.findOne()Execute the query and return the first matching entity.Execute the query and return the first matching entity, or throw if not found.limit(int limit) Limit the number of results.list()Execute the query and return all matching entities.offset(int offset) Skip a number of results.Add ordering.default QueryBuilder<T> orderByAsc(String column) Add ascending ordering.default QueryBuilder<T> orderByDesc(String column) Add descending ordering.Add an equality condition (column = value).whereBetween(String column, Object start, Object end) Add a BETWEEN condition (column BETWEEN start AND end).whereGreaterThan(String column, Object value) Add a greater-than condition (column > value).whereGreaterThanOrEqual(String column, Object value) Add a greater-than-or-equal condition (column >= value).whereILike(String column, String pattern) Add a case-insensitive LIKE condition.Add an IN condition (column IN (values)).whereLessThan(String column, Object value) Add a less-than condition (column < value).whereLessThanOrEqual(String column, Object value) Add a less-than-or-equal condition (column <= value).Add a LIKE condition for pattern matching.Add a not-equal condition (column !whereNotIn(String column, List<?> values) Add a NOT IN condition.whereNotNull(String column) Add an IS NOT NULL condition.Add an IS NULL condition.
-
Method Details
-
where
Add an equality condition (column = value).- Parameters:
column- the column namevalue- the value- Returns:
- this builder
-
whereNot
Add a not-equal condition (column != value).- Parameters:
column- the column namevalue- the value- Returns:
- this builder
-
whereGreaterThan
Add a greater-than condition (column > value).- Parameters:
column- the column namevalue- the value- Returns:
- this builder
-
whereGreaterThanOrEqual
Add a greater-than-or-equal condition (column >= value).- Parameters:
column- the column namevalue- the value- Returns:
- this builder
-
whereLessThan
Add a less-than condition (column < value).- Parameters:
column- the column namevalue- the value- Returns:
- this builder
-
whereLessThanOrEqual
Add a less-than-or-equal condition (column <= value).- Parameters:
column- the column namevalue- the value- Returns:
- this builder
-
whereLike
Add a LIKE condition for pattern matching.Use % for wildcard matching: - "abc%" matches strings starting with "abc" - "%abc" matches strings ending with "abc" - "%abc%" matches strings containing "abc"
- Parameters:
column- the column namepattern- the pattern with % wildcards- Returns:
- this builder
-
whereILike
Add a case-insensitive LIKE condition.- Parameters:
column- the column namepattern- the pattern with % wildcards- Returns:
- this builder
-
whereIn
Add an IN condition (column IN (values)).- Parameters:
column- the column namevalues- the values to match- Returns:
- this builder
-
whereNotIn
Add a NOT IN condition.- Parameters:
column- the column namevalues- the values to exclude- Returns:
- this builder
-
whereNull
Add an IS NULL condition.- Parameters:
column- the column name- Returns:
- this builder
-
whereNotNull
Add an IS NOT NULL condition.- Parameters:
column- the column name- Returns:
- this builder
-
whereBetween
Add a BETWEEN condition (column BETWEEN start AND end).- Parameters:
column- the column namestart- the start value (inclusive)end- the end value (inclusive)- Returns:
- this builder
-
orderBy
Add ordering.- Parameters:
column- the column to order byascending- true for ASC, false for DESC- Returns:
- this builder
-
orderByAsc
Add ascending ordering.- Parameters:
column- the column to order by- Returns:
- this builder
-
orderByDesc
Add descending ordering.- Parameters:
column- the column to order by- Returns:
- this builder
-
limit
Limit the number of results.- Parameters:
limit- maximum number of results- Returns:
- this builder
-
offset
Skip a number of results.- Parameters:
offset- number of results to skip- Returns:
- this builder
-
list
-
findOne
-
findOneOrThrow
T findOneOrThrow()Execute the query and return the first matching entity, or throw if not found.- Returns:
- the first matching entity
- Throws:
NoSuchElementException- if no match found
-
count
long count()Count matching entities.- Returns:
- count of matching entities
-
exists
boolean exists()Check if any matching entities exist.- Returns:
- true if at least one match exists
-
delete
int delete()Delete all matching entities.- Returns:
- number of entities deleted
-