Spring Data| spring data(3) Repository的子接口
接上篇 spring data(2)
使用repository的子接口,之间使用接口的方法即可使用。
在idea中按住alt+ctrl+B键查看repository的子接口,可以看到有四个子接口。
CrudRepository接口
有以下的默认方法
以save方法为例进行测试;
package com.dsdj.repository;
import com.dsdj.domain.Employee;
import org.springframework.data.repository.CrudRepository;
public interface EmployeeCrudRepository extends CrudRepository<Employee,Integer> {
}
EmployeeService.java
package com.dsdj.service;
import com.dsdj.domain.Employee;
import com.dsdj.repository.EmployeeCrudRepository;
import com.dsdj.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName employeeService
* @Description TODO
* @Author dsdj
* @Date 2018/11/4 下午2:46
* @Version 1.0
**/
@Service
public class EmployeeService {
@Autowired
private EmployeeCrudRepository employeeCrudRepository;
@Transactional
public void save(){
List<Employee> employees = new ArrayList<>();
for (int i = 0 ; i<100;i++){
Employee employee = new Employee();
employee.setAge(i);
employee.setName("test"+i);
employees.add(employee);
}
employeeCrudRepository.save(employees);
}
}
测试类
@Test
public void testSave(){
employeeService.save();
}
测试成功:只需要实现子接口即可,接口中不需要些save方法;
其他默认方法类似。
PagingAndSortingRepository接口
- 该接口包含分页和排序的功能
- 带排序的查询:findAll(Sort sort)
- 带分页的查询:findAll(Pageable pageable)
- 继承crud的接口
默认方法
源码:
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
/**
* Returns all entities sorted by the given options.
*
* @param sort
* @return all entities sorted by the given options
*/
Iterable<T> findAll(Sort sort);
/**
* Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object.
*
* @param pageable
* @return a page of entities
*/
Page<T> findAll(Pageable pageable);
}
测试:Page findAll(Pageable pageable);
需要pageable对象,查看源码是接口,查找其实现类org.springframework.data.domain.PageRequest
coding
public interface EmployeePagingAndSortingRepository extends PagingAndSortingRepository<Employee,Integer> {
}
@Resource(name = "employeePagingAndSortingRepository")
private EmployeePagingAndSortingRepository employeePagingAndSortingRepository;
public void findAllPage(){
Pageable pageable = new PageRequest(0,5);
Page<Employee> page = employeePagingAndSortingRepository.findAll(pageable);
System.out.println("总数"+page.getTotalElements());
System.out.println("总页数"+page.getTotalPages());
System.out.println("当前页面集合"+page.getContent().toString());
System.out.println("当前第几页"+page.getNumber());
System.out.println("当前页面的记录数"+page.getNumberOfElements());
}
测试
@Test
public void testFindAllPage(){
employeeService.findAllPage();
}
输出
总数8
总页数2
当前页面集合[Employee{id=1, name='zhangsan', age=9}, Employee{id=2, name='test1', age=20}, Employee{id=3, name='test2', age=21}, Employee{id=4, name='test3', age=22}, Employee{id=5, name='test4', age=20}]
当前第几页0
当前页面的记录数5
测试:Iterable findAll(Sort sort);
coding 2
public void findAllSort(){
Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");
/**
* public Sort(Order... orders)
*/
Sort sort = new Sort(order);
Pageable pageable = new PageRequest(0,5,sort);
Page<Employee> page = employeePagingAndSortingRepository.findAll(pageable);
System.out.println("总数"+page.getTotalElements());
System.out.println("总页数"+page.getTotalPages());
System.out.println("当前页面集合"+page.getContent().toString());
System.out.println("当前第几页"+page.getNumber());
System.out.println("当前页面的记录数"+page.getNumberOfElements());
}
测试
@Test
public void testFindAllSort(){
employeeService.findAllSort();
}
测试结果
总数100
总页数20
当前页面集合[Employee{id=100, name='test99', age=99}, Employee{id=99, name='test98', age=98}, Employee{id=98, name='test97', age=97}, Employee{id=97, name='test96', age=96}, Employee{id=96, name='test95', age=95}]
当前第几页0
当前页面的记录数5
over
JpaRepository接口
可以看到Jpa继承了上述的两个接口
方法
使用和之前的接口类似,就不演示了。
JpaSpecificationExecutor接口
这个接口和前面的接口不同
分页条件查询
coding
EmployeeJpaSpecificationExecutor
public interface EmployeeJpaSpecificationExecutor extends JpaRepository<Employee,Integer> ,
JpaSpecificationExecutor<Employee> {
}
测试类
package com.dsdj.repository;
import com.dsdj.domain.Employee;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.*;
public class EmployeeJpaSpecificationExecutorTest {
private ApplicationContext ctx = null;
private EmployeeJpaSpecificationExecutor employeeJpaSpecificationExecutor;
@Before
public void setup(){
ctx = new ClassPathXmlApplicationContext("bean-jpa.xml");
employeeJpaSpecificationExecutor = (EmployeeJpaSpecificationExecutor)ctx.getBean("employeeJpaSpecificationExecutor");
System.out.println("setup");
}
@After
public void tearDown(){
ctx = null;
System.out.println("tearDown");
}
/**
* 1、分页
* 2、排序
* 3、条件查询:age<40
*/
@Test
public void testFind(){
Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");
Sort sort = new Sort(order);
/**
* root:需要查询的类型
* query:添加查询条件
* cb:构建Predicate
*/
Specification<Employee> specification = new Specification<Employee>() {
@Override
public Predicate toPredicate(Root<Employee> root,
CriteriaQuery<?> query,
CriteriaBuilder cb) {
Path id = root.get("age");
return cb.lt(id,50);
}
};
Pageable pageable = new PageRequest(0,5,sort);
Page<Employee> page = employeeJpaSpecificationExecutor.findAll(specification,pageable);
System.out.println("总数"+page.getTotalElements());
System.out.println("总页数"+page.getTotalPages());
System.out.println("当前页面集合"+page.getContent().toString());
System.out.println("当前第几页"+page.getNumber());
System.out.println("当前页面的记录数"+page.getNumberOfElements());
}
}