我正在瀏覽Spring Data JPA教程。 我是困惑關於這個框架如何在內部工作。 讓我提出具體的方案Spring Data JPA如何在內部工作
有這是簡單地通過下面的接口
@Repository
public interface LocationJPARepository extends JpaRepository<Location, Long> {
List<Location> findByStateLike(String stateName);
}
和相應的測試用例更換特定代碼
/**
* 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;
}
工作的罰款
@Test
public void testFindWithLike() throws Exception {
List<Location> locs = locationRepository.getLocationByStateName("New");
assertEquals(4, locs.size());
}
新的測試案例
@Test
public void testFindWithLike() throws Exception {
List<Location> locs = locationJPARepository.findByStateLike("New");
assertEquals(4, locs.size());
}
我的問題
- 如何框架知道如果我使用尋找精確匹配=或使用SQL LIKE操作部分匹配(它不能是方法的名字嗎?)
- 如果它在某種程度上決定我我正在尋找部分匹配,然後仍然有子選項...像名稱%或%名稱或%名稱%...
- 也如何決定案例是重要的像? (我可以像使用toUpper()一樣使用SQL來區分大小寫,即通過比較大寫的所有內容)
- (增加的問題)有沒有一種方法可以檢查日誌中的EXACT SQL?
希望我能夠正確解釋我的問題。讓我知道如果我需要添加更清晰。
我建議閱讀參考指南,[這裏](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation )和[這裏](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods)。簡而言之,它基於你的方法如何命名。 –
有沒有辦法檢查日誌中的EXACT SQL? – Lav