JPQL查询
@Query中的value类似sql的查询语句
*但是查询的是类和类中的属性,而不是数据库中的字段名*
一 测试 jpql一个参数查询:
/*
* 符合SpringDatajpa的dao层接口规范
*JpaRepository<操作的实体类类型,实体类中主键属性的类型>
*封装了基本的CRUD操作
JpaSpecificationExecutor<操作的实体类类型>
*封装了复杂查询操作(分页)
* */
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
/*
*
* 案例 :根据客户名称查询客户
* 使用 jpql的形式查询
* jpql:from Customer where custName=?
* 配置jpql语句,使用的@Query注解
*
*
* */
@Query(value = "from Customer where custName=? ")
public Customer findJpql(String custName);
}
public class jpqlTest {
@Autowired
private CustomerDao customerDao;
//测试jpql一个参数查询
@Test
public void testFindJPQL()
{
Customer customer=customerDao.findJpql("迅腾科技公司");
System.out.println(customer);
}
}
二 测试 jpql多个参数查询
/*
*
* 案例:根据客户名称和客户id查询客户
* jpql: from Customer where custName =? and custId=?
*
* 对于多个占位符
* 赋值的时候,默认的情况下,占位符的位置需要和方法参数中的位置保持一致
*
* 可以指定占位符参数的位置
* *索引的范式,指定此站位的取值来源
* */
@Query(value = "from Customer where custName=?2 and custId=?1 ")
public Customer findCustNameAndId(Long id,String name);
//测试jpql多个参数查询
@Test
public void testFindCustNameAndId()
{
Customer customer=customerDao.findCustNameAndId(4l,"迅腾工作人员aa");
System.out.println(customer);
}
三 测试 jpql的更新操作
/*
*
* 使用jpql完成更新操作
*
* sql:update cst_customer set cust_name=? where cust_id (column值)
* jpql:update Customer set custName=? where custId=? (domin对应的属性值)
* @Query : 代表的事进行查询
* *声明此方法是用来进行更新操作
* @Modifying
* * 当前执行的事一个更新操作
* */
@Query(value = "update Customer set custName=?2 where custId=?1")
@Modifying
public void updateCustomer (long custId,String custName);
@Test
@Transactional//添加事务支持
@Rollback(value = false)
public void testUpdateCust()
{
customerDao.updateCustomer(4l,"迅腾工作人员");
}