Various Repository as an interface provided by Spring Data JPA

Various Repository Interface inheritance:

Repository :

public interface UserRepository  extends Repository<User, Integer> {
}
  • The method of naming query:
findByNameLike(String name)
findByName(String name)
findByNameAndAge(String name, Integer age)
findByNameOrAddress(String name)
...

reference:

https://www.jianshu.com/p/1d6f27f675bb
https://www.jianshu.com/p/1d6f27f675bb
  • Based @Query notes queries and updates

    1. @Query annotation-based queries

      • JPQL way

        @Query("form User WHERE name = ?1")
        List<User> findByName(String name);

        ps: JPQL placeholder 1 starting from the index.

      • SQL way

        @Query("select * from User WHERE name = ?1")
        List<User> findByName(String name);
    2. Based update @Query comment

      The need to increase @Modifying update @Query annotation-based

      • JPQL way

        @Query("Update User set name = ?1 WHERE id = ?2")
        @Modifying
        int updateNameAndId(String name, Integer id);
      • SQL way

        @Query("update user set name = ?1 WHERE id = ?2")
        @Modifying
        int updateNameAndId(String name, Integer id);

CrudReposiroty :

CrudRepository: 主要是进行增删改查的方法 

PagingAndSortingRepository :

PagingAndSortingRepository: 主要是进行排序或者分页

JPARepository

JPARepository: 主要对继承父接口中方法的返回值进行了适配,因为在父类接口中通常都返回迭代器,需要我们自己进行强制类型转化。而在JpaRepository中,直接返回了List

JpaSpecificationExecutor

JpaSpecificationExecutor: 主要提供了多条件查询的支持,并且可以在查询中添加分页和排序。 

to sum up :

Spring Data Jpa provided a total of

Repository:
  • Support method named queries (provided findBy + attribute method)

  • @Query

    HQL: nativeQuery default false

      SQL: nativeQuery default true

          更新的时候,需要配合@Modifying使用
CurdRepository:

Repository inherited mainly provides the data CRUD

PagingAndSortRepository:

Inherited CrudRepository provides paging and sorting of data, paging or only drawback is that all sorts of data, can not do conditional

JpaRepository:

Inherited interfaces PagingAndSortRepository, developers often used, mainly inherited PagingAndSortRepository, the return value type of did fit

JpaSpecificationExecutor

Providing multi-criteria query

Guess you like

Origin www.cnblogs.com/linhuaming/p/11823952.html