data access for spring boot
Spring data is a package of solutions that spring uses to solve data access problems, and spring data is an umbrella project that contains a large number of access solutions for relational and non-relational databases. Included sub-items:
Spring Data JPA;
Spring Data MongoDB
Spring Data REST
Spring Data Elasticsearch, etc.
Spring data supports the above storage technologies using a unified API, which is implemented by spring through the spring Data Common project, which is dependent on each of these projects. A key concept of spring data common:Spring data repository abstraction . The root interface of abstraction is the repository interface. Its subinterface crudrepository defines the related operations of the crud (such as Findall,save,saveandflush, etc.). The Crudrepository sub-interface pagingandsortingrepository defines the sorting and paging operations.
Different data access technologies provide different repository implementations, such as spring data JPA, which is jparepository.
Spring Data JPA
is a standard specification for ORM (Domain model and database table mapping) technologies. The so-called specification is defined only by standard rules (such as annotations, excuses) and does not provide implementations. Implementations can be implemented by software providers such as Hibernate.
1. Define an interface that inherits Jparepository, which means that findall,save,saveandflush are already available by default, such as Personrepository.
2. Open Spring data JPA support via @enablejparepositories ("packet at the data layer")
3. Define query methods, which can be defined according to keywords (and,or,orderby, etc.) or @namedquery or @query custom SQL statements;
4. Entity classes plus @entity annotations indicating that an entity class (such as person) is mapped to a database table
5. Run: Person p = personrepository.findbynameandaddress (name,address);
Non-relational database
Mongodb
MongoDB is a document-based, storage-type database.
Spring data MongoDB provides object/document annotations:
@Document: Mapping a domain object to a document with MongoDB
@Id: Map The current property is Id
@DbRef: The current property will refer to other documents
@Field: Define a name for the document's properties
@version: Use the current property as a version
eg. @Document//Map domain model and MongoDB documentation
public class person{
@Id//Indicates that this property is a document Id
Private String ID;
private String name;
@Field ("Locs")//This property has a name of locs in the document
Private collection<location> locations = new linkedhashset<location> ();
}
Public interface Personrepository extends Mongorepository<person,string>{
}
Redis
Memory data storage based on key-value pairs.
Spring Data Redis provides us with two templates for Reidstemplate and Stringreidstemplate, where Stringreidstemplate operates only on data that is character-based. The main data access methods provided by the template are:
Opsforvalue () operation only data with simple properties
Opsforlist () Data with List
Opsforset () data with Set
Opsforzset () contains Zset (ordered set) data
Opsforhash () data with hash
When our data is stored in Redis, both key and value are serialized to the database through the serializer provided by spring.
valueoperates<string,string> stringredistemplate = Redistemplate.opsforvalue ();
Stringredistemplate.set ("Test", "Test", 10,timeunit.second);
String value = Stringredistemplate.get ("test");
Stringredistemplate.delete ("test"); Delete a key
Redistemplate.opsforset (). Add ("Testset", "1");
Set<string> members = Redistemplate.opsforset (). Members ("Testset");]
... ...
Spring Data REST
Spring data rest supports the automatic conversion of repository such as spring data JPA to rest services.
Direct access using GET request: HTTP://localhost:8080/persons, get list data json format, HTTP://LOCALHOST:8080/PERSONS/7, get 7th data.
The node path is HTTP://localhost:8080/persons, which is the default rule for spring data rest and adds s to form the path after the entity class.
Modify the mapping domain name: implemented by @repositoryrestresource annotations on the repository implementation class.
@RepositoryRestResource (Path = "People")
Public interface Personrepository extends jparepository{}
Spring Boot (iii) data access