Spring Data Jpa的查询
SpringJpa的复杂查询
-
借助接口中定义好的方法完成查询
/* * 统计查询:查询客户的总数量 * */ @Test public void testCount(){ long count = customerDao.count(); System.out.println(count); } /* * 判断id为4的客户是否存在 * */ @Test public void testExists(){ boolean b = customerDao.exists(4); System.out.println(b); } /* * 根据id从数据库中查询 * @Transactional:保证getOne的正常运行 * findOne:立即加载 * getOne:延迟加载,底层是getReference * */ @Test @Transactional public void testGetOne(){ Customer customer = customerDao.getOne(1); System.out.println(customer); }
-
jpql查询方式
需要将jpql语句配置到接口方法上
- 特有的查询,需要在dao接口上配置方法
- 在新添加的方法上,使用注解的形式配置jpql查询语句
- 注解:@Query
根据客户名查询客户
public interface CustomerDao extends JpaRepository<Customer,Integer>, JpaSpecificationExecutor<Customer> { /* * 根据客户名查询客户 * jpql:from customer where name = ? * */ @Query(value = "from Customer where name = ?") public Customer findJpql(String custname); }
@RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境 @ContextConfiguration(locations = "classpath:applicationContext.xml") //指定spring容器的配置信息 public class JpqlTest { @Resource CustomerDao customerDao; @Test public void testFindJpql(){ Customer customer = customerDao.findJpql("黑马"); System.out.println(customer); } }
根据客户名和客户id查询客户
/* * 根据客户名和客户id查询客户 * 对于多个占位符,默认情况下,占位符位置和参数的位置保持一致 * 可以指定占位符参数的位置 * ? 索引:指定此占位符的取值来源,例如?2:就是从方法参数中取第二个参数 * *//* * 根据客户名和客户id查询客户 * */ @Query(value = "from Customer where name = ? and id = ?") Customer findCusByNameAndId(String custname,Integer id);
@Test public void testFindCusByNameAndId(){ Customer customer = customerDao.findCusByNameAndId("黑马程序员",3); System.out.println(customer); }
根据id更新客户名
/* * 根据客户id更新客户名 * @Query:代表的是进行查询 * @Modifying:声明此方法是用来进行更新的 * */ @Query(value = "update Customer set name = ?2 where id = ?1") @Modifying void updCusById(Integer id,String custname);
/* * 修改、删除需要添加事务支持,否则会报错 * 添加了事务的支持,默认执行之后会回滚事务,使用@Rollback设置不自动回滚 * */ @Test @Transactional @Rollback(value = false) public void testUpdCusById(){ customerDao.updCusById(1,"heima"); }
-
SQL语句的查询
需要将sql语句配置到接口方法上
-
特有的查询,需要在dao接口上配置方法
-
在新添加的方法上,使用注解的形式配置sql查询语句
-
注解:@Query
属性:nativeQuery:是否使用本地查询,true(使用本地查询,就是sql查询),false(不使用,默认值,也就是jpql查询)
查询全部客户
/* * 查询全部客户 * nativeQuery:true sql查询 | false:jpql查询 * */ @Query(value = "select * from customer",nativeQuery = true) List<Object[]> findSql();
@Test public void testFindSql(){ List<Object[]> list = customerDao.findSql(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
客户名模糊查询
/* * 客户名模糊查询 * */ @Query(value = "select * from customer where name like ?",nativeQuery = true) List<Object[]> findByLike(String name);
@Test public void testFindByLike(){ List<Object[]> list = customerDao.findByLike("%马%"); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
-
-
方法名称规则查询
是对jpql查询更加深入的一层封装,我们只需要按照SpringDataJpa提供的方法名称规则定义方法,不需要再去配置jpql语句,完成查询
根据客户名查询客户
/* * 方法名的约定: * findBy:查询 * 对象中属性的名称(首字母大写),查询的条件 * */ Customer findByName(String custname);
//测试方法命名查询 @Test public void testFindBy(){ Customer customer = customerDao.findByName("heima"); System.out.println(customer); }
模糊查询
/* * 模糊查询 * */ Customer findByNameLike(String custname);
//测试模糊查询 @Test public void testFindByNameLike(){ Customer customer = customerDao.findByNameLike("%学%"); System.out.println(customer); }
多条件查询
//多条件查询 Customer findByNameLikeAndId(String custname,Integer id);
//测试多条件查询 @Test public void testFindByNameLikeAndId(){ Customer customer = customerDao.findByNameLikeAndId("%学%",2); System.out.println(customer); }