Spring Data 学习日记(三)Repository 子接口与自定义查询

先看如下一张图

Spring Data 学习日记(三)Repository 子接口与自定义查询_第1张图片
image.png
  • 可以看出 Repository 有三个子接口:
  CrudRepository,
  PagingAndSortingRepository,
  JpaRepository
  1. CrudRepository
package org.springframework.data.repository;

import java.io.Serializable;

@NoRepositoryBean
public interface CrudRepository extends Repository {
     S save(S var1);

     Iterable save(Iterable var1);

    T findOne(ID var1);

    boolean exists(ID var1);

    Iterable findAll();

    Iterable findAll(Iterable var1);

    long count();

    void delete(ID var1);

    void delete(T var1);

    void delete(Iterable var1);

    void deleteAll();
}
  1. PagingAndSortingRepository

package org.springframework.data.repository;

import java.io.Serializable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@NoRepositoryBean
public interface PagingAndSortingRepository extends CrudRepository {
    Iterable findAll(Sort var1);

    Page findAll(Pageable var1);
}

  1. JpaRepository
 package org.springframework.data.jpa.repository;

import java.io.Serializable;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

@NoRepositoryBean
public interface JpaRepository extends PagingAndSortingRepository {
    List findAll();

    List findAll(Sort var1);

    List findAll(Iterable var1);

     List save(Iterable var1);

    void flush();

     S saveAndFlush(S var1);

    void deleteInBatch(Iterable var1);

    void deleteAllInBatch();

    T getOne(ID var1);
}

由上面的源码可以看出三者有继承关系

CrudRepository
       |---PagingAndSortingRepository
                   |---JpaRepository

这里仅仅继承JpaRepository测试 代码如下

public interface EmployeeJpaReposity extends JpaRepository {
}
----------------------------------
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestJPARepositoty {
    @Autowired
    private EmployeeReposity employeeReposity;
    @Autowired
    private EmployeeJpaReposity employeeJpaReposity;
    @Test
    public void test() {
        List employeeList = employeeReposity.findAllBySal(3000.0);
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }
    @Test
    public void testJpaReposity() {
        System.out.println("所有员工信息");
        List employees = employeeJpaReposity.findAll();
        for (Employee e : employees) {
            System.out.println(e);
        }
        long count = employeeJpaReposity.count();
        System.out.println("员工总数-------------------");
        System.out.println("-----count---------->"+count);

        //测试按照工资降序
        System.out.println("按照工资降序排列---------------");
        Sort sort = new Sort(Sort.Direction.DESC,"sal");
        List all = employeeJpaReposity.findAll(sort);
        for (Employee e:all) {
            System.out.println(e);
        }
        System.out.println("分页测试---------------------");
        //查询第三页,每页四条记录
        Pageable pageable = new PageRequest(2,4);
        Page employeePage = employeeJpaReposity.findAll(pageable);
        for (Employee e:employeePage) {
            System.out.println(e);
        }
    }
}
--------------------------------
测试结果:

所有员工信息
Employee{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=1980-12-17, sal=800.0, comm=null, deptno=20}
Employee{empno=7499, ename='ALLEN', job='SALESMAN', mgr=7698, hiredate=1981-02-20, sal=1600.0, comm=300.0, deptno=30}
Employee{empno=7521, ename='WARD', job='SALESMAN', mgr=7698, hiredate=1981-02-22, sal=1250.0, comm=500.0, deptno=30}
Employee{empno=7566, ename='JONES', job='MANAGER', mgr=7839, hiredate=1981-04-02, sal=2975.0, comm=null, deptno=20}
Employee{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=1981-09-28, sal=1250.0, comm=1400.0, deptno=30}
Employee{empno=7698, ename='BLAKE', job='MANAGER', mgr=7839, hiredate=1981-05-01, sal=2850.0, comm=null, deptno=30}
Employee{empno=7782, ename='CLARK', job='MANAGER', mgr=7839, hiredate=1981-06-09, sal=2450.0, comm=null, deptno=10}
Employee{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=1987-04-19, sal=3000.0, comm=null, deptno=20}
Employee{empno=7839, ename='KING', job='PRESIDENT', mgr=null, hiredate=1981-11-17, sal=5000.0, comm=null, deptno=10}
Employee{empno=7844, ename='TURNER', job='SALESMAN', mgr=7698, hiredate=1981-09-08, sal=1500.0, comm=0.0, deptno=30}
Employee{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=1987-05-23, sal=1100.0, comm=null, deptno=20}
Employee{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=1981-12-03, sal=950.0, comm=null, deptno=30}
Employee{empno=7902, ename='FORD', job='ANALYST', mgr=7566, hiredate=1981-12-03, sal=3000.0, comm=null, deptno=20}
Employee{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=1982-01-23, sal=1300.0, comm=null, deptno=10}
员工总数-------------------
-----count---------->14
按照工资降序排列---------------
Employee{empno=7839, ename='KING', job='PRESIDENT', mgr=null, hiredate=1981-11-17, sal=5000.0, comm=null, deptno=10}
Employee{empno=7902, ename='FORD', job='ANALYST', mgr=7566, hiredate=1981-12-03, sal=3000.0, comm=null, deptno=20}
Employee{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=1987-04-19, sal=3000.0, comm=null, deptno=20}
Employee{empno=7566, ename='JONES', job='MANAGER', mgr=7839, hiredate=1981-04-02, sal=2975.0, comm=null, deptno=20}
Employee{empno=7698, ename='BLAKE', job='MANAGER', mgr=7839, hiredate=1981-05-01, sal=2850.0, comm=null, deptno=30}
Employee{empno=7782, ename='CLARK', job='MANAGER', mgr=7839, hiredate=1981-06-09, sal=2450.0, comm=null, deptno=10}
Employee{empno=7499, ename='ALLEN', job='SALESMAN', mgr=7698, hiredate=1981-02-20, sal=1600.0, comm=300.0, deptno=30}
Employee{empno=7844, ename='TURNER', job='SALESMAN', mgr=7698, hiredate=1981-09-08, sal=1500.0, comm=0.0, deptno=30}
Employee{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=1982-01-23, sal=1300.0, comm=null, deptno=10}
Employee{empno=7521, ename='WARD', job='SALESMAN', mgr=7698, hiredate=1981-02-22, sal=1250.0, comm=500.0, deptno=30}
Employee{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=1981-09-28, sal=1250.0, comm=1400.0, deptno=30}
Employee{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=1987-05-23, sal=1100.0, comm=null, deptno=20}
Employee{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=1981-12-03, sal=950.0, comm=null, deptno=30}
Employee{empno=7369, ename='SMITH', job='CLERK', mgr=7902, hiredate=1980-12-17, sal=800.0, comm=null, deptno=20}
分页测试---------------------
Employee{empno=7839, ename='KING', job='PRESIDENT', mgr=null, hiredate=1981-11-17, sal=5000.0, comm=null, deptno=10}
Employee{empno=7844, ename='TURNER', job='SALESMAN', mgr=7698, hiredate=1981-09-08, sal=1500.0, comm=0.0, deptno=30}
Employee{empno=7876, ename='ADAMS', job='CLERK', mgr=7788, hiredate=1987-05-23, sal=1100.0, comm=null, deptno=20}
Employee{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=1981-12-03, sal=950.0, comm=null, deptno=30}

条件查询

以上接口里有很多方法,但实际开发中可能有很复杂的条件查询;只需定义规范的名称接口就可以了;
规范如下:


Spring Data 学习日记(三)Repository 子接口与自定义查询_第2张图片
image.png

例如:

public interface EmployeeJpaReposity extends JpaRepository {

    // 查询某个部门工资大于sal 的员工信息
       ListfindByDeptnoAndSalGreaterThan(Integer deptno,Double sal);
    // 查询员工姓名含有AR字母的员工信息
        List findByEnameContaining(String ename);
}
--------------------------------------------------
测试代码:
    @Test
    public void testMyInterface(){
        //查询部门编号20 工资大于2000的 员工列表
        List list = employeeJpaReposity.findByDeptnoAndSalGreaterThan(20, 2000.0);
        System.out.println("查询部门编号20 工资大于2000的 员工列表");
        for ( Employee e:list) {
            System.out.println(e);
        }
        //查询员工姓名含有AR字母的列表
        List enameLike = employeeJpaReposity.findByEnameContaining("AR");
        System.out.println("查询员工姓名含有AR字母的列表");
        for (Employee e:enameLike) {
            System.out.println(e);
        }
    }
查询结果:
---------------------------------------------------
查询部门编号20 工资大于2000的 员工列表
Employee{empno=7566, ename='JONES', job='MANAGER', mgr=7839, hiredate=1981-04-02, sal=2975.0, comm=null, deptno=20}
Employee{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=1987-04-19, sal=3000.0, comm=null, deptno=20}
Employee{empno=7902, ename='FORD', job='ANALYST', mgr=7566, hiredate=1981-12-03, sal=3000.0, comm=null, deptno=20}
查询员工姓名含有a字母的列表
Employee{empno=7521, ename='WARD', job='SALESMAN', mgr=7698, hiredate=1981-02-22, sal=1250.0, comm=500.0, deptno=30}
Employee{empno=7654, ename='MARTIN', job='SALESMAN', mgr=7698, hiredate=1981-09-28, sal=1250.0, comm=1400.0, deptno=30}
Employee{empno=7782, ename='CLARK', job='MANAGER', mgr=7839, hiredate=1981-06-09, sal=2450.0, comm=null, deptno=10}

使用Query注解自定义接口查询

入职时间为date的员工列表
 nativeQuery = true 表示支持原生SQL 查询
@Query(nativeQuery = true,value = "select * from emp  e where e.hiredate=to_date(:date,'yyyymmdd')")
public  List getEmployeeByHiredate(@Param("date") String date);
@Test
public void testGetEmployeeByHiredate() throws ParseException {
     //查询1981-12-03入职的员工
    List employeeByHiredate = employeeJpaReposity.getEmployeeByHiredate("19811203");
    for (Employee e : employeeByHiredate) {
        System.out.println(e);
    }
}

结果如下:

Employee{empno=7900, ename='JAMES', job='CLERK', mgr=7698, hiredate=1981-12-03, sal=950.0, comm=null, deptno=30}
Employee{empno=7902, ename='FORD', job='ANALYST', mgr=7566, hiredate=1981-12-03, sal=3000.0, comm=null, deptno=20}

你可能感兴趣的:(Spring Data 学习日记(三)Repository 子接口与自定义查询)