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、四篇的最終源代碼:戳這裏

相關文章
相關標籤/搜索