尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Spring-boot读书笔记一Spring data commons

Spring-boot读书笔记一Spring data commons
📅 发布时间:2026/6/19 11:12:49

1. Repository<T, ID> - Base Marker Interface
Special Effect: Pure marker - no methods


public interface UserRepository extends Repository<User, Long> {// Only custom methods allowed - no built-in CRUDList<User> findByUsername(String username);
}

Use Case: When you want only custom query methods without any built-in CRUD operations.

2. CrudRepository<T, ID> - Basic CRUD Operations
Special Effect: Complete CRUD functionality


public interface ProductRepository extends CrudRepository<Product, Long> {// Automatically gets these methods:// save(), findById(), findAll(), delete(), count(), exists()
}// Usage
productRepository.save(product);           // INSERT/UPDATE
productRepository.findById(1L);            // SELECT by ID
productRepository.findAll();               // SELECT all
productRepository.deleteById(1L);          // DELETE
productRepository.count();                 // COUNT records

Use Case: Basic database operations without pagination needs.

3. PagingAndSortingRepository<T, ID> - Pagination & Sorting
Special Effect: Adds pagination and sorting capabilities

public interface OrderRepository extends PagingAndSortingRepository<Order, Long> {// Gets CRUD methods + pagination/sorting
}// Usage
Pageable pageable = PageRequest.of(0, 10, Sort.by("createdDate").descending());
Page<Order> orders = orderRepository.findAll(pageable);// Sorting only
Iterable<Order> sortedOrders = orderRepository.findAll(Sort.by("amount").ascending());

Use Case: When you need large dataset handling with pagination and sorting.

4. JpaRepository<T, ID> - JPA-Specific Enhancements
Special Effect: JPA optimizations and batch operations


public interface UserRepository extends JpaRepository<User, Long> {// Gets all previous methods + JPA-specific optimizations
}// Special JPA methods
userRepository.flush();                    // Force synchronization with DB
userRepository.saveAndFlush(user);         // Save + immediate flush
userRepository.deleteInBatch(users);       // Batch delete (single SQL)
userRepository.deleteAllInBatch();         // Delete all in one SQL statement

Use Case: JPA/Hibernate applications needing performance optimizations.

5. ReactiveCrudRepository<T, ID> - Reactive Programming
Special Effect: Non-blocking, reactive operations

public interface UserRepository extends ReactiveCrudRepository<User, Long> {// Returns Mono/Flux instead of regular objects
}// Usage
Mono<User> user = userRepository.findById(1L);        // Single result
Flux<User> users = userRepository.findAll();          // Multiple results
Mono<User> saved = userRepository.save(user);         // Non-blocking save

Use Case: High-concurrency applications with reactive streams.

6. RxJava2CrudRepository<T, ID> - RxJava Integration
Special Effect: RxJava reactive types

public interface ProductRepository extends RxJava2CrudRepository<Product, Long> {// Returns Observable/Single/Maybe
}// Usage
Single<Product> product = productRepository.findById(1L);
Observable<Product> products = productRepository.findAll();

Use Case: Applications using RxJava reactive programming.

Comparison Summary:
Interface Methods Return Types Special Feature
Repository 0 Custom Pure marker
CrudRepository ~12 Objects/Iterables Basic CRUD
PagingAndSortingRepository +2 Page/Slice Pagination
JpaRepository +8 Lists JPA optimizations
ReactiveCrudRepository ~12 Mono/Flux Non-blocking
RxJava2CrudRepository ~12 Single/Observable RxJava types

Choosing the Right Interface:

  • Repository → Custom methods only
  • CrudRepository → Basic CRUD needs
  • PagingAndSortingRepository → Large datasets
  • JpaRepository → JPA/Hibernate projects (most common)
  • ReactiveCrudRepository → WebFlux/reactive applications
  • RxJava2CrudRepository → RxJava-based projects

Each interface builds upon the previous one, adding specific capabilities for different use cases.

相关新闻

  • 离散数学(1) | 命题逻辑的基本概念
  • 在 Windows 上使用 uv 时的 hardlink 警告:Failed to hardlink files; falling back to full copy 完美解决方案
  • 终极MacBook凹口改造指南:打造个性化动态音乐控制中心

最新新闻

  • 2026年众智商学院CPPM适合采购岗位哪些人报考?学习内容和在职成长路径 - 众智商学院职业教育
  • 2026深圳黄金回收正规渠道测评!新手变现必备攻略 - 奢侈品回收测评
  • PPT2Image:企业级演示文档自动化转换的技术实现与架构解析
  • 2026岳阳放心贵金属回收,CCIC 中检授权收黄金回收铂金回收白银回收持证实体门店 - 中安检金银铂钻回收
  • MPC555/556存储器映射解析:从地址到硬件控制的嵌入式开发指南
  • MCF5206总线操作:从原理到实战的深度解析

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号