当前位置: 首页 > 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.rkmt.cn/news/192367.html

相关文章:

  • 离散数学(1) | 命题逻辑的基本概念
  • 在 Windows 上使用 uv 时的 hardlink 警告:Failed to hardlink files; falling back to full copy 完美解决方案
  • 终极MacBook凹口改造指南:打造个性化动态音乐控制中心
  • Jordium Gantt Vue3 1.4.3 重磅发布:虚拟渲染 + 虚拟滚动,大数据甘特图正式“无卡顿时代” - 指南
  • Web视频解码器性能优化的三重奏:从136KB到20KB的极致压缩实践
  • 快速理解充电线背后的USB接口有几种差异
  • 但是我有雨窗
  • Vue Storefront跨境电商实战:从零搭建全球电商平台的完整指南
  • Windows 11直角窗口终极指南:5分钟告别圆角设计
  • YOLOv8 config file not specified配置缺失处理
  • 三极管工作原理及详解:小白指南之放大与开关模式
  • UF2文件格式终极指南:从零基础到快速上手
  • Google Webfonts Helper:自托管谷歌字体的终极解决方案
  • Boop 2.0:如何轻松实现Switch和3DS游戏文件无线传输
  • vue-grid-layout完全指南:从零构建可拖拽的响应式布局
  • Azure OpenAI服务与MCP集成全流程解析(从规划到上线仅需4步)
  • Make-A-Video 项目终极指南:从文本到视频的AI魔法
  • vLLM+SGLang双引擎加速!ms-swift推理性能实测报告发布
  • 行业报告:测试自动化采纳率
  • 芒种播种希望:新用户引导体系全面改版
  • 相空间重构的Matlab实现:延迟时间t与嵌入维数m的确定及互信息应用
  • YOLOv8联邦学习架构设想:保护数据隐私
  • 3步轻松获取谢希仁计算机网络教材:网络工程师的终极学习指南
  • 移动AI向量搜索终极指南:sqlite-vec在iOS/Android的完整部署方案
  • 5分钟全面掌握PingFang SC Regular字体的完整使用指南
  • 【MCP AI Copilot集成核心考点】:掌握这5大关键技术,轻松通过企业级认证
  • 自定义数据集导入指南:ms-swift灵活适配企业私有数据
  • 【2025 MCP Azure OpenAI 集成指南】:掌握企业级AI落地的5大核心步骤
  • Git钩子现代化管理:如何在大型项目中实现高效代码质量控制
  • AWQ导出流程:生成兼容多种推理引擎的模型