按照Spring Data JPA 定義的規則,查詢方法以findBy開頭,涉及條件查詢時,條件的屬性用條件關鍵字連接,要注意的是:條件屬性首字母需大寫。框架在進行方法名解析時,會先把方法名多余的前綴截取掉,然后對剩下部分進行解析。
|
|
|
|
| Keyword |
Sample |
JPQL |
| And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
| Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
| Is,Equals |
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) |
package com.ytkj.entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* @Entity
* 作用:指定當前類是實體類。
* @Table
* 作用:指定實體類和表之間的對應關系。
* 屬性:
* name:指定數據庫表的名稱
* @Id
* 作用:指定當前字段是主鍵。
* @GeneratedValue
* 作用:指定主鍵的生成方式。。
* 屬性:
* strategy :指定主鍵生成策略。
* @Column
* 作用:指定實體類屬性和數據庫表之間的對應關系
* 屬性:
* name:指定數據庫表的列名稱。
* unique:是否唯一
* nullable:是否可以為空
* inserttable:是否可以插入
* updateable:是否可以更新
* columnDefinition: 定義建表時創建此列的DDL
* secondaryTable: 從表名。如果此列不建在主表上(默認建在主表),該屬性定義該列所在從表的名字搭建開發環境[重點]
*
* 客戶實體類
* 配置映射關系
* 實體類和表映射
* 實體類屬性和表字段映射
*/
@Entity
@Table(name = "cst_customer")
public class Customer implements Serializable {
/**
* 聲明主鍵配置
*/
@Id
/**
* 配置主鍵的生成策略
*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
/**
* 指定實體類屬性和數據庫表之間的對應關系
*/
@Column(name ="cust_id")
private Long custId;//客戶主鍵
@Column(name = "cust_name")
private String custName;//客戶名稱
@Column(name ="cust_source" )
private String custSource;//客戶來源
@Column(name = "cust_industry")
private String custIndustry;//客戶行業
@Column(name ="cust_level")
private String custLevel;//客戶級別
@Column(name ="cust_address")
private String custAddress;//客戶地址
@Column(name = "cust_phone")
private String custPhone;//客戶電話
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custAddress='" + custAddress + '\'' +
", custPhone='" + custPhone + '\'' +
'}';
}
}
package com.ytkj.dao;
import com.ytkj.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
/**
* JpaRepository<實體類類型,主鍵類型>:用來完成基本CRUD操作
* JpaSpecificationExecutor<實體類類型>:用於復雜查詢(分頁等查詢操作)
*/
public interface CustomerDao3 extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
/**
* 按照Spring Data JPA 定義的規則,查詢方法以findBy開頭,涉及條件查詢時,條件的屬性用條件關鍵字連接,
* 要注意的是:條件屬性首字母需大寫。框架在進行方法名解析時,會先把方法名多余的前綴截取掉,然后對剩下部分進行解析。
* 方法命名規則查詢
* 方法名的約定
* findBy:查詢
* 對象中的屬性名(首字母大寫):查詢的條件
*
*
*/
public Customer findByCustName(String name);
public List<Customer> findByCustNameLike(String name);
}
import com.ytkj.dao.CustomerDao3; import com.ytkj.entity.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)//聲明spring提供的單元測試環境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 public class SpringdatajpaMethodTest { @Autowired CustomerDao3 customerDao3; @Test public void findByName(){ Customer customer = customerDao3.findByCustName("哈哈哈"); System.out.println(customer); } @Test public void findByName2(){ List<Customer> list = customerDao3.findByCustNameLike("zhe"); for (int i=0;i<list.size();i++){ Customer customer = list.get(i); System.out.println(customer); } } }
