环境和上一篇的环境一样:点击
/**
* 案例:根据客户名称查询客户
* 使用jpql的形式查询
* jpql:from Customer where custName = ?
*
* 配置jpql语句,使用@Query注解
*/
// 行版本的
// JDBC style parameters (?) are not supported for JPA queries. 语句后面需要添加占位符的位置从1,开始
@Query(value = "from Customer where custName =?1")
public Customer findCustomerByCustName1(String name);
/**
* 案例:根据客户名称和客户id查询客户
* jpql:from Customer where custName = ? and custId = ?
* 低版本中 :默认对于多个站位符中,方法参数需要与占位符的顺序相同,也可以指定占位符的来源
* 高版本中都需要指定来源
*
*/
@Query(value = "from Customer where custName = ?1 and custId = ?2")
public Customer findCustomerByCustNameAndCustId(String name,Long id);
/**
* 使用jpql 根据id更新新用户额的名称
* sql : update cst_customer set cst_name = ? where cst_id = ?
* jpql:update Customer set custName = ? where custId = ?
* @Query
* 是用来查询的,要进行其他的操作需要申明
*
* 申明此操作是用来更新操作的
* @Modifying 代表的是当前的是一个更新操作
*/
//报错:问题没有解决记录一下
//InvalidDataAccessApiUsageException: Parameter value [4] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [4] did not match expected type [java.lang.String (n/a)]
@Query(value = " update Customer set custName = ?1 where custId = ?2")
@Modifying
public void updateCustomerById(String name,long id);
/**
* 使用sql的形式查询
* 查询全部的客户
* sql : select * from cst_customer;
* Query: 配置sql查询
* value:sql查询
* nativeQuery:查询方式
* true:sql查询
* false:jpql查询
*
*/
@Query(value = "select * from cst_customer",nativeQuery = true)
public List<Object[]> findsql();
@Query(value = "select * from cst_customer where cust_name like ?1",nativeQuery = true)
public List<Object [] > findAllByCustName(String name);
/**
*根据约定的命名规范来编写方法签名,可以直接使用
* findBy:查询
* 对象中的属性(首字母大写):查询条件
* CustName
* 默认情况使用 等于的方式进行查询
* 特殊查询方式
* findByCustName ---- 根据客户名称查询
* 再springdataJpa的运行阶段
* 会根据方法名称进行解析 findBy from xxx(实体类)
* 属性名称 where custName =
*/
public Customer findCustomerByCustName(String name);
/**
* 特殊查询方式
* findBy + 属性名称 (根据属性名称进行完成匹配查询)
* findBy + 属性名称 + “查询方式(Like | isnull)"
* findByCustNameLike
*
*/
public List<Customer> findByCustName(String name);
/**
* 使用客户名称模糊匹配和客户所属行业精准匹配查询
*/
public Customer findByCustNameLikeAndCustIndustry(String cusName,String custIndustry);
测试代码:
@Test
public void testfindCustomerByCustName(){
Customer customer = customerDao.findCustomerByCustName("qijian");
System.out.println(customer);
}
@Test
public void testfindCustomerByCustNameAndCustId(){
Customer customer = customerDao.findCustomerByCustNameAndCustId("qijian",4l);
System.out.println(customer);
}
/**
* 测试jpql的更新操作
* springDateJpa中使用jpql完成更新,删除操作
* 需要添加事务的支持
* 默认会执行完成后,回滚事务
* @RollBack : 设置是否自动回滚
* false|true
*/
//报错:问题没有解决记录一下
//InvalidDataAccessApiUsageException: Parameter value [4] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [4] did not match expected type [java.lang.String (n/a)]
@Test
@Transactional
@Rollback(value = false)
public void testUpdateById(){
customerDao.updateCustomerById("qijian",4l);
}
@Test
public void testfindsql(){
List<Object []> list = customerDao.findsql();
for (Object[] obj:list){
System.out.println(Arrays.toString(obj));
}
}
@Test
public void testfindAllByCustName(){
List<Object []> list = customerDao.findAllByCustName("qijian");
for (Object[] obj:list){
System.out.println(Arrays.toString(obj));
}
}
@Test
public void testFindCustomerByCustName(){
Customer customer = customerDao.findCustomerByCustName("qijian");
System.out.println(customer);
}
/**
* 测试方法名规则查询
*/
@Test
public void testfindByCustName(){
List list = customerDao.findByCustName("qijian");
System.out.println(list);
}
@Test
public void findByCustNameLikeAndCustIndustry(){
customerDao.findByCustNameLikeAndCustIndustry("IT","qiji%");
}
版权声明:本文为qq_43663493原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。