Spring4 series three spring data analysis

Back-end development, simply put, is to model different businesses, abstract data, and perform various CRUD on data. According to different needs, there are probably several types of data: database (mysql), nosql (redis), full-text search engine (solr), big data (hdfs), cloud storage and CDN. The first 2 of these are for transactional operations, the third focuses on fast lookups, fuzzy lookups (find the most similar), the fourth is for analytics (data warehouse upgrades), and finally pure reliable storage (multiple backups) hard disk). In spring, all places that can store and process data are called Repository. Its goal is to provide a unified data access interface and avoid writing SQL as much as possible.

 

1. Overview of related jars

spring-data-commons encapsulates repository and related annotations

spring-data-jpa implements jpaRepository

2. Realization

1. Basic abstraction

–Repository: It is just an identifier, indicating that any inheriting it is a warehouse interface class, which is used to encapsulate the crud of the database

–CrudRepository: inherits Repository and implements a set of CRUD-related methods 

–PagingAndSortingRepository: inherits CrudRepository and implements a set of methods related to paging sorting 

–JpaRepository: inherits PagingAndSortingRepository and implements a set of methods related to the JPA specification 

– A custom XxxxRepository needs to inherit JpaRepository, so the XxxxRepository interface has the capability of a general data access control layer.

–JpaSpecificationExecutor: implements a set of JPACriteria query related methods 

 

2. The significance of the existence of the service layer is to build pages, JPACriteria, etc., combine the underlying repository, and the controller layer can directly call the corresponding crud

 

3. Declare methods in the Repository subinterface 

 * 1. It is not declared casually, but needs to comply with certain specifications 

 * 2. The query method is developed with find|read|get 

 * 3. Involving conditional query, the attribute of condition needs to define keyword connection 

 * 4. It should be noted that the properties of the condition are capitalized 

 * 5. Supports cascading query of attributes. If the current class has qualified attributes, it will be used first, and cascading attributes will not be used. 

 * If you need to use cascading properties, use -- to connect between properties 

4. Examples of custom queries

    // Way 1 to pass parameters for the @Query annotation: use placeholders  

    @Query("SELECT P FROM Person P where P.lastName=?1 AND P.email=?2")  

    List<Person> testQueryAnnotationParams1(String lastName,String email);  

      

    //Method 2 of passing parameters for the @Query annotation: using the named parameter method  

    @Query("SELECT P FROM Person P where P.lastName=:lastName AND P.email=:email")  

    List<Person> testQueryAnnotationParams2(@Param("email")String email,@Param("lastName")String lastName);  

          

    //Spring Data runs adding %% to the placeholder  

    @Query("select p from Person p where p.lastName like %?1% or p.email like %?2%")  

    List<Person> testQueryAnnotationLikeParam(String lastName,String email);  

      

    //Set nativeQuery=true to use native sql query  

    @Query(value="SELECT count(id) FROM jpa_persons",nativeQuery=true)  

    public long getTotalCount();  

      

      

    //Update and delete operations can be completed through custom JPQL, note: JPQL does not support Insert operations  

    //Write a JPQL statement in the @Query annotation, but it must be modified with @Modify to notify SpringData, which is an Update or Delete  

    //Update or delete operations need to use transactions. At this time, you need to define the Service layer and add transaction operations to the methods of the service layer.  

    //By default, each method of SpringData has a transaction, but it is a read-only transaction, and they cannot complete the modification operation  

    @Modifying  

    @Query("update Person p set p.email=:email where id=:id")  

    void updatePersonEmail(@Param("id")Integer id,@Param("email")String email);

5. Corresponding realization

jpa ->JpaRepository 

solr -> SolrTemplate 

redis->RedisTemplate

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326504575&siteId=291194637