SpringData Repository Bean方法定義規範程式碼例項
阿新 • • 發佈:2020-08-27
本節主要介紹Repository Bean中方法定義規範。
1、方法不是隨便宣告,需要符合一定的規範。
2、按照Spring Data的規範,查詢方法以find|read|get開頭,涉及條件查詢時,條件的屬性用條件關鍵字連線,要注意的是:條件屬性需要首字母大寫。
3、Spring Data支援的關鍵字如下:
4、支援屬性的級聯查詢,若當前類有符合條件的屬性,則優先使用,而不使用級聯屬性。若想使用級聯屬性,則屬性之間用"_"連線。
package com.ntjr.springdata; import java.util.Date; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Table(name = "JPA_PERSONS") @Entity public class Person { private Integer id; private String lastName; private String email; private Date birth; private Address address; private Integer addressId; @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } @Column(name = "LAST_NAME") public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @JoinColumn(name = "ADDRESS_ID") @ManyToOne(cascade = { CascadeType.ALL },targetEntity = Address.class) public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Integer getAddressId() { return addressId; } public void setAddressId(Integer addressId) { this.addressId = addressId; } @Override public String toString() { return "Person [id=" + id + ",lastName=" + lastName + ",email=" + email + ",birth=" + birth + "]"; } }
package com.ntjr.springdata; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Table(name="JPA_ADDRESSES") @Entity public class Address { private Integer id; private String province; private String city; @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
package com.ntjr.springdata; import java.util.Date; import java.util.List; import org.springframework.data.repository.RepositoryDefinition; /** * * 1、實現Repository介面 2、通過註解的方式@RepositoryDefinition將一個bean定義為Repository介面 */ @RepositoryDefinition(idClass = Integer.class,domainClass = Person.class) public interface PersonRepsitory { // 根據lastName獲取對應的person Person getByLastName(String lastName); // WHERE lastName like ?% AND id<? List<Person> findByLastNameStartingWithAndIdLessThan(String lastName,Integer id); // WHERE lastName Like %? And Id <? List<Person> findByLastNameEndingWithAndIdLessThan(String lastName,Integer id); // WHERE email IN(?,?,?) Or birth <? List<Person> findByEmailInOrBirthLessThan(List<String> emails,Date birth); // WHERE a.id >? /** * AddressId 優先查詢Person類中的AddressId屬性,如果Person類中沒有,就查詢Address類中的Id屬性 * 這樣容易出現問題,建議級聯查詢中,屬性之間使用"_"連線 * */ List<Person> findByAddressIdGreaterThan(Integer id); // 這樣就很明確是進行級聯查詢,Address類中的ID屬性 List<Person> findByAddress_IdGreaterThan(Integer id); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。