欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringData入门(四)-Spring data JPA高级

程序员文章站 2024-03-12 22:07:50
...
  • 今天要讲的这几个接口都是Repository接口的子接口
    SpringData入门(四)-Spring data JPA高级

一.CrudRepository接口使用详解

  • save(entity):保存一个实体

  • save(entities):保存多个实体

  • findOne(id):找到一个对象

  • exists(id):根据ID判断对象是否存在

  • findAll():找到所有实体对象

  • delete(id):根据ID删除实体对象

  • delete(entity):根据实体对象删除实体对象

  • delete(entities):删除多个实体对象

  • deleteAll():删除所有实体对象

  • 为了避免和前面章节的表起冲突,我们换一个表名
    SpringData入门(四)-Spring data JPA高级

  • 继承CrudRepository接口

public interface EmployeeCrudRepository extends CrudRepository<Employee,Integer> {


}
  • 测试类
public class EmployeeCrudReositoryTest {


    private ApplicationContext ctx = null;

    private EmployeeCrudRepository employeeCrudRepository;
    @Before
    public void init(){
         ctx = new AnnotationConfigApplicationContext(SpringConfigNew.class);
         employeeCrudRepository = ctx.getBean(EmployeeCrudRepository.class);
    }

    @After
    public void destroy(){
         ctx = null;
    }

    @Test
    public void saveTest(){
        List<Employee> employees = new ArrayList<>();
        for(int i=0;i<100;i++){

            Employee employee = new Employee();

            employee.setName("test"+i);
            employee.setAge(100-i);

            employees.add(employee);
        }
        employeeCrudRepository.saveAll(employees);

    }
}

  • 存入100个数据
    SpringData入门(四)-Spring data JPA高级

二.PagingAndSortingRespository接口使用详解

  • 该接口包含分页和排序的功能
  • 带排序的查询:findAll(Sort sort)
  • 带排序的分页查询:findAll(Pageable pageable)
  • 继承PagingAndSortingRespository接口
public interface EmployeePagingAndSortingRepository extends PagingAndSortingRepository<Employee,Integer> {

}
  • 测试类
public class EmployeePagingAndSortingRepositoryTest {

    private ApplicationContext ctx = null;

    private EmployeePagingAndSortingRepository employeePagingAndSortingRepository;
    @Before
    public void init(){
        ctx = new AnnotationConfigApplicationContext(SpringConfigNew.class);
        employeePagingAndSortingRepository = ctx.getBean( EmployeePagingAndSortingRepository.class);
    }

    @After
    public void destroy(){
        ctx = null;
    }

    //分页测试
    @Test
    public void PageTest(){

        //Pageable pageable = new PageRequest(0,9); 方法过期,现在直接用静态方法了
        //index是从0开始的,不是从1开始的
        Pageable pageable = PageRequest.of(1,9);//page是从0开始的 一页9个记录
        Page<Employee> employeePage = employeePagingAndSortingRepository.findAll(pageable);

        System.out.println("查询的总页数:"+employeePage.getTotalPages());
        System.out.println("查询的总记录数:"+employeePage.getTotalElements());
        System.out.println("查询的当前第几页:"+employeePage.getNumber()+1);
        System.out.println("查询的当前页面的集合:"+employeePage.getContent());
        System.out.println("查询的当前页面的记录数:"+employeePage.getNumberOfElements());


    }

    //排序和分页测试
    @Test
    public void PageAndSort(){
        Sort.Order order = new Sort.Order(Sort.Direction.ASC,"id");//以id升序

        //Sort sort = new Sort(order); 过期了 也是静态方法
        Sort sort = Sort.by(order);

        //Pageable pageable = new PageRequest(0,9, sort); 过期了 也是静态方法
        Pageable pageable = PageRequest.of(0,9,sort);

        Page<Employee> employeePage = employeePagingAndSortingRepository.findAll(pageable);

        System.out.println("查询的总页数:"+employeePage.getTotalPages());
        System.out.println("查询的总记录数:"+employeePage.getTotalElements());
        System.out.println("查询的当前第几页:"+employeePage.getNumber()+1);
        System.out.println("查询的当前页面的集合:"+employeePage.getContent());
        System.out.println("查询的当前页面的记录数:"+employeePage.getNumberOfElements());
    }
}

三.JpaRepository接口使用详解

  • finaAll:查询所有记录
  • findAll(Sort sort):查询所有记录并排序
  • save(entities):保存多个实体对象
  • flush:
  • deleteInBatch(entities):一个批次里面删除那些实体
  • 继承JpaRepository接口
public interface EmployeeJpaRepository extends JpaRepository<Employee,Integer> {
}
  • 测试类
public class EmployeeJpaRepositoryTest {

    private ApplicationContext ctx = null;

    private EmployeeJpaRepository employeeJpaRepository;
    @Before
    public void init(){
        ctx = new AnnotationConfigApplicationContext(SpringConfigNew.class);
        employeeJpaRepository = ctx.getBean(EmployeeJpaRepository.class);
    }

    @After
    public void destroy(){
        ctx = null;
    }

    @Test
    public void findTest(){
        Employee employee = employeeJpaRepository.findById(99).get();
        System.out.println("employee:"+ employee.toString());

        System.out.println("employee(88)"+employeeJpaRepository.existsById(88));
        System.out.println("employee(101)"+employeeJpaRepository.existsById(101));

    }
}

四.JpaSpecificationExecutor接口使用详解

  • 这个不属于之前介绍的结构体系 是单独的,Specification封装了JPA Criteria查询条件
  • 继承JpaSpecificationExecutor接口
//接口可以多继承 多继承 功能更加强大
public interface EmployeeJpaSpecificationExecutor extends  JpaRepository<Employee,Integer>,JpaSpecificationExecutor<Employee> {
}

  • 测试类
public class EmployeeJpaSpecificationExecutorTest {
    private ApplicationContext ctx = null;

    private EmployeeJpaSpecificationExecutor employeeJpaSpecificationExecutor = null;
    @Before
    public void init(){
        ctx = new AnnotationConfigApplicationContext(SpringConfigNew.class);
        employeeJpaSpecificationExecutor = ctx.getBean(EmployeeJpaSpecificationExecutor.class);
    }

    @After
    public void destroy(){
        ctx = null;
    }

    /*
    1.分页
    2.排序
    3.查询条件:age > 50
     */
    @Test
    public void queryTest(){

        Sort.Order order = new Sort.Order(Sort.Direction.ASC,"id");

        Sort sort = Sort.by(order);

        Pageable pageable = PageRequest.of(0,9,sort);

        /*
        root:查询的类型
        query : 添加查询条件
        cb : 构建 Predicate
         */
        Specification<Employee> specification = new Specification<Employee>() {
            //查询条件
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //root(employee(age))
                Path path = root.get("age");
                return cb.gt(path,50);
            }
        };

        Page<Employee> employeePage = employeeJpaSpecificationExecutor.findAll(specification,pageable);

        System.out.println("查询的总页数:"+employeePage.getTotalPages());
        System.out.println("查询的总记录数:"+employeePage.getTotalElements());
        System.out.println("查询的当前第几页:"+employeePage.getNumber()+1);
        System.out.println("查询的当前页面的集合:"+employeePage.getContent());
        System.out.println("查询的当前页面的记录数:"+employeePage.getNumberOfElements());

    }

}
相关标签: Spring Data JPA