当前位置: 首页 > news >正文

Spring-boot读书笔记一Spring data commons

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.

http://www.jsqmd.com/news/176340/

相关文章:

  • 终极指南:开源AI编程助手OpenCode的完整评测与实战应用
  • Syft实战宝典:告别软件供应链安全盲区的终极指南
  • GlusterFS共享卷使用说明:跨节点数据同步技巧
  • Obsidian插件开发:创建DDColor实验日志自动记录工具
  • Realm Java全文搜索实战:从基础配置到高级查询的完整指南
  • HTML转Sketch:打破设计与开发边界的协作利器
  • BI报表及可视化分析类工具使用经验总结(下)
  • Web视频解码器性能优化的三重奏:从136KB到20KB的极致压缩实践
  • Jordium Gantt Vue3 1.4.3 重磅发布:虚拟渲染 + 虚拟滚动,大数据甘特图正式“无卡顿时代” - 指南
  • 错过将落后一年!MCP Azure Stack HCI混合部署技术红利期仅剩最后90天
  • YOLOv8 config file not specified配置缺失处理
  • 三极管工作原理及详解:小白指南之放大与开关模式
  • Windows 11直角窗口终极指南:5分钟告别圆角设计
  • 从零实现无源蜂鸣器驱动电路:新手也能轻松上手
  • Make-A-Video 项目终极指南:从文本到视频的AI魔法
  • 【企业混合云转型必读】:掌握MCP+Azure Stack HCI部署的7大核心技术要点
  • Azure OpenAI服务与MCP集成全流程解析(从规划到上线仅需4步)
  • Zonos语音合成完全指南:5步打造媲美真人的AI语音
  • vLLM+SGLang双引擎加速!ms-swift推理性能实测报告发布
  • vendor-reset 驱动程序:5步搞定Linux设备重置的终极指南
  • DETR模型加速5步实战:从0.036秒到0.008秒的性能飙升之旅
  • 芒种播种希望:新用户引导体系全面改版
  • MapsModelsImporter:零基础掌握的Blender地图建模神器
  • 行业报告:测试自动化采纳率
  • HTTP Shortcuts:5分钟打造专属Android自动化神器
  • FP8量化导出实战:在ms-swift中压缩模型体积,节省70%显存
  • LUT调色包下载网站OUT了!现在设计师都在用AI生成色彩方案
  • 相空间重构的Matlab实现:延迟时间t与嵌入维数m的确定及互信息应用
  • 5个步骤快速上手SuperSonic插件开发:打造专属ChatBI功能
  • Apache OpenDAL™ 异步与阻塞操作终极指南:高性能数据访问层的完全解析