首页 > 技术文章 > springData中dao继承

zxh06820 2020-04-04 23:10 原文

编写了符合Spring Data JPA 规范的Dao层接口之后,就可以使用定义好的Dao层接口进行客户的基本CRUD操作

 * JpaRepository<实体类类型,主键类型>:用来完成基本CRUD操作

 * JpaSpecificationExecutor<实体类类型>:用于复杂查询(分页等查询操作)

Spring Data JPA只是对标准JPA操作进行了进一步封装,简化了Dao层代码的开发

 

2.对于复杂的查询场景,使用@Query注解

@Query 注解的使用,只需在方法上面标注该注解,同时提供一个JPQL查询语句即可

 

@Query 使用jpql的方式查询。?1代表参数的占位符,其中1对应方法中的参数索引

    @Query(value="from Customer where custName = ?1")

    public Customer findCustomer(String custName);

 

3.使用 @Query 的同时,用 @Modifying 来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询

    @Query(value="update Customer set custName = ?1 where custId = ?2")

    @Modifying

    public void updateCustomer(String custName,Long custId);

 

4.按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。

    //方法命名方式查询(根据客户名称查询客户)

    public Customer findByCustName(String custName);

 

5.如图:

Keyword

Sample

JPQL

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstnameIs,

findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age ⇐ ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection age)

… where x.age not in ?1

TRUE

findByActiveTrue()

… where x.active = true

FALSE

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

 

6在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询。相比JPQL,其优势是类型安全,更加的面向对象。

/**
 *    JpaSpecificationExecutor中定义的方法
 **/
 public interface JpaSpecificationExecutor<T> {
       //根据条件查询一个对象
     T findOne(Specification<T> spec);    
       //根据条件查询集合
     List<T> findAll(Specification<T> spec);
       //根据条件分页查询
     Page<T> findAll(Specification<T> spec, Pageable pageable);
       //排序查询查询
     List<T> findAll(Specification<T> spec, Sort sort);
       //统计查询
     long count(Specification<T> spec);
}

 

对于JpaSpecificationExecutor,这个接口基本是围绕着Specification接口来定义的。我们可以简单的理解为,Specification构造的就是查询条件。

    //构造查询条件
    /**
    *    root    :Root接口,代表查询的根对象,可以通过root获取实体中的属性
    *    query   :代表一个顶层查询对象,用来自定义查询
    *    cb      :用来构建查询,此对象里有很多条件方法
    **/
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb);

 

推荐阅读