Spring Boot JPA-JpaRepository

本文介绍了Spring Data在数据访问控制层的应用,特别是JpaRepository接口的使用。通过继承JpaRepository,开发者可以获得基本的CRUD及分页排序功能,而无需编写实现类。此外,还探讨了Spring Data如何根据查询方法自动解析成SQL。

SpringData

AppleFramework在数据访问控制层采用了Spring Data作为这一层的解决方案
提供了一整套数据访问层(DAO)的解决方案,致力于减少数据访问层(DAO)的开发量。它使用一个叫作Repository的接口类为基础,它被定义为访问底层数据模型的超级接口。而对于某种具体的数据访问操作,则在其子接口中定义。

public interface Repository<T, ID extends Serializable> { 
}

所有继承这个接口的interface都被spring所管理,此接口作为标识接口,功能就是用来控制domain模型的。
Spring Data可以让我们只定义接口,只要遵循spring data的规范,就无需写实现类。

Repository

Repository(资源库):通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调。这个叫法就类似于我们通常所说的DAO,在这里,我们就按照这一习惯把数据访问层叫Repository.
Spring Data给我们提供几个Repository,基础的Repository提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下:
Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类,方便Spring自动扫描识别
CrudRepository: 继承Repository,实现了一组CRUD相关的方法
PagingAndSortingRepository: 继承CrudRepository,实现了一组分页排序相关的方法
JpaRepository: 继承PagingAndSortingRepository,实现一组JPA规范相关的方法
JpaSpecificationExecutor: 比较特殊,不属于Repository体系,实现一组JPA Criteria查询相关的方法
我们自己定义的XxxxRepository需要继承JpaRepository,这样我们的XxxxRepository接口就具备了通用的数据访问控制层的能力。

CrudRepository
/**
     * 保存一个实体。
     */
    <S extends T> S save(S entity);

    /**
     * 保存提供的所有实体。
     */
    <S extends T> Iterable<S> saveAll(Iterable<S> entities);

    /**
     * 根据id查询对应的实体。
     */
    Optional<T> findById(ID id);

    /**
     * 根据id查询对应的实体是否存在。
     */
    boolean existsById(ID id);

    /**
     * 查询所有的实体。
     */
    Iterable<T> findAll();

    /**
     * 根据给定的id集合查询所有对应的实体,返回实体集合。
     */
    Iterable<T> findAllById(Iterable<ID> ids);

    /**
     * 统计现存实体的个数。
     */
    long count();

    /**
     * 根据id删除对应的实体。
     */
    void deleteById(ID id);

    /**
     * 删除给定的实体。
     */
    void delete(T entity);

    /**
     * 删除给定的实体集合。
     */
    void deleteAll(Iterable<? extends T> entities);

    /**
     * 删除所有的实体。
     */
    void deleteAll();
PagingAndSortingRepository
/**
     * 返回所有的实体,根据Sort参数提供的规则排序。
     */
    Iterable<T> findAll(Sort sort);

    /**
     * 返回一页实体,根据Pageable参数提供的规则进行过滤。
     */
    Page<T> findAll(Pageable pageable);
JpaRepository
/**
     * 将所有未决的更改刷新到数据库。
     */
    void flush();

    /**
     * 保存一个实体并立即将更改刷新到数据库。
     */
    <S extends T> S saveAndFlush(S entity);

    /**
     * 在一个批次中删除给定的实体集合,这意味着将产生一条单独的Query。
     */
    void deleteInBatch(Iterable<T> entities);

    /**
     * 在一个批次中删除所有的实体。
     */
    void deleteAllInBatch();

    /**
     * 根据给定的id标识符,返回对应实体的引用。
     */
    T getOne(ID id);

使用JpaRepository查询

基本查询也分为两种,一种是spring data默认已经实现,一种是根据查询的方法来自动解析成SQL。

KeywordSampleJPQL snippet
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is,EqualsfindByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age ⇐ ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNullfindByAgeIsNull… where x.age is null
IsNotNull,NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)
ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection age)… where x.age not in ?1
TRUEfindByActiveTrue()… where x.active = true
FALSEfindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)

1.按照Spring Data的规范的规范,查询方法以find | read | get 开头,涉及查询条件时,条件的属性用条件关键字连接,
2.要注意的是:条件属性以首字母大写。

参考

spring mvc 的jpa JpaRepository数据层 访问方式汇总
SpringDataJpa——JpaRepository增删改查

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值