SpringDataJPA -05- JPQL的基本使用

29 篇文章 3 订阅

目录


基本概述

 jpql的查询方式

​   jpql : jpa query language (jpq查询语言)

  ​ 特点:
    1. 语法或关键字和sql语句类似

    2. 查询的是类和类中的属性

​    3. 需要将JPQL语句配置到接口方法上

​       1).特有的查询:需要在dao接口上配置方法

      ​ 2).在新添加的方法上,使用注解的形式配置jpql查询语句

      ​ 3).注解 : @Query

查询

单参数查询
//    @Query(value="select * from cst_customer where cust_name=?1",nativeQuery = true)
//    @Query(value="from Customer  where cust_name= ?1")
//    @Query(value="select c from Customer  c where c.custName=?1")
//    @Query(value="from Customer c where c.custName=:#{#custName}")
    @Query(value="from Customer c where c.custName=:custName")
    List<Customer> findAllCustomerByName(@Param("custName") String custName);

这几种方式是等价的

  • @Query(value=“select * from cst_customer where cust_name=?1”,nativeQuery = true)
  • @Query(value=“from Customer where cust_name= ?1”)
  • @Query(value=“select c from Customer c where c.custName=?1”)
  • @Query(value=“from Customer c where c.custName=:#{#custName}”)
  • @Query(value=“from Customer c where c.custName=:custName”)

多参数查询
//    @Query(value="from Customer  c where c.custId=?2 and c.custName=?1")
//    @Query(value="from Customer  c where c.custId=:#{#custId} and c.custName=:#{#custName}")
@Query(value="from Customer  c where c.custId=:custId and c.custName=:custName")
List<Customer> findCustomersByNameAndIndus(@Param("custName") String name,@Param("custId") Long id);

这几种方式是等价的(还有一种原生sql的方式)

  • @Query(value=“from Customer c where c.custId=?2 and c.custName=?1”)
  • @Query(value=“from Customer c where c.custId=:#{#custId} and c.custName=:#{#custName}”)
  • @Query(value=“from Customer c where c.custId=:custId and c.custName=:custName”)

参数为对象查询
@Query(value="from Customer  c where c.custId=:#{#customer.custId}")
Customer findCustomerByInfo(@Param("customer") Customer customer);

更新和删除

注意

​ 1.使用 delete 或者 update 操作时

​   @query注解下方需要增加 @Modifying注解

​ 2.在测试dao层(update、delete)方法时,必须开启事务@Transactional注解

​ 3.在测试dao层(update、delete)方法时,默认不开启事务提交,

​   需要配置@Rollback(value = false)


update
@Query(value="update Customer c set c.custName=:custName where c.custId=:custId")
@Modifying
Integer updateCustomer(@Param("custId")Long custId,@Param("custName") String custName);

测试

@Test
@Transactional(rollbackFor = Exception.class)
@Rollback(value=false)  //设置是否自动回滚
public void testJpqlUpdate() {
    Integer flag = this.customerRepository.updateCustomer(2L, "黑马飞起来");
    System.out.println(flag==1?"更新成功":"更新失败");
}

delete
@Query(value="delete Customer  c where c.custId=:custId")
@Modifying
Integer deleteCustomer(@Param("custId")Long custId);

测试

@Test
@Transactional(rollbackFor = Exception.class)
@Rollback(value=false)  //设置是否自动回滚
public void testJpqlUpdate() {
    Integer flag = this.customerRepository.deleteCustomer(3L);
    System.out.println(flag==1?"删除成功":"删除失败");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值