使用规范构建动态查询
以编程方式构建灵活的查询
使用规范构建动态查询 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Problem with Static Queries
When users can filter by many optional fields, writing one query method per combination explodes quickly.
Spring Data Specifications let you build queries dynamically at runtime based on which filters are present.
JpaSpecificationExecutor
Extend JpaSpecificationExecutor on your repository to enable Specification-based queries.
public interface UserRepository
extends JpaRepository<User, Long>,
JpaSpecificationExecutor<User> {
}A Specification
A Specification<T> describes a single WHERE predicate using the JPA Criteria API.
Specification<User> hasName(String name) {
return (root, query, cb) ->
cb.equal(root.get("name"), name);
}Executing a Specification
Pass the spec to findAll.
List<User> users =
userRepository.findAll(hasName("Alice"));Combining with and
Specifications compose with and, or, and not.
Specification<User> spec =
hasName("Alice").and(isActive(true));
List<User> users = userRepository.findAll(spec);Building Conditionally
The real power: add predicates only when the filter is supplied.
Specification<User> spec = Specification.where(null);
if (name != null) {
spec = spec.and(hasName(name));
}
if (active != null) {
spec = spec.and(isActive(active));
}
return userRepository.findAll(spec);Like and Comparison Predicates
The Criteria builder offers many predicate types.
Specification<User> nameLike(String part) {
return (root, query, cb) ->
cb.like(root.get("name"), "%" + part + "%");
}
Specification<User> olderThan(int age) {
return (root, query, cb) ->
cb.greaterThan(root.get("age"), age);
}Joining Related Entities
Specifications can navigate joins to filter on associated entities.
Specification<User> inCity(String city) {
return (root, query, cb) ->
cb.equal(root.join("address").get("city"), city);
}Specifications with Paging
Combine a Specification with a Pageable for filtered, paginated results.
Page<User> page = userRepository.findAll(
spec, PageRequest.of(0, 20));Counting Matches
count accepts a Specification too.
long matches = userRepository.count(hasName("Alice"));Reusing Predicate Factories
Group spec factories in a helper class so they can be reused and unit tested.
public class UserSpecs {
public static Specification<User> active() {
return (root, q, cb) ->
cb.isTrue(root.get("active"));
}
}Quick Check
Test your understanding of Specifications.
Recap
You learned dynamic querying:
- Extend
JpaSpecificationExecutor - A
Specificationis one Criteria predicate - Compose with
and/or, add conditionally - Combine with
Pageablefor filtered pages
用 AI 导师学习 Java — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 24
- 课程
- 93
常见问题解答
「使用规范构建动态查询」课时是免费的吗?
是的 — 「使用规范构建动态查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
「使用规范构建动态查询」这节课中我会学到什么?
以编程方式构建灵活的查询 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「使用规范构建动态查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?
能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用规范构建动态查询
- 投影和 DTO
- 使用 @CreatedDate 进行审计
- 分页和排序