步驟 1 : 可運行項目
首先下載一個簡單的可運行項目作為演示:網盤鏈接:https://www.90pan.com/b1869097
下載后解壓,比如解壓到 E:\project\springboot 目錄下
步驟 2 : JPA 條件查詢方式
JPA 條件查詢方式很有意思,是不需要寫 SQL 語句的,只需要在 dao 接口里按照規范的命名定義對應的方法名,即可達到查詢相應字段的效果了。
在如下代碼里做了如下事情:
- test1() 查詢所有數據
- test2() 通過自定義的接口方法 findByName,根據 name 查詢分類表
- test3() 通過自定義的接口方法 findByNameLikeAndIdGreaterThanOrderByNameAsc,根據名稱模糊查詢,id 大於某值, 並且名稱正排序查詢。
TestJPA.java 類:
package com.ryan.springboot.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.ryan.springboot.Application;
import com.ryan.springboot.dao.CategoryDAO;
import com.ryan.springboot.pojo.Category;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestJPA {
@Autowired CategoryDAO dao;
@Test
public void test1() {
List<Category> cs= dao.findAll();
System.out.println("所有的分類信息:");
for (Category c : cs) {
System.out.println(c.getName());
}
System.out.println();
}
@Test
public void test2() {
System.out.println("查詢名稱是 \" 瘋人院\" 的信息:");
List<Category> cs= dao.findByName("瘋人院");
for (Category c : cs) {
System.out.println("c.getName():"+ c.getName());
}
System.out.println();
}
@Test
public void test3() {
System.out.println("根據名稱模糊查詢,id 大於5, 並且名稱正排序查詢");
List<Category> cs= dao.findByNameLikeAndIdGreaterThanOrderByNameAsc("%人%",5);
for (Category c : cs) {
System.out.println(c);
}
System.out.println();
}
}
CategoryDAO.java 類:
package com.ryan.springboot.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ryan.springboot.pojo.Category;
public interface CategoryDAO extends JpaRepository<Category,Integer>{
public List<Category> findByName(String name);
public List<Category> findByNameLikeAndIdGreaterThanOrderByNameAsc(String name, int id);
}
Category.java 類:
package com.ryan.springboot.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "category_")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
步驟 3 : 實現原理
雖然 JPA 沒有自己手動寫 sql 語句,但是通過反射獲取自定義的接口方法里提供的信息,就知道用戶希望根據什么條件來查詢了。 然后 JPA 底層再偷偷摸摸地拼裝對應的 sql 語句,丟給數據庫,就達到了條件查詢的效果啦。
對反射不熟悉的同學,可了解反射基礎教程: 反射基礎教程
步驟 4 : 條件查詢規范
上面只是個別舉例,下表把 jpa 做的各種查詢規范都列出來了。 如果要做其他相關查詢,按照表格中的規范設計接口方法即可。
關鍵詞 | 舉例 | 生成的JPQL 語句片段 |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname findByFirstnameIs findByFirstnameEquals |
… where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age ⇐ ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection age) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
步驟 5 : 測試
運行 TestJPA 類就可以看到如圖所示的效果了
更多關於 Springboot_JPA_條件查詢 詳細內容,點擊學習: http://t.cn/A62YcgRW