本节主要介绍Repository Bean中方法定义规范。
1、方法不是随便声明,需要符合一定的规范。
2、按照Spring Data的规范,查询方法以find|read|get开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性需要首字母大写。
3、Spring Data支持的关键字如下:
4、支持属性的级联查询,若当前类有符合条件的属性,则优先使用,而不使用级联属性。若想使用级联属性,则属性之间用"_"连接。
1 package com.ntjr.springdata; 2 3 import java.util.Date; 4 5 import javax.persistence.CascadeType; 6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.GeneratedValue; 9 import javax.persistence.Id; 10 import javax.persistence.JoinColumn; 11 import javax.persistence.ManyToOne; 12 import javax.persistence.Table; 13 14 @Table(name = "JPA_PERSONS") 15 @Entity 16 public class Person { 17 18 private Integer id; 19 private String lastName; 20 private String email; 21 private Date birth; 22 private Address address; 23 private Integer addressId; 24 25 @GeneratedValue 26 @Id 27 public Integer getId() { 28 return id; 29 } 30 31 public void setId(Integer id) { 32 this.id = id; 33 } 34 35 public String getLastName() { 36 return lastName; 37 } 38 39 @Column(name = "LAST_NAME") 40 public void setLastName(String lastName) { 41 this.lastName = lastName; 42 } 43 44 public String getEmail() { 45 return email; 46 } 47 48 public void setEmail(String email) { 49 this.email = email; 50 } 51 52 public Date getBirth() { 53 return birth; 54 } 55 56 public void setBirth(Date birth) { 57 this.birth = birth; 58 } 59 60 @JoinColumn(name = "ADDRESS_ID") 61 @ManyToOne(cascade = { CascadeType.ALL }, targetEntity = Address.class) 62 public Address getAddress() { 63 return address; 64 } 65 66 public void setAddress(Address address) { 67 this.address = address; 68 } 69 70 71 public Integer getAddressId() { 72 return addressId; 73 } 74 75 public void setAddressId(Integer addressId) { 76 this.addressId = addressId; 77 } 78 79 @Override 80 public String toString() { 81 return "Person [id=" + id + ", lastName=" + lastName + ", email=" + email + ", birth=" + birth + "]"; 82 } 83 84 }
1 package com.ntjr.springdata; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 @Table(name="JPA_ADDRESSES") 9 @Entity 10 public class Address { 11 12 private Integer id; 13 private String province; 14 private String city; 15 16 @GeneratedValue 17 @Id 18 public Integer getId() { 19 return id; 20 } 21 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 26 public String getProvince() { 27 return province; 28 } 29 30 public void setProvince(String province) { 31 this.province = province; 32 } 33 34 public String getCity() { 35 return city; 36 } 37 38 public void setCity(String city) { 39 this.city = city; 40 } 41 42 }
1 package com.ntjr.springdata; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import org.springframework.data.repository.RepositoryDefinition; 7 8 /** 9 * 10 * 1、实现Repository接口 2、通过注解的方式@RepositoryDefinition将一个bean定义为Repository接口 11 */ 12 @RepositoryDefinition(idClass = Integer.class, domainClass = Person.class) 13 public interface PersonRepsitory { 14 // 根据lastName获取对应的person 15 Person getByLastName(String lastName); 16 17 // WHERE lastName like ?% AND id<? 18 List<Person> findByLastNameStartingWithAndIdLessThan(String lastName, Integer id); 19 20 // WHERE lastName Like %? And Id <? 21 List<Person> findByLastNameEndingWithAndIdLessThan(String lastName, Integer id); 22 23 // WHERE email IN(?,?,?) Or birth <? 24 List<Person> findByEmailInOrBirthLessThan(List<String> emails, Date birth); 25 26 // WHERE a.id >? 27 /** 28 * AddressId 优先查找Person类中的AddressId属性,如果Person类中没有,就查找Address类中的Id属性 29 * 这样容易出现问题,建议级联查询中,属性之间使用"_"连接 30 * 31 */ 32 List<Person> findByAddressIdGreaterThan(Integer id); 33 34 // 这样就很明确是进行级联查询,Address类中的ID属性 35 List<Person> findByAddress_IdGreaterThan(Integer id); 36 }