我将我的 ContactDao 定义如下:
public interface ContactDao extends JpaRepository<Contact, Long> {
/**
* Finds all contacts that the given user has entered where the contact's full name matches {@code name}.
* @param userId The current user's LDAP id.
* @param name The name to search for.
* @return A list of contacts matching the specified criteria.
*/
@Query(" select c from Form as f" +
" inner join f.contacts as c" +
" where f.requestorUserId = :userId" +
" and lower(c.fullName) like lower(:name)" +
" order by lower(c.fullName)")
List<Contact> findUserContactsByUserIdAndName(@Param("userId") String userId, @Param("name") String name);
}
以上工作完美,生成的 SQL 是我自己编写的。问题是我想在:name
参数中添加周围的通配符。有什么好方法可以做到这一点吗?我查看了 Spring Data JPA 参考文档,但找不到任何关于通配符和@query
.
以下hack有效,但有点难看:
and lower(c.fullName) like '%' || lower(:name) || '%'
有没有人有更好的解决方案?
谢谢,穆尔。