Spring Data JPA的查询方式
4.1 使用Spring Data JPA中接口定义的方法进行查询
在继承JpaRepository,和JpaRepository接口后,我们就可以使用接口中定义的方法进行查询
继承JpaRepository后的方法列表
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Kv6AzI2-1607324826037)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20201130211929073.png)]](https://i-blog.csdnimg.cn/blog_migrate/da3a17f27e462f6c64ab99d23af54519.png)
继承JpaSpecificationExecutor的方法列表
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-32NpbchH-1607324826041)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20201130211936037.png)]](https://i-blog.csdnimg.cn/blog_migrate/c4edc0b88df86459c113813867de763b.png)
测试count()方法和exists()方法
/**
* 测试统计查询: 查询客户的总数量
* count():通过主键id进行统计客户总数量
* */
@Test
public void testCount(){
long count = customerDao.count(); //查询全部客户的总数量
System.out.println(count);
}
/**
* 测试 : 判断id为4的客户是否存在
* 方法一
* 可以查询一下id为4的客户,
* 如果返回值为空,代表不存在,如果不为空,代表存在。
*
* 方法二:
* 判断数据库中id为4的客户的数量
* 如果数量为0,代表不存在,如果大于0,代表存在。
*/
@Test
public void testExists(){
boolean exists = customerDao.exists(4);
System.out.println("id为4的客户是否存在: "+exists );
}
测试getOne()方法
/**
* 根据id从数据库查询
* @Transactional: 保证getone方法能正常运行
*
* findOne:
* 底层调用的是: em.findOne()方法 em是EntiyManager 立即加载
* getOne:
* 底层调用的是: em.getReference() :延迟加载
* *返回的是一个客户的动态代理对象
* * 什么时候用,什么时候查询
*/
@Test
@Transactional
public void testGetOne(){
Customer customer = customerDao.getOne(1);
System.out.println(customer);
}
使用JPQL的方式查询
jpql的查询方式:
- jpql: jpa query language (jpq查询语言)
- 特点: 语法或关键字和sql语句类似, 查询的是类和类中的属性
- 需要将JPQL语句配置到接口方法上
- 特有的查询:需要在dao接口上配置方法
- 在新添加的方法上,使用注解的形式配置jpql查询语句
- 注解: @Query
使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件,这时就可以使用@Query注解,结合JPQL的语句方式完成查询
@Query 注解的使用非常简单,只需在方法上面标注该注解,同时提供一个JPQL查询语句即可
public interface CustomerDao extends JpaRepository<Customer, Long>,JpaSpecificationExecutor<Customer> {
//@Query 使用jpql的方式查询。
@Query(value="from Customer")
public List<Customer> findAllCustomer();
//@Query 使用jpql的方式查询。?1代表参数的占位符,其中1对应方法中的参数索引
@Query(value="from Customer where custName = ?1")
public Customer findCustomer(String custName);
}
此外,也可以通过使用 @Query 来执行一个更新操作,为此,我们需要在使用 @Query 的同时,用 @Modifying 来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询
@Query(value="update Customer set custName = ?1 where custId = ?2")
@Modifying
public void updateCustomer(String custName,Long custId);
测试Jpql查询
在CustomerDao文件中定义Jpql查询方法
/**
* 符合SpringDataJpa的dao层接口规范
* JpaRepository<操作的实体类类型,实体类中主键属性的类型>
* * 用来完成基本CRUD操作
* JpaSpecificationExecutor<操作的实体类类型>:
* * 用于复杂查询(分页等查询操作)
*
* */
public interface CustomerDao extends JpaRepository<Customer,Integer>,JpaSpecificationExecutor<Customer>{
/**
* 案例: 根据客户名查询客户信息
* 使用jpql进行查询
*
* jpql: from Customer where cust_name =?
*
* 配置jpql语句,使用的@Query注解
*/
@Query(value = "from Customer where cust_name =?1")
public Customer findJpql(String custName);
}
在JpqlTest.java中测试
@RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class JpqlTest {
//从容器中获取CustomerDao对象
@Autowired
private CustomerDao customerDao;
//测试jpql语句
@Test
public void testJpql(){
Customer customer = customerDao.findJpql

最低0.47元/天 解锁文章
5812

被折叠的 条评论
为什么被折叠?



