Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
Spring Data aims to provide a familiar and consistent Spring-based programming model for data access while retaining special traits of the underlying data store.
Repositories: These are the central part of Spring Data. A repository is a high-level persistence-oriented abstraction that allows users to perform CRUD operations without the need for boilerplate code. You typically define an interface extending one of the Spring Data repository interfaces, and Spring provides the implementation at runtime.
Query Methods: Simply by defining method signatures in your repository interface, you can generate queries without having to write their implementations. For example, findByLastName(String lastName)
will generate a query to fetch records based on the lastName
field.
Consistent CRUD Operations: Provides a consistent way to perform CRUD operations across various data stores.
Query Derivation: Automatically derives queries from method names in your repository interface.
Custom Queries: Allows you to define custom queries using the query language of the underlying datastore or even native queries.
Pagination and Sorting: Simplifies pagination and sorting operations through the PagingAndSortingRepository
interface.
Domain Base Classes: Offers base classes for entities to derive from which include basic properties like id
, createdDate
, etc.
Spring Data isn��t just about relational databases. It has several modules for different data sources:
Apart from supporting various data stores, Spring Data also integrates smoothly with other parts of the Spring ecosystem, like Spring Security for incorporating security measures in your data access.
Spring Data addresses the complexity of data access layers in modern applications by providing a unified programmin
Overview of Spring Data JPA:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; // Getters and setters }
Using Spring Data MongoDB for NoSQL Data Access:
@Document public class Book { @Id private String id; private String title; // Getters and setters }
Implementing CRUD Operations with Spring Data:
public interface UserRepository extends JpaRepository<User, Long> { List<User> findByUsername(String username); }
Query Methods in Spring Data Repositories:
public interface UserRepository extends JpaRepository<User, Long> { List<User> findByUsernameAndEmail(String username, String email); }
Auditing with Spring Data:
@EntityListeners(AuditingEntityListener.class) public class AuditedEntity { @CreatedBy private String createdBy; @CreatedDate private LocalDateTime createdDate; @LastModifiedBy private String lastModifiedBy; @LastModifiedDate private LocalDateTime lastModifiedDate; // Getters and setters }
Custom Query Methods and Native Queries in Spring Data:
public interface UserRepository extends JpaRepository<User, Long> { @Query("SELECT u FROM User u WHERE u.username = :username") List<User> findUsersByUsername(@Param("username") String username); }