Spring Data

Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.

It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.

Quick Start

特性

  • Powerful repository and custom object-mapping abstractions
  • Dynamic query derivation from repository method names
  • Implementation domain base classes providing basic properties
  • Support for transparent auditing (created, last changed)
  • Possibility to integrate custom repository code
  • Easy Spring integration via JavaConfig and custom XML namespaces
  • Advanced integration with Spring MVC controllers
  • Experimental support for cross-store persistence

主要模块

社区模块

相关模块

  • Spring Data JDBC Extensions - Provides extensions to the JDBC support provided in the Spring Framework.
  • Spring for Apache Hadoop - Simplifies Apache Hadoop by providing a unified configuration model and easy to use APIs for using HDFS, MapReduce, Pig, and Hive.
  • Spring Content - Associate content with your Spring Data Entities and store it in a number of different stores including the File-system, S3, Database or Mongo’s GridFS.

发布版本

Spring Data is an umbrella project consisting of independent projects with, in principle, different release cadences. To manage the portfolio, a BOM (Bill of Materials - see this example) is published with a curated set of dependencies on the individual project. The release trains have names, not versions, to avoid confusion with the sub-projects.

The names are an alphabetic sequence (so you can sort them chronologically) with names of famous computer scientists and software developers. When point releases of the individual projects accumulate to a critical mass, or if there is a critical bug in one of them that needs to be available to everyone, the release train will push out “service releases” with names ending “-SRX”, where “X” is a number.

Currently the release train contains the following modules:

  • Spring Data Commons
  • Spring Data JPA
  • Spring Data KeyValue
  • Spring Data LDAP
  • Spring Data MongoDB
  • Spring Data REST
  • Spring Data Redis
  • Spring Data for Apache Cassandra
  • Spring Data for Apache Geode
  • Spring Data for Apache Solr
  • Spring Data for Pivotal GemFire
  • Spring Data Couchbase (community module)
  • Spring Data Elasticsearch (community module)
  • Spring Data Neo4j (community module)

快速上手

Download

The recommended way to get started using spring-data in your project is with a dependency management system – the snippet below can be copied and pasted into your build. Need help? See our getting started guides on building with Maven and Gradle.

For a quick taste, look at the following domain object:

@Entity
public class Employee {

  private @Id @GeneratedValue Long id;
  private String firstName, lastName, description;

  private Employee() {}

  public Employee(String firstName, String lastName, String description) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.description = description;
  }
}

This defines a simple JPA entity with a few fields. The following code shows a simple repository definition:

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

  Employee findByFirstName(String firstName);

  List<Employee> findByLastName(String lastName);
}

This interface extends Spring Data’s CrudRepository and defines the type (Employee) and the id type (Long). Put this code inside a Spring Boot application with spring-boot-starter-data-jpa like this:

@SpringBootApplication
public class MyApp {

  public static void main(String[] args) {
    SpringApplication.run(MyApp.class, args);
  }
}

Launch your app and Spring Data (having been autoconfigured by Boot, SQL or NoSQL) will automatically craft a concrete set of operations:

  • save(Employee)
  • delete(Employee)
  • find(Employee)
  • find(Long)
  • findAll()

On top of the CRUD operations inherited from CrudRepository, the interface defines two query methods.

  • findByFirstName(…) automatically writes a JPA query based on firstName and only return the first employee found.
  • findByLastName(…) automatically writes a JPA query based on lastName and returns a collection.

Spring Boot starters

If you are using Spring Boot, you will inherit predefined versions for each project. To plugin a newer or older release train, configure the spring-data-releasetrain.version property to the release train iteration name you want to use.

Spring Boot provides so called started POMs for a variety of SPring Data modules which pull in a curated set of dependencies you’ll need to use the individual modules. For details on that see the Spring Boot reference documentation.