Repository接口是Spring Data的核心接口,其不提供任何方法(空接口,即标记接口),开发者需要在自定义的接口中声明所需要的方法。该接口的可以使开发者只定义接口,并遵循Spring Data的方法声明规范,而可无需写实现类。
其中,Repository接口的具体测试代码下载地址:http://download.youkuaiyun.com/download/bingbeichen/9809103。
public interface Repository<T, ID extends Serializable> {}
1. 使用Repository接口
自定义的持久层接口若实现Repository,则该接口会被Spring的IoC容器识别为一个Repository的Bean,并纳入到IoC容器中,进而可以再该接口中定义满足一定规范的方法。
方式一:自定义持久层接口继承于Repository接口或其子接口
public interface PersonRepsotory extends Repository<Person, Integer> { //... }
方式二:在自定义的持久层接口上使用@RepositoryDefinition注解
@RepositoryDefinition(domainClass=Person.class, idClass=Integer.class)
public interface PersonRepsotory { //... }
Repository子接口概述:
2. CrudRepository接口
// public interface PersonRepsotory extends CrudRepository<Person, Integer>{}
@Test
public void testCrudReposiory(){
List<Person> persons = new ArrayList<>();
for(int i = 'a'; i <= 'z'; i++){
Person person = new Person();
person.setAddressId(i + 1);
person.setBirth(new Date());
person.setEmail((char)i + "" + (char)i + "@atguigu.com");
person.setLastName((char)i + "" + (char)i);
persons.add(person);
}
personService.savePersons(persons);
}
package com.atguigu.springdata;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class PersonService {
@Autowired
private PersonRepsotory personRepsotory;
@Transactional
public void savePersons(List<Person> persons){
// 批量保存
personRepsotory.save(persons);
}
@Transactional
public void updatePersonEmail(String email, Integer id){
personRepsotory.updatePersonEmail(id, email);
}
}
3. PagingAndSortingRepository接口
// public interface PersonRepsotory extends PagingAndSortingRepository<Person, Integer>{}
@Test
public void testPagingAndSortingRespository(){
int pageNo = 6 - 1; //pageNo从0开始
int pageSize = 5;
//Pageable 接口通常使用的其 PageRequest 实现类. 其中封装了需要分页的信息
//排序相关的. Sort 封装了排序的信息
//Order 是具体针对于某一个属性进行升序还是降序.
Order order1 = new Order(Direction.DESC, "id");
Order order2 = new Order(Direction.ASC, "email");
Sort sort = new Sort(order1, order2);
PageRequest pageable = new PageRequest(pageNo, pageSize, sort);
Page<Person> page = personRepsotory.findAll(pageable);
System.out.println("总记录数: " + page.getTotalElements());
System.out.println("当前第几页: " + (page.getNumber() + 1));
System.out.println("总页数: " + page.getTotalPages());
System.out.println("当前页面的 List: " + page.getContent());
System.out.println("当前页面的记录数: " + page.getNumberOfElements());
}
4. JpaRepository接口
// public interface PersonRepsotory extends JpaRepository<Person, Integer>{}
@Test
public void testJpaRepository(){
Person person = new Person();
person.setBirth(new Date());
person.setEmail("xy@atguigu.com");
person.setLastName("xyz");
person.setId(28);
Person person2 = personRepsotory.saveAndFlush(person);
System.out.println(person == person2);
}
5. JpaSpecificationExecutor接口(不属于Repository体系)
// public interface PersonRepsotory extends JpaSpecificationExecutor<Person>{}
/**
* 目标: 实现带查询条件的分页,条件为id>5
*
* 调用 JpaSpecificationExecutor 的 Page<T> findAll(Specification<T> spec, Pageable pageable);
* Specification: 封装了 JPA Criteria 查询的查询条件
* Pageable: 封装了请求分页的信息: 例如 pageNo, pageSize, Sort
*/
@Test
public void testJpaSpecificationExecutor(){
int pageNo = 3 - 1;
int pageSize = 5;
PageRequest pageable = new PageRequest(pageNo, pageSize);
//通常使用 Specification 的匿名内部类
Specification<Person> specification = new Specification<Person>() {
/**
* @param *root: 代表查询的实体类.
* @param query: 可以从中可到 Root 对象, 即告知 JPA Criteria 查询要查询哪一个实体类. 还可以
* 来添加查询条件, 还可以结合 EntityManager 对象得到最终查询的 TypedQuery 对象.
* @param *cb: CriteriaBuilder 对象. 用于创建 Criteria 相关对象的工厂. 当然可以从中获取到 Predicate 对象
* @return: *Predicate 类型, 代表一个查询条件.
*/
@Override
public Predicate toPredicate(Root<Person> root,
CriteriaQuery<?> query, CriteriaBuilder cb) {
Path path = root.get("id");
Predicate predicate = cb.gt(path, 5);
return predicate;
}
};
Page<Person> page = personRepsotory.findAll(specification, pageable);
System.out.println("总记录数: " + page.getTotalElements());
System.out.println("当前第几页: " + (page.getNumber() + 1));
System.out.println("总页数: " + page.getTotalPages());
System.out.println("当前页面的 List: " + page.getContent());
System.out.println("当前页面的记录数: " + page.getNumberOfElements());
}
6. 为某一个Repository上添加自定义方法
7. 为所有的Repository都添加自定义方法
2470

被折叠的 条评论
为什么被折叠?



