洞察如何选择适合你的企业的小程序开源框架来实现高效开发与管理
522
2023-04-24
SpringData Repository Bean方法定义规范代码实例
本节主要介绍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 adhttp://dressId;
}
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、实http://现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
// WHERE lastName Like %? And Id
List
// WHERE email IN(?,?,?) Or birth
List
// WHERE a.id >?
/**
* AddressId 优先查找Person类中的AddressId属性,如果Person类中没有,就查找Address类中的Id属性
* 这样容易出现问题,建议级联查询中,属性之间使用"_"连接
*
*/
List
// 这样就很明确是进行级联查询,Address类中的ID属性
List
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~