/**
- 在CustomerDao接口中添加查询方法
*/
@Query(value = “from Customer where custName = ?”)
public Customer findByName(String custName);
/**
- 在CustomerFindTest中测试根据name查询
*/
@Test
public void testFindJPQL(){
Customer customer=customerDao.findByName("客户1");
System.out.println(customer);
}
**2\. 根据客户名称与客户id查询客户,使用jpql形式实现**
`配置jpql语句,使用应使用@Query注解, jpql语句 : jpql : from Customer where custName = ? and custId = ?`
对于多个占位符参数:赋值的时候,默认的情况下,占位符的位置需要和方法参数中的位置保持一致,但是可以指定占位符参数的位置 `? 索引` 的方式,指定次占位的取值来源。
/**
- 在CustomerDao接口中添加查询方法
*/
@Query(value = “from Customer where custName = ? and custId = ?”)
//@Query(value = “from Customer where custId = ?2 and custName = ?1”)
public Customer findCustNameAndId(String name,Long id);
/**
- 测试根据客户名称与id查询
*/
@Test
public void testFindCustNameAndId(){
Customer customer=customerDao.findCustNameAndId("客户1",1l);
System.out.println(customer);
}
**3\. 使用jpql语句实现更新操作**
`配置jpql语句,使用应使用@Query注解, jpql语句 : jpql : update Customer set custName = ? where custId = ?`
于查询语句不同,我们还需要对更新操作进行声明`@Modifying :当前执行的更新操作`,同时springDataJpa中使用jpql完成更新或删除操作需要手动添加事务的支持,因为方法在执行结束后默认会执行回滚事务,我们还需要使用`@Rollback(value =false)` 取消回滚
/**
- 在CustomerDao接口中添加更新语句的方法
*/
@Query(value = “update Customer set custName = ?2 where custId = ?1”)
@Modifying
public void updateCustomer(long custId,String custName);
/**
- 测试jpql的更新操作
*/
<