例如,使用spel,您可以在查询中使用SpringBean方法。
@Query("select e from MyEntity e where ?#{@myBean.myMethod(#param1)} is true")
List<MyEntity> entities = getEntity(@Param("param1") String param);
您可以以任何适当的方式定义bean,例如:
@Component
public class MyBean {
public boolean myMethod(String param) {...}
}
或者甚至可以是相同的回购:
public interface MyRepo extends JpaRepository<MyEntity, Long> {
@Query("select e from MyEntity e where ?#{@myRepo.myMethod(#param1)} is true")
List<MyEntity> entities = getEntity(@Param("param1") String param);
default boolean myMethod(String param) {...}
}
也可以在查询中使用参数属性:
@Query("select u from User u where u.firstname = ?#{#customer.firstname}")
List<User> findUsersByCustomersFirstname(@Param("customer") Customer customer);
其他信息:
1
,
2
,
3