虚拟视图
2018-10-28 13:09 更新
多表关联常常通过创建数据库视图的方式来解决,其实也可以使用虚拟视图,维护更为方便。
表person和表id_card通过外键id_card_id关联。
创建虚拟视图PersonIdCard类:
数据库访问PersonIdCardRepository类:@Entity @Immutable @Subselect("SELECT p.id id, p.name name, p.age age, ic.number number " + "FROM person p " + "LEFT JOIN id_card ic " + "ON p.id_card_id=ic.id") public class PersonIdCard { @Id private Long id; private String name; private Integer age; private String number;
// getter and setter
}
public interface PersonIdCardRepository extends JpaRepository<PersonIdCard, Long>, JpaSpecificationExecutor<PersonIdCard> {
}
查询年龄大于20的人。public List<PersonIdCard> findAll(SearchRequest request) {
Specification<PersonIdCard> specification = new Specifications<PersonIdCard>()
.gt(Object.nonNull(request.getAge()), "age", 20)
.build();
return personRepository.findAll(specification);
}
以上内容是否对您有帮助:
更多建议: