spring data jpa查询

it2023-03-20  83

spring data jpa查询

1 spring Data JPA查询调用接口方法查询(count,exists) 编写测试统计查询测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 测试CRUD的操作 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class CustomerDaoTest { // 声明CustomerDao业务对象 @Autowired // 自动注入 private CustomerDao customerDao; /** * 测试统计查询:查询客户的总数量 * count:统计总条数 */ @Test public void testCount() { // 查询全部的客户数量 long count = customerDao.count(); System.out.println(count); } }

运行结果如图所示: 编写测试客户是否存在的代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 测试CRUD的操作 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class CustomerDaoTest { // 声明CustomerDao业务对象 @Autowired // 自动注入 private CustomerDao customerDao; /** * 测试:判断id为5的客户是否存在 * 1. 可以查询以下id为5的客户 * 如果值为空,代表不存在,如果不为空,代表存在 * 2. 判断数据库中id为5的客户的数量 * 如果数量为0,代表不存在,如果大于0,代表存在 */ @Test public void testExists() { boolean exists = customerDao.exists(5l); System.out.println("id为5的客户 是否存在:"+exists); } }

运行结果如图所示: 2 spring Data JPA查询调用接口方法查询(findOne和getOne的区别) 编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import com.txw.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; /** * 测试CRUD的操作 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class CustomerDaoTest { // 声明CustomerDao业务对象 @Autowired // 自动注入 private CustomerDao customerDao; /** * 根据id从数据库查询 * @Transactional : 保证getOne正常运行 * findOne: * em.find() :立即加载 * getOne: * em.getReference :延迟加载 * * 返回的是一个客户的动态代理对象 * * 什么时候用,什么时候查询 */ @Test @Transactional public void testGetOne() { Customer customer = customerDao.getOne(5l); System.out.println(customer); } }

运行结果如图所示: 3 JPQL查询引入 在继承JpaRepository,和JpaRepository接口后,我们就可以使用接口中定义的方法进行查询。 继承JpaRepository后的方法列表 继承JpaSpecificationExecutor的方法列表 jpql的查询方式 jpql:jpa query language (jpq查询语言) 特点:语法或关键字和sql语句类似。 查询的是类和类中的属性。 需要将JPQL语句配置到接口方法上 1.特有的查询:需要在dao接口上配置方法 2.在新添加的方法上,使用注解的形式配置jpql查询语句 3.注解 : @Query 使用Spring Data JPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来说,我们还需要灵活的构造查询条件,这时就可以使用@Query注解,结合JPQL的语句方式完成查询。 @Query 注解的使用非常简单,只需在方法上面标注该注解,同时提供一个JPQL查询语句即可。 此外,也可以通过使用 @Query 来执行一个更新操作,为此,我们需要在使用 @Query 的同时,用 @Modifying 来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询。 4 使用jpql完成基本查询 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 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); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import com.txw.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 根据客户名称查询 */ @Test public void testFindJPQL() { Customer customer = customerDao.findJpql("学无止路"); System.out.println(customer); } }

运行结果如图所示: 5 多占位符的赋值 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 案例:根据客户名称和客户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); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import com.txw.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; @Test public void testFindCustNameAndId() { // Customer customer = customerDao.findCustNameAndId(5l, "Adair"); Customer customer = customerDao.findCustNameAndId(5l,"Adair"); System.out.println(customer); } }

运行结果如图所示: 6 使用jpql完成更新操作 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 使用jpql完成更新操作 * 案例 : 根据id更新,客户的名称 * 更新7号客户的名称,将名称改为“学无止路” * sql :update cst_customer set cust_name = ? where cust_id = ? * jpql : update Customer set custName = ? where custId = ? * @Query : 代表的是进行查询 * * 声明此方法是用来进行更新操作 * @Modifying * * 当前执行的是一个更新操作 */ @Query(value = " update Customer set custName = ?2 where custId = ?1 ") @Modifying public void updateCustomer(long custId,String custName); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 测试jpql的更新操作 * * springDataJpa中使用jpql完成 更新/删除操作 * * 需要手动添加事务的支持 * * 默认会执行结束之后,回滚事务 * @Rollback : 设置是否自动回滚 * false | true */ @Test @Transactional //添加事务的支持 @Rollback(value = false) public void testUpdateCustomer() { customerDao.updateCustomer(7l,"学无止路"); } }

运行之前数据库的数据如图所示: 运行结果如图所示: 运行之后数据库的数据如图所示: 7 查询全部 sql语句的查询 1.特有的查询:需要在dao接口上配置方法 2.在新添加的方法上,使用注解的形式配置sql查询语句 3.注解 :@Query value :jpql语句 | sql语句 nativeQuery :false(使用jpql查询) | true(使用本地查询:sql查询) 是否使用本地查询 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import java.util.List; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 使用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(); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Arrays; import java.util.List; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 测试sql查询 */ @Test public void testFindSql() { List<Object[]> list = customerDao.findSql(); for(Object [] obj : list) { System.out.println(Arrays.toString(obj)); } } }

运行结果如图所示: 8 条件全部 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import java.util.List; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 使用sql的形式查询: * 查询全部的客户 * sql : select * from cst_customer; * Query : 配置sql查询 * value : sql语句 * nativeQuery : 查询方式 * true : sql查询 * false:jpql查询 */ @Query(value="select * from cst_customer where cust_name like ?1",nativeQuery = true) public List<Object [] > findSql(String name); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Arrays; import java.util.List; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 测试sql模糊配置查询 */ @Test public void testFindSql() { List<Object[]> list = customerDao.findSql("Adair%"); for(Object [] obj : list) { System.out.println(Arrays.toString(obj)); } } }

运行结果如图所示: 9 方法命名规则查询:基本查询 顾名思义,方法命名规则查询就是根据方法的名字,就能创建查询。只需要按照Spring Data JPA提供的方法命名规则定义方法的名称,就可以完成查询工作。Spring Data JPA在程序执行的时候会根据方法名称进行解析,并自动生成查询语句进行查询 按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。

//方法命名方式查询(根据客户名称查询客户) public Customer findByCustName(String custName);

具体的关键字,使用方法和生产成SQL如下表所示: 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 方法名的约定: * findBy : 查询 * 对象中的属性名(首字母大写) : 查询的条件 * CustName * * 默认情况 : 使用 等于的方式查询 * 特殊的查询方式 * findByCustName -- 根据客户名称查询 * 再springdataJpa的运行阶段 * 会根据方法名称进行解析 findBy from xxx(实体类) * 属性名称 where custName = * 1.findBy + 属性名称 (根据属性名称进行完成匹配的查询=) * 2.findBy + 属性名称 + “查询方式(Like | isnull)” * findByCustNameLike * 3.多条件查询 * findBy + 属性名 + “查询方式” + “多条件的连接符(and|or)” + 属性名 + “查询方式” */ public Customer findByCustName(String custName); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import com.txw.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 测试方法命名规则的查询 */ @Test public void testNaming() { Customer customer = customerDao.findByCustName("Adair"); System.out.println(customer); } }

运行结果如图所示: 注意事项:custName在数据库中数据相同就会报错误。 10 方法命名规则查询:模糊匹配 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import java.util.List; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 根据客户名进行模糊查询 * @param custName * @return */ public List<Customer> findByCustNameLike(String custName); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import com.txw.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 测试方法命名规则的查询 */ @Test public void testNaming() { List<Customer> list = customerDao.findByCustNameLike("学无止路"); for (Customer customer : list) { System.out.println(customer); } } }

运行结果如图所示: 11 方法命名规则查询:多条件查询 编写CustomerDao的案例的代码如下:

package com.txw.dao; import com.txw.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; /** * 符合SpringDataJpa的dao层接口规范 * JpaRepository<操作的实体类类型,实体类中主键属性的类型> * 封装了基本CRUD操作 * JpaSpecificationExecutor<操作的实体类类型> * 封装了复杂查询(分页) * @author: Adair */ @SuppressWarnings("all") // 注解警告信息 public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> { /** * 使用客户名称模糊匹配和客户所属行业精准匹配的查询 * @param custName * @param custIndustry * @return */ public Customer findByCustNameLikeAndCustIndustry(String custName,String custIndustry); }

编写测试代码如下:

package com.txw.test; import com.txw.dao.CustomerDao; import com.txw.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 使用jpql测试 * @author: Adair */ @RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息 @SuppressWarnings("all") // 注解警告信息 public class JpqlTest { // 声明CustomerDao业务对象 @Autowired private CustomerDao customerDao; /** * 测试方法命名规则的查询 */ @Test public void testFindByCustNameLikeAndCustIndustry() { Customer customer = customerDao.findByCustNameLikeAndCustIndustry("学无止路%", "笔记"); System.out.println(customer); } }

运行结果如图所示:

最新回复(0)