SpringData 学习—— 使用 @Query 注解实现查询

本文介绍如何利用Spring Data JPA中的@Query注解实现多种查询方式,包括无参数查询、按参数名查询、使用LIKE模糊匹配以及执行原生SQL。通过示例代码演示了不同场景下@Query注解的应用。

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

为了节约时间使得各位看官看起来更加简单舒适,这一节把测试方法和测试代码放在一起。

测试方法:

// ------------------------------------ 使用 @Query 注解
// 没有参数的查询
@Query("select p from Person p where p.id = (select max(p2.id) from Person p2)")
Person getMaxIdPerson();

/**
 * 参数名称和参数顺序耦合
 * @param lastName
 * @param email
 * @return
 */
@Query("select p from Person p where lastName=?1 and email=?2")
Person readPersonByLastNameAndEmail(String lastName,String email);

@Query("select p from Person p where email=:email and  lastName=:name")
Person readPersonByLastNameAndEmailThroughName(@Param("name") String lastName,@Param("email") String email);

// 使用 like
@Query("select p from Person p where lastName like ?1")
Person readPersonByLike(String likeName);

// @Query 注解支持使用百分号
@Query("select p from Person p where lastName like %?1%")
Person readPersonByLike2(String likeName);

// @Query 注解支持使用百分号
@Query("select p from Person p where lastName like %:lastName%")
Person readPersonByLike3(@Param("lastName")String name);

// 使用原生的 SQL
@Query(value="select * from jpa_person p1 where p1.last_name like %:lastName%",nativeQuery=true)
Person getPersonUsingOriginSQL(@Param("lastName")String lastName);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

测试代码:

// 以下测试 @Query 注解
@Test
public void testQueryAnnotationWithoutParam(){
    Person person = personRepository.getMaxIdPerson();
    System.out.println(person);
}

@Test
public void testQueryAnnotationWithParam(){
    Person person = personRepository.readPersonByLastNameAndEmail("liwei","liwei@sina.com");
    System.out.println(person);
}

@Test
public void testQueryAnnotationWithParamThroughName(){
    Person person = personRepository.readPersonByLastNameAndEmailThroughName("zhouguang","zhouguang@163.com");
    System.out.println(person);
}

@Test
public void testQueryAnnotationWithParamThroughLike(){
    Person person = personRepository.readPersonByLike("%zhou%");
    System.out.println(person);
}

@Test
public void testQueryAnnotationWithParamThroughLike2(){
    Person person = personRepository.readPersonByLike2("hu");
    System.out.println(person);
}

@Test
public void testQueryAnnotationWithParamThroughLike3(){
    Person person = personRepository.readPersonByLike3("wei");
    System.out.println(person);
}

@Test
public void testQueryAnnotationWithParamThroughLike4(){
    Person person = personRepository.getPersonUsingOriginSQL("wei");
    System.out.println(person);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

注意到:如果我们使用原生的 SQL,控制台打印的语句也会是原生 SQL 的,例如上面我们最后一个测试方法控制台打印:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值