SpringData系列三 Repository Bean 方法定义规范

时间:2022-04-06 22:08:27

    本节主要介绍Repository Bean中方法定义规范。

  1、方法不是随便声明,需要符合一定的规范。

  2、按照Spring Data的规范,查询方法以find|read|get开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性需要首字母大写

  3、Spring Data支持的关键字如下:

   SpringData系列三  Repository Bean 方法定义规范

   SpringData系列三  Repository Bean 方法定义规范

  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 + "]";
} }

Person.java

 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;
} }

Address.java

 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);
}

PersonRepository.java