delete()删除操作
@Test public void testDelete(){ customerDao.delete(3l); }
findAll()查询所有操作
@Test public void testFindAll(){ List<Customer> customerList = customerDao.findAll(); for (Customer customer : customerList) { System.out.println(customer); } }
执行过程
I:实例化的customerDao是一个动态代理对象SimpleJpaRepository
II:SimpleJpaRepository调用findOne()方法,findOne()会通过实体类管理器em调用find()方法完成查询
Spring Data JPA 完成复杂查询
统计查询count()
@Test public void testCount(){ long count = customerDao.count(); System.out.println(count); }
是否存在exists()
@Test public void testExists(){ boolean isExists = customerDao.exists(1l); System.out.println(isExists); }
根据ID查询getOne()
@Test @Transactional public void testGetOne(){ Customer one = customerDao.getOne(1l); System.out.println(one); }
底层调用的是EntityManager的getReference(),延迟加载,find()是立即加载
JPA中的查询方法:
使用JPQL完成复杂查询
JPQL:JPA Query Language
特点:语法或关键字与sql语句类似,查询的是类和类中的属性
需要将JPQL语句配置到接口方法上
- 特有的查询,需要在dao接口上配置方法
- 在新添加的方法上使用注解的形式配置JPQL语句
- 注解为@Query
在CustomerDao接口中新增方法,根据客户名称查询客户,使用JPQL语句
@Query(value = "from Customer where custName= ?") Customer findByCustName(String custName);
在CustomerDao中测试该方法
@Test public void testFindByCustName(){ Customer thor = customerDao.findByCustName("Thor"); System.out.println(thor); }
输出结果
多条件(多占位符)查询
在CustomerDao接口中新增方法
// 根据客户名称和id查询客户 @Query(value = "from Customer where custName = ? and custId=?") Customer findByCustNameAndCustId(String custName, Long custId);
在CustomerDaoTest中测试该方法
@Test public void testFindByCustNameAndCustId(){ Customer thor = customerDao.findByCustNameAndCustId("Thor",3L); System.out.println(thor); }
通过custId更新custName
CustomerDao接口中新增方法updateCustNameByCustId
// 要指定参数位置 // 声明为更新操作 @Query(value = "update Customer set custName = ?2 where custId = ?1") @Modifying @Transactional void updateCustNameByCustId(Long custId, String custName);
在CustomerDaoTest中测试该方法
@Test public void testUpdateCustName(){ customerDao.updateCustNameByCustId(3L,"Thor Odin"); }
使用SQL语句完成复杂查询
- 特有的查询,需要在dao接口上配置方法
- 在新添加的方法上,使用注解的形式配置SQL查询语句
- 注解为@Query(value=,nativeQuery=),value表示jpql语句或者sql语句,nativeQuery为boolean,false表示使用jpql查询,true表示sql查询
SQL语句查询全部
定义方法selectAll()
@Query(value = "SELECT * FROM customer", nativeQuery = true) List<Customer> selectAll();
测试该方法
@Test public void testSelectAll(){ List<Customer> customers = customerDao.selectAll(); for (Customer customer : customers) { System.out.println(customer); } }
输出结果
SQL语句模糊查询
定义方法selectByCustNameLike
@Query(value = "SELECT * FROM customer where cust_name like ?1", nativeQuery = true) List<Customer> selectByCustNameLike(String like);
测试该方法
@Test public void testSelectByCustNameLike(){ List<Customer> customers = customerDao.selectByCustNameLike("Thor%"); for (Customer customer : customers) { System.out.println(customer); } }
输出结果
方法名称规则查询
是对jpql查询更加深入的一层封装,只需要按照Spring Data JPA提供的方法名规则定义方法,不需要在配置jpql语句即可完成查询
命名规则:查询使用findBy,对象中的属性为查询的条件,如想要通过custName查询Customer,方法名命名为findByCustName,入参为custName,翻译成sql语句就是select * from customer where cust_name = ?,默认使用=,如果是模糊查询需要Like关键字
findByCustName
Customer findByCustName(String custName);
@Test public void testFindByCustName(){ Customer thor = customerDao.findByCustName("Thor Odin"); System.out.println(thor); }
输出结果
findByCustNameLike 模糊查询
List<Customer> findByCustNameLike(String custName);
@Test public void testFindByCustNameLike(){ List<Customer> customers = customerDao.findByCustNameLike("Thor%"); for (Customer customer : customers) { System.out.println(customer); } }
输出结果
findByCustNameAndCustId 多条件查询使用And关键字,查询的属性的顺序要与入参顺序一致
Customer findByCustNameLikeAndCustIndustry(String custName, String custIndustry);
@Test public void testFindByCustNameLikeAndCustIndustry(){ Customer stark_industry = customerDao.findByCustNameLikeAndCustIndustry("Iron%", "Military Industry"); System.out.println(stark_industry); }