Introduction to Spring Data JPA
Spring Data JPA is part of the larger Spring Data family. It is based on JPA and enhances the Data access function. Using Spring Data JPA, you can easily access the database without writing a lot of code templates.
The Spring Data family
The primary function of Spring Data is to provide a simple, normative, Spring-based programming model for Data access. It enables the use of data access technologies, supporting both relational databases such as mysql and non-relational databases such as Redis. Here are the family members of Spring Data:
- Spring Data Commons – Core Spring concepts underpinning every Spring Data module.
- Spring Data JDBC – Spring Data repository support for JDBC.
- Spring Data JDBC Ext – Support for database specific extensions to standard JDBC including support for Oracle RAC fast connection failover, AQ JMS support and support for using advanced data types.
- Spring Data JPA – Spring Data repository support for JPA.
- Spring Data KeyValue – Map based repositories and SPIs to easily build a Spring Data module for key-value stores.
- Spring Data LDAP – Spring Data repository support for Spring LDAP.
- Spring Data MongoDB – Spring based, object-document support and repositories for MongoDB.
- Spring Data Redis – Easy configuration and access to Redis from Spring applications.
- Spring Data REST – Exports Spring Data repositories as hypermedia-driven RESTful resources.
- Spring Data for Apache Cassandra – Easy configuration and access to Apache Cassandra or large scale, highly available, data oriented Spring applications.
- Spring Data for Apache Geode – Easy configuration and access to Apache Geode for highly consistent, low latency, data oriented Spring applications.
- Spring Data for Apache Solr – Easy configuration and access to Apache Solr for your search oriented Spring applications.
- Spring Data for Pivotal GemFire – Easy configuration and access to Pivotal GemFire for your highly consistent, Low latency/high through-put, data Oriented Spring applications. JPA is a specification, not a specific implementation framework, Spring Data JPA relies on the Hibernate JPA implementation
Let’s take a quick look at Spring Data JPA with an example
The Spring Data Jpa example
1. Create a new project
2. Create a database
create table user
(
user_id bigint not null comment 'user id',
name varchar(64) null comment 'name',
age int null comment 'age',
sex smallint null comment 'gender',
mobile varchar(64) null comment 'Mobile phone Number',
mail varchar(64) null comment 'email'
)
comment 'User table';
Copy the code
3. Start coding
Create a new bean class for the user table as follows:
Here’s another Controller:
4. Finally test the interface
Postman tests it out