JPA学习心得二(查询方法)

本文详细介绍了使用Java持久化API(JPA)进行数据库查询的四个核心步骤:声明Repository接口、定义查询方法、配置Spring代理及使用存储库实例。通过具体代码示例,帮助读者快速掌握JPA查询的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JPA查询分为四个步骤

1、声明扩展Repository或其子接口之一的接口,并将其键入到它将处理的域类和ID类型。

interface PersonRepository extends Repository<Person, Long> { … }

2、在接口上声明查询方法。

interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByLastname(String lastname);
}

 3、设置Spring以为这些接口创建代理实例。通过JavaConfig

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
class Config {}

4、获取注入的存储库实例并使用它。

public class SomeClient {

  @Autowired
  private PersonRepository repository;

  public void doSomething() {
    List<Person> persons = repository.findByLastname("Matthews");
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值