SpringData入门笔记(四) - 花式查询

Repository接口

接口内容

  • Repository接口是Spring Data的核心接口,是一个空接口(没有任何方法)
  • public interface Repository<T, ID entends Serializable>{} ,该接口中T表示一个实体类,ID表示该实体类中的主键类型
  • 也能够使用@RepositoryDefinition注解使用

接口做用

该接口起到标记做用,继承该接口或者使用@RepositoryDefinition注解的类,会被Spring容器管理。java

接口使用

入门笔记(三)中的employeesql

实现 Repository 接口数据库

public interface EmployeeRepository extends Repository<Employee, Integer>

使用 @RepositoryDefinition 注解dom

@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)

Repository接口的子接口

  • CrudRepository:继承Repository,实现了CRUD(增删查改)相关方法
  • PagingAndSortingRepository:继承了CrudRepository,实现了分页排序相关方法
  • JpaRepository:继承PagingAndSortingRepository,实现JPA规范相关的方法

Repository中查询方法定义规则和使用

方法规则

输入图片说明

输入图片说明

怎么看懂这张表格?

?i 表示第i个参数(变量)函数

如想查询
lastname=?1 且 firstname = ?2 (JPQL snippet列),.net

则命名规则如
findByLastnameAndFirstname(Sample列)code

方法使用

你至少有入门笔记(三)中的环境和数据库
并执行以下sql语句对象

INSERT INTO employee (NAME, age) VALUES ('test1', 20);
INSERT INTO employee (NAME, age) VALUES ('test2', 21);
INSERT INTO employee (NAME, age) VALUES ('test3', 22);
INSERT INTO employee (NAME, age) VALUES ('test4', 20);
INSERT INTO employee (NAME, age) VALUES ('test5', 21);
INSERT INTO employee (NAME, age) VALUES ('test6', 22);
INSERT INTO employee (NAME, age) VALUES ('test16', 22);
/**
     * name 以 参数1 开始 且 age 小于 参数2
     * @param name
     * @param age
     * @return
     */
    public List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age);


    /**
     * name 在 参数1 列表中 或 age 小于 参数2
     * @param names
     * @param age
     * @return
     */
    public List<Employee> findByNameInOrAgeLessThan(List<String> names, Integer age);

Query注解

这块我也不知道怎么描述了,直接上代码,相信你能看懂blog

/**
     * 查询id最大的Employee
     * @return
     */
    @Query("select e from Employee e where id = (select max(id) from Employee t1)")
    public Employee getEmployeeMaxId();

    /**
     * 查询 name 等于 参数1 且 age 等于 参数2
     * @param name
     * @param age
     * @return
     */
    @Query("select o from Employee o where o.name = ?1 and o.age = ?2")
    public List<Employee> getEmployeeByNameAndAge(String name, Integer age);

    /**
     * 查询 name 等于 名为 name 的参数 且 age 等于 名为 age 的参数 (第二种方式)
     * @param name
     * @param age
     * @return
     */
    @Query("select o from Employee o where o.name = :name and o.age = :age")
    public List<Employee> getEmployeeByNameAndAge2(@Param("name") String name, @Param("age") Integer age);

    /**
     * 查询 name 中包含 参数1 的Employee
     * @param name
     * @return
     */
    @Query("select o from Employee o where o.name like %?1%")
    public List<Employee> getEmployeeByNameLike(String name);

注:排序

  1. from 后紧跟对象,不是数据库中的表名
  2. Query注解有两种形式,一种用?i表示第i个参数,一种用:paramName表示参数名称。在使用后者时,函数的参数前必须有@Param("paramName")注解

固然,Query注解也支持使用sql语言查询,示例以下:

@Query(nativeQuery = true, value = "select count(1) from employee")
    public Integer getEmployeeCount();

注:

  1. 将注解的nativeQuery参数设置为true
  2. 此时的from后紧跟的是表名

笔记3、四篇的最终源代码:戳这里

相关文章
相关标签/搜索