Spring Data JPA为Java Persistence API(JPA)提供了存储库支持。它简化了需要访问JPA数据源的应用程序的开发。官方文档,请先阅读。 JPA
Spring Data存储库抽象中的中央接口是Repository
。它需要域类以及域类的ID类型作为类型参数来进行管理。该接口主要用作标记接口,以捕获要使用的类型并帮助您发现扩展该接口的接口 。
该接口中是没有任何的接口方法,所有的操作实现是子接口去实现:
@Indexed
public interface Repository {
}
该接口看名字就应该知道了,提供了CRUD等方法的定义,所以我们平时只需要继承这个接口就可以使用jpa来实现crud了。
public interface CrudRepository extends Repository {
S save(S var1);
Iterable saveAll(Iterable var1);
Optional findById(ID var1);
boolean existsById(ID var1);
Iterable findAll();
Iterable findAllById(Iterable var1);
long count();
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable extends T> var1);
void deleteAll();
}
可以看到接口中定义的方法。
CrudRepository
,有一个PagingAndSortingRepository
抽象,它添加了其他方法来简化对实体的分页访问
public interface PagingAndSortingRepository extends CrudRepository {
Iterable findAll(Sort var1);
Page findAll(Pageable var1);
}
可以看到接口中,只有这两个接口方法,用于分页的处理:
PagingAndSortingRepository repository = // … get access to a bean
Page users = repository.findAll(PageRequest.of(1, 20));
使用 实例:
/**
* 分页查询 继承 PagingAndSortingRepository 就可以实现
* @return
*/
@GetMapping(path="pageList")
public Page list(Integer page,Integer size) {
Page all = userRepository.findAll(PageRequest.of(page, size));
return all;
}
List 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 |
… where x.age in ?1 |
NotIn |
findByAgeNotIn(Collection |
… 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 getAllUsers() {
userRepository.findAll(PageRequest.of(1, 20));
return userRepository.findAll();
}
/**
* 分页查询 继承 PagingAndSortingRepository 就可以实现
* @return
*/
@GetMapping(path="pageList")
public Page list(Integer page,Integer size) {
Page 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。
如果这篇文章,有帮助到大家的,请给作者一个一键三连,谢谢。