SpringBoot 整合 JPA 实现CRUD

描述:

Spring Data JPA为Java Persistence API(JPA)提供了存储库支持。它简化了需要访问JPA数据源的应用程序的开发。官方文档,请先阅读。 JPA

核心概念:

Spring Data存储库抽象中的中央接口是Repository。它需要域类以及域类的ID类型作为类型参数来进行管理。该接口主要用作标记接口,以捕获要使用的类型并帮助您发现扩展该接口的接口 。

该接口中是没有任何的接口方法,所有的操作实现是子接口去实现:

@Indexed
public interface Repository<T, ID> {
}

在这里插入图片描述

CrudRepository

该接口看名字就应该知道了,提供了CRUD等方法的定义,所以我们平时只需要继承这个接口就可以使用jpa来实现crud了。

public interface CrudRepository<T, ID> extends Repository<T, ID> {
    <S extends T> S save(S var1);

    <S extends T> Iterable<S> saveAll(Iterable<S> var1);

    Optional<T> findById(ID var1);

    boolean existsById(ID var1);

    Iterable<T> findAll();

    Iterable<T> findAllById(Iterable<ID> var1);

    long count();

    void deleteById(ID var1);

    void delete(T var1);

    void deleteAll(Iterable<? extends T> var1);

    void deleteAll();
}

可以看到接口中定义的方法。

PagingAndSortingRepository

CrudRepository,有一个PagingAndSortingRepository抽象,它添加了其他方法来简化对实体的分页访问

public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
    Iterable<T> findAll(Sort var1);

    Page<T> findAll(Pageable var1);
}

可以看到接口中,只有这两个接口方法,用于分页的处理:

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(PageRequest.of(1, 20));

使用 实例:

    /**
     * 分页查询 继承  PagingAndSortingRepository 就可以实现
     * @return
     */
    @GetMapping(path="pageList")
    public  Page<User> list(Integer page,Integer size) {
        Page<User> all = userRepository.findAll(PageRequest.of(page, size));
        return all;
    }

使用方法名创建查询

List <User> findByEmailAddressAndLastname(String emailAddress,String lastname); 

使用对应的表达式就可以自动帮助我们生成查询。

JPA支持的关键字以及包含该关键字的方法所转换的含义

关键字样本JPQL 语句
DistinctfindDistinctByLastnameAndFirstnameselect distinct … where x.lastname = ?1 and x.firstname = ?2
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is, EqualsfindByFirstname,findByFirstnameIs,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
IsNull, NullfindByAge(Is)Null… 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<Age> ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection<Age> ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstname) = UPPER(?1)

自己写了一个简单的crud ,供参考:

/**
     * 没有id 就新增,有就修改
     * @param user
     */
    @PostMapping(path="save")
    public void addNewUser (User user) {
        userRepository.save(user);
    }

    /**
     * 获取所有的用户对象
     * @return
     */
    @GetMapping(path="list")
    public  Iterable<User> getAllUsers() {
     userRepository.findAll(PageRequest.of(1, 20));
        return userRepository.findAll();
    }


    /**
     * 分页查询 继承  PagingAndSortingRepository 就可以实现
     * @return
     */
    @GetMapping(path="pageList")
    public  Page<User> list(Integer page,Integer size) {
        Page<User> all = userRepository.findAll(PageRequest.of(page, size));
        return all;
    }

    /**
     * 删除用户数据
     * @param id
     */
    @GetMapping(path="del")
    public  void deleteUsers(Integer id) {
        userRepository.deleteById(id);
    }

其他好玩的,请自行读文档,调试开发,实例代码在github上面需要的请自行拉取:spring-boot-integrate 然后后续会集成更多的模块进去,需要请点个star。

如果这篇文章,有帮助到大家的,请给作者一个一键三连,谢谢。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值