SpringData @Query和@Modifying注解原理解析

@Query注解查询适用于所查询的数据无法通过关键字查询得到结果的查询。这种查询可以摆脱像关键字查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring Data的特有实现。

索引参数与命名参数

    1、索引参数如下所示,索引值从1开始,查询中"?X"个数需要与方法定义的参数个数相一致,并且顺序也要一致。     

@Query("SELECT p FROM Person p WHERE p.lastName = ?1 AND p.email = ?2")
 List<Person> testQueryAnnotationParams1(String lastName, String email);

    注释:上面代码中的?1,?2表示参数的占位符,需要和方法中所传递的参数顺序一致。X是从1开始。

    2、命名参数(推荐使用此方式):可以定义好参数名,赋值时使用@Param("参数名"),而不用管顺序。

// 为@Query注解传递参数的方式1:命名参数
 @Query("SELECT p FROM Person p WHERE p.lastName = :lastName AND p.email = :email")
 List<Person> testQueryAnnotationParams2(@Param("email") String email, @Param("lastName") String lastName);

    注释:上面代码中:lastName ,:email 表示为参数命名,方法中所传递的参数使用@Param注解标识命名参数。这种方式不用管参数的顺序。

    3、含有LIKE关键字的查询

方式1:可以在占位符上添加"%",这样在查询方法中就不用添加"%"

// like查询 Spring Date 允许在占位符上添加%
 @Query("SELECT p FROM Person p WHERE p.lastName LIKE %?1% OR p.email LIKE %?2%")
 List<Person> testQueryAnnotationLikeParam(String lastName, String email);

@Test
  public void testAnnoationParams3() {
    List<Person> persons = personRepsitory.testQueryAnnotationLikeParam("A", "A@126.com");
    System.out.println(persons);
  }

方式2:不在占位符上添加"%",这样就必须在查询方法的参数上添加"%"

// like查询
@Query("SELECT p FROM Person p WHERE p.lastName LIKE ?1 OR p.email LIKE ?2")
List<Person> testQueryAnnotationLikeParam2(String lastName, String email);

@Test
  public void testAnnoationParams4() {
    List<Person> persons = personRepsitory.testQueryAnnotationLikeParam2("%A%", "%A@126.com%");
    System.out.println(persons);
  }

方式3:在命名参数上添加"%"

// like查询 使用命名参数
@Query("SELECT p FROM Person p WHERE p.lastName LIKE %:lastName% OR p.email LIKE %:email%")
List<Person> testQueryAnnotationLikeParam3(@Param("email") String email, @Param("lastName") String lastName);

4、使用原生SQL进行查询

**
   * 设置nativeQuery=true 即可以使用原生的SQL进行查询
   * @return
   */
  @Query(value = "SELECT count(id) FROM jpa_persons", nativeQuery = true)
  long getTotalCount();

注释:当设置nativeQuery=true即可以使用原生SQL进行查询

@Modifying注解

1、在@Query注解中编写JPQL实现DELETE和UPDATE操作的时候必须加上@modifying注解,以通知Spring Data 这是一个DELETE或UPDATE操作。

2、UPDATE或者DELETE操作需要使用事务,此时需要 定义Service层,在Service层的方法上添加事务操作。

3、注意JPQL不支持INSERT操作。  

@Transactional
  @Modifying
  @Query("UPDATE Person p SET p.email = :email WHERE p.id = :id")
  void updatePersonEmail(@Param("id") Integer id, @Param("email") String email);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • springboot使用@data注解减少不必要代码

    一.idea安装lombok插件 二.重启idea 三.添加maven依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> 四.书写一个实体类 import lombok.Data; @Data public cla

  • 可以修改脚本自身运行次数的vbs(Self modifying script)

    This script behaves like a virus This script shows you how a script can be self modifying, like viruses. It is a warning about the possibilities of macro viruses. File Name : selfmodify.vbs Requirement : none Author : Jean-Luc Antoine Submitted : 01/

  • 详解Spring Data JPA使用@Query注解(Using @Query)

    经过几天的折腾,终于到了学习一个重量级的查询方式上,使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQL的语言,略有区别,后者我们更熟悉一些.话不多说,看代码. 1.在CustomerRepository里添加 /** * 模糊匹配 * @param bauer * @return */ @Query("select c from Customer c where c.firstName=?1") Customer findByFirstName2

  • Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页示例

    本博客介绍基于Spring Data这款orm框架加上 Jquery.pagination插件实现的分页功能. 本博客是基于一款正在开发中的github开源项目的,项目代码地址:https://github.com/u014427391/jeeplatform 欢迎star(收藏)或者可以下载去学习,还在开发- 介绍一下Spring Data框架 spring Data : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储. 下面给出SpringData 项目所支

  • springData使用QueryDsl的示例代码

    经过多年,spring data jpa越来越完善,在版本迭代的过程中,会不断增加功能,今天看新的reference发现有Querydsl.然后搜索到上面的参考资料2 无论是JpaSpecificationExecutor,还是QueryDslPredicateExecutor,它俩都提供了使用Predicate(意义相同,都是构建where子句;类不同,javax.persistence.criteria.Predicate,com.querydsl.core.types.Predicate)

  • SpringData @Query和@Modifying注解原理解析

    @Query注解查询适用于所查询的数据无法通过关键字查询得到结果的查询.这种查询可以摆脱像关键字查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring Data的特有实现. 索引参数与命名参数 1.索引参数如下所示,索引值从1开始,查询中"?X"个数需要与方法定义的参数个数相一致,并且顺序也要一致. @Query("SELECT p FROM Person p WHERE p.lastName = ?1 AND p.email = ?2")

  • Spring @Conditional注解原理解析

    这篇文章主要介绍了Spring @Conditional注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @Conditional是Spring4新提供的注解,它的作用是根据某个条件加载特定的bean. 我们需要创建实现类来实现Condition接口,这是Condition的源码 public interface Condition { boolean matches(ConditionContext var1, AnnotatedT

  • spring @Component注解原理解析

    这篇文章主要介绍了spring @Component注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.@controller 控制器(注入服务) 2.@service 业务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>) 5.@Comp

  • springboot @ComponentScan注解原理解析

    这篇文章主要介绍了springboot @ComponentScan注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @ComponentScan 告诉Spring从哪里找到bean. 如果你的其他包都在@SpringBootApplication注解的启动类所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了. 如果你有一些bean所在的包,不在启动类的包及其下级包,那么你需要手动加上@Compone

  • Java8接口默认静态方法及重复注解原理解析

    接口默认方法和静态方法 默认方法 interface MyInterface1 { default String method1() { return "myInterface1 default method"; } } class MyClass{ public String method1() { return "myClass method"; } } /** * 父类和接口中都有相同的方法,默认使用父类的方法,即类优先 * @author 莫雨朵 * */

  • Spring @Primary和@Qualifier注解原理解析

    一 前言 本篇内容主要是讲解2个重要的注解使用方式和场景,@Primary,@Qualifier注解:其作用就是消除bean注入时的歧义,能够让spring容器知道加载哪个bean: 知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;) 二 实现方式 如下示例中使用被单接口Sheet, 实现类为SheetA , SHeetB ; 由于注入容器时都是 Sheet类型,会发生异常,此时就是使用@Pri

  • Spring AspectJ AOP框架注解原理解析

    什么是AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. AspectJ是一个基于Java语言的AOP框架 Spring2.0以后新增了对AspectJ切点表达式支持 @AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面 新版本Spring框架,建议使用AspectJ方式来开发AOP AspectJ表达式: 语法:exe

  • Spring注解@RestControllerAdvice原理解析

    这篇文章主要介绍了Spring注解@RestControllerAdvice原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 前言 前段时间部门搭建新系统,需要出异常后统一接口的返回格式,于是用到了Spring的注解@RestControllerAdvice.现在把此注解的用法总结一下. 用法 首先定义返回对象ResponseDto package com.staff.points.common; import lombok.Data;

  • SpringBoot http请求注解@RestController原理解析

    这篇文章主要介绍了SpringBoot http请求注解@RestController原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @RestController @RestController = @Controller + @ResponseBody组成,等号右边两位同志简单介绍两句,就明白我们@RestController的意义了: @Controller 将当前修饰的类注入SpringBoot IOC容器,使得从该类所在的项目

  • Spring中@DependsOn注解的作用及实现原理解析

    本文给大家讲解Spring中@DependsOn注解的作用及实现原理! 官方文档解释 Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another thro

随机推荐