JPA usage of spring boot

1. JPA:

JPA is the abbreviation of Java Persistence API. The ORM specification proposed by sun company is implemented by various ORM frameworks (Hibernate, oplink). It unifies the use of ORM framework and makes it easier for developers to switch and learn ORM framework.

2, Spring Data:

Is an open source framework to simplify database access and support cloud services. Its main goal is to make data access convenient and fast, and support map reduce framework and cloud computing data services. Spring Data includes several subprojects, including JPA Hadoop, etc

3, Spring Data JPA:

One of the Spring Data projects integrates the use of JPA and implements the basic operations of adding, deleting, modifying, querying and paging. The diagram is as follows:

4, There are four query methods in spring data jpa:

1. @ Query annotation Query

  • Introduction: add interface method in dao and add @ Query annotation on method and write hql statement in annotation to Query
  • 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);
}

@Query parameter description:

Name brief introduction
value Specifies the JPQL statement, which is native when nativeQuery=true
countQuery JPQL statement specifying count is generated automatically if it is not specified. When nativeQuery=true, it is a native sql statement
countProjection According to which field count is generally the default
nativeQuery The default value is false, indicating whether the value contains a native Sql statement
name Specify the name of a query, which must be unique. If not specified, the default generation rule is: {$domainClass}.${queryMethodName}
countName Specify the query name of a count, which must be unique. If not specified, the default generation rule is: {$domainClass}.${queryMethodName}.count

2. Custom simple query

  • Introduction: add interface methods in dao and name them according to the pattern supported by spring data jpa. spring data jpa will generate sql statements according to the specified rules
  • Example:
public interface TestDao extends JpaRepository<Test, Long> {
 List<Test> findByKeyword(String keyword);
}

3. Query by Example

  • Introduction: the JpaRepository interface contains QueryByExampleExecutor, which supports querying entities as references
  • Example:
//service (if testDao inherits the JpaRepository interface)
  public List<Test> findTests() {
  Test test = new Test();
  test.setName("aaa");
  return testDao.findAll(Example.of(test));
 }
  • Note: as long as there is value in the entity, it will be put into the query criteria. If the entity contains the basic type such as int, it will match with 0.

V. query result format

1. Entity list uses the default form

2,Map-List

  • Note: the as name must be written, otherwise the key in the map 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);

 3,Vo-List

  • Note: similar to Map, it is initialized in the form of construction method, and the order of incoming must be the same as that of 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);

4,Projection-List

  • Note: testproject is an interface with corresponding get methods. In this mode, you do not need to use the new method to pass 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);
public interface TestProjection{
    @Value("#{target.name+ '-' + target.value}")
    String getAll();
    String getName();
    String getValue();
}

Six, sort

  • Static sorting: write directly in jpql statement
  • Parameter sorting:
    • Jpassort.unsafe ("collation"): after the collation statement is directly appended to the sql, it is used when the collation is not just a property but a certain processing, such as: length (property name)
    • Sort.by("property name"): security check will be performed. The property name must be a property 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);
//service medium
 List<TestProjection>findTest(String keyWord,String prop){
    testdao.findTest(keyWord,Sort.by(prop))
 }

VII. Pagination

Add the Pageable pageable parameter in the dao method to org.springframework.data.domain.Pageable

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

 

Published 367 original articles, won praise 117, visited 550000+
His message board follow

Tags: Spring SQL Java Hibernate

Posted on Fri, 14 Feb 2020 05:16:34 -0500 by Tracer