SpringDataJPA
Repository 的子接口
基础的 Repository 提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下:
Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类
- CrudRepository: 继承 Repository,实现了一组 CRUD 相关的方法
- PagingAndSortingRepository: 继承 CrudRepository,实现了一组分页排序相关的方法
- JpaRepository: 继承 PagingAndSortingRepository,实现一组 JPA 规范相关的方法
- 自定义的 XxxxRepository 需要继承 JpaRepository,这样的 XxxxRepository 接口就具备了通用的数据访问控制层的能力。
JpaSpecificationExecutor: 不属于Repository体系,实现一组 JPA Criteria 查询相关的方法
public List<Person> getByAddressIdGreaterThan(Integer addressId); |
select person0_.id as id1_1_, person0_.address_id as address_5_1_, person0_.birth as birth2_1_, person0_.email as email3_1_, person0_.last_name as last_nam4_1_ from tx_persons person0_ left outer join address address1_ on person0_.address_id=address1_.id where address1_.id>? |
——————————————————————
Person类
public class Person { private Integer id; private String lastName; private String email; private Date birth; private Address address; private Integer AddressId; ............. } |
PersonRepository类
public List<Person> getByAddressIdGreaterThan(Integer addressId); |
报错:
Caused by: org.hibernate.DuplicateMappingException: Table [tx_persons] contains phyical column name [address_id] represented by different logical column names: [addressId], [ADDRESS_ID] |
——————————————————————————————————————
Person类(给属性起别名)
public class Person { private Integer id; private String lastName; private String email; private Date birth; private Address address; private Integer AddressId; @Column(name = "add_id") public Integer getAddressId() { return AddressId; } ............. } |
select person0_.id as id1_1_, person0_.address_id as address_6_1_, person0_.add_id as add_id2_1_, person0_.birth as birth3_1_, person0_.email as email4_1_, person0_.last_name as last_nam5_1_ from tx_persons person0_ where person0_.add_id>? |
结果:若当前实例类有符合条件的属性,则优先使用
————————————————————————————
PersonRepository类
public List<Person> getByAddress_IdGreaterThan(Integer addressId); |
select person0_.id as id1_1_, person0_.address_id as address_6_1_, person0_.add_id as add_id2_1_, person0_.birth as birth3_1_, person0_.email as email4_1_, person0_.last_name as last_nam5_1_ from tx_persons person0_ left outer join address address1_ on person0_.address_id=address1_.id where address1_.id>? |
若涉及到级联属性查询时,级联类和级联类属性以_分开。
完整demo
Person.java
package com.wuxinzhe.springdata; import java.util.Date; 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 = "TX_PERSONS") @Entity public class Person { private Integer id; private String lastName; private String email; private Date birth; private Address address; private Integer AddressId; @Column(name = "add_id") public Integer getAddressId() { return AddressId; } public void setAddressId(Integer addressId) { AddressId = addressId; } @JoinColumn(name = "ADDRESS_ID") @ManyToOne public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } 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; } @Override public String toString() { return "Person [id=" + id + ", lastName=" + lastName + ", email=" + email + ", birth=" + birth + ", address=" + address + "]"; } } |
PersonRepository类
package com.wuxinzhe.springdata; import java.util.List; import org.springframework.data.repository.Repository; import org.springframework.data.repository.RepositoryDefinition; /** * 1. Repository是一个空接口,即是一个标记接口 * 2. 若我们定义个接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean,纳入到IOC容器中,进而可以在该接口中定义满足一定规范的方法 * 3. 实际上也可以通过一个注解@RepositoryDefinition来替代继承Repository接口 * 4. 支持属性的级联查询,若当前实例类有符合条件的属性,则优先使用 * 5. 若涉及到级联属性查询时,级联类和级联类属性以_分开。 */ @RepositoryDefinition(domainClass=Person.class,idClass=Integer.class) public interface PersonRepository{ //public interface PersonRepository extends Repository<Person, Integer>{ public Person getByLastName(String lastName); // 获取id >= ? public List<Person> getByIdGreaterThanEqual(Integer id); // 获取id > ? public List<Person> getByIdGreaterThan(Integer id); // IN ? AND id < ? public List<Person> getByEmailInAndIdLessThan(List<String> emails,Integer id); public List<Person> getByAddress_IdGreaterThan(Integer addressId); } |
CrudRepository
JPARepository