描述:
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 语句 |
---|---|---|
Distinct | findDistinctByLastnameAndFirstname | select distinct … where x.lastname = ?1 and x.firstname = ?2 |
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is , Equals | findByFirstname ,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 , Null | findByAge(Is)Null | … 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<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … 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。
如果这篇文章,有帮助到大家的,请给作者一个一键三连,谢谢。