Lav*_*Lav 9 spring jpa spring-data spring-data-jpa
我正在阅读Spring Data JPA Tutorial.我很困惑这个框架如何在内部工作.让我说明具体情况
有特定的代码
/**
* Custom finder
*/
public List<Location> getLocationByStateName(String name) {
@SuppressWarnings("unchecked")
List<Location> locs = entityManager
.createQuery("select l from Location l where l.state like :state")
.setParameter("state", name + "%").getResultList(); // note
return locs;
}
Run Code Online (Sandbox Code Playgroud)
这只是由以下界面取代
@Repository
public interface LocationJPARepository extends JpaRepository<Location, Long> {
List<Location> findByStateLike(String stateName);
}
Run Code Online (Sandbox Code Playgroud)
相应的测试用例运行良好
@Test
public void testFindWithLike() throws Exception {
List<Location> locs = locationRepository.getLocationByStateName("New");
assertEquals(4, locs.size());
}
Run Code Online (Sandbox Code Playgroud)
新的测试案例
@Test
public void testFindWithLike() throws Exception {
List<Location> locs = locationJPARepository.findByStateLike("New");
assertEquals(4, locs.size());
}
Run Code Online (Sandbox Code Playgroud)
我的问题
希望我能够正确解释我的问题.如果我需要添加更多清晰度,请告诉我.
我建议您查看参考指南的" 查询创建"部分.它非常清楚地解释了规则.
例如,当您想要通过名字找到User并忽略大小写时,您将使用方法名称,如同findByFirstnameIgnoreCase将其转换为条件UPPER(x.firstame) = UPPER(?1).
默认情况下,当您有findByProperty方法时,匹配是完全匹配的,因此如果您想要具有LIKE功能,则可以使用方法名称findByFirstnameLike,而后者又会转换为条件where x.firstname like ?1.
你可以结合这些关键字,但它可能会有点疯狂.我个人更喜欢使用@Query注释来处理更复杂的查询,以避免超长的存储库方法名称.
| 归档时间: |
|
| 查看次数: |
8518 次 |
| 最近记录: |