A, the JPA:

JPA is the abbreviation of Java Persistence API, ORM specification proposed by Sun Company. The specific implementation is implemented by various ORM frameworks (Hibernate, Oplink), which unified the use of ORM framework, making it easier for developers to switch and learn ORM framework.

Ii. Spring Data:

Is an open source framework for simplifying database access and enabling cloud services. Its main goal is to make data access easy and fast, and to support the Map-Reduce framework and cloud computing data services. Spring Data contains several subprojects including JPA Hadoop, etc

Spring Data JPA

One of Spring Data’s projects integrates the use of JPA to implement basic add, delete, change, search, and pagination operations. The diagram is as follows:

JpaRepository’s own query method:

Query methods Jpa method name return
The query findAll() The list collection
The primary key query findById() Optional< UserEntity> Entity object
User-defined query conditions findByNameAndPassword(String name, Long password) UserEntity entity
User-defined fuzzy query findByNameLike(“%”+name+”%”) The list collection
Custom Query You need to declare custom methods in JPA to ensure naming conventions! Can be customized

In Spring Data JPA, there are four types of query:

1, @query annotation Query

  • Summary: Add interface methods to dao and add @Query annotations to the methods and write HQL statements in the annotations to perform queries
  • Example:
public interface TestDao extends JpaRepository<Test.Long> {
 @Query("select t from Test t where t.name = :keyWord")
 List<Test> findTest(@Param("keyWord") String keyWord);
}
Copy the code

@query Parameter description

The name of the Introduction to the
value Specify JPQL statements, which are native SQL statements when nativeQuery=true
countQuery JPQL statements that specify count are automatically generated if they are not specified, and are native SQL statements when nativeQuery=true
countProjection Depending on which field count is generally the default
nativeQuery The default is false to indicate whether the value is a native Sql statement
name Specify a query name that must be unique. If not specified, the default generation rule is:{$domainClass}.${queryMethodName}
countName Specify a query name for count, which must be unique. If not specified, the default generation rule is:{$domainClass}.${queryMethodName}.count

2. Customize simple queries

  • Summary: Add interface methods to the DAO and name them according to the schema supported by Spring Data JPA, which generates SQL statements according to the specified rules
  • Example:
public interface TestDao extends JpaRepository<Test.Long> {
 List<Test> findByKeyword(String keyword);
}
Copy the code

3, query by Example

  • Summary: QueryByExampleExecutor is included in the JpaRepository interface, which supports querying entities as references
  • Example:
// Service (provided testDao inherits the JpaRepository interface)
  public List<Test> findTests(a) {
  Test test = new Test();
  test.setName("aaa");
  return testDao.findAll(Example.of(test));
 }
Copy the code
  • Note: Anything in the entity that has a value will be included in the query condition. If the entity contains an underlying type such as int, it will be matched with 0.

Fifth, query result format

1. Use the default entity-list format

2, the Map – the List

  • Note: As name must be written. If the map is not written, key is the subscript.
@Query("select new map(t.name as name,t.value as value) from Test t where t.name = :keyWord")
 List<Map<String,Object>> findTest(@Param("keyWord") String keyWord);
Copy the code

3, Vo – the List

  • Note: Like Map, initialization is done in the form of constructors, which must be passed in the same order as the constructor
@Query("select new TestVo(t.name as name,t.value as value) from Test t where t.name = :keyWord")
 List<TestVo> findTest(@Param("keyWord") String keyWord);
Copy the code

4, the Projection – the List

  • Note: TestProjection is an interface with a corresponding GET method. This model does not need to use the new method to transmit values. The @Value annotation is supported to specify the data source of getAll.
@Query("select t.name as name,t.value as value from Test t where t.name = :keyWord")
 List<TestProjection> findTest(@Param("keyWord") String keyWord);
Copy the code
public interface TestProjection{
    @Value("#{target.name+ '-' + target.value}")
    String getAll(a);
    String getName(a);
    String getValue(a);
}
Copy the code

Six, sorting,

  • Static sorting: just write it in a JPQL statement

  • Parameter sorting:

    • Jpasort. unsafe(” collation “) : Appends collation statements directly after SQL, for example, length(attribute name), when sorting is not just an attribute.
    • Sort.by(” Attribute name “) : Security checks are performed. The attribute name must be an attribute in the entity
@Query("select t.name as name,t.value as value from Test t where t.name = :keyWord")
 List<TestProjection> findTest(@Param("keyWord") String keyWord,Sort sort);
Copy the code
/ / service
 List<TestProjection>findTest(String keyWord,String prop){
    testdao.findTest(keyWord,Sort.by(prop))
 }
Copy the code

Seven, paging

Increase in the dao methods Pageable Pageable parameters can org. Springframework. Data. Domain. Pageable

 @Query("select t from Test t where t.name = :keyWord")
Page<Test> findTest(@Param("keyWord") String keyWord,Pageable pageable);
Copy the code