如何使用 SpringData 在字符串中以随机顺序在多个 LIKE 中搜索单词

我有字符串“AlphaBetaZeta GammaTheta”(来自 MS SQL 数据库的表字段),我需要使用具有许多类似的弹簧数据和随机顺序和关键字的随机计数进行搜索


我正在尝试搜索:


findByLabelContaining(Collection<String> labels)

findByLabelLike(Collection<String> labels)

但这些不起作用。


在数据库中启用全文搜索或使用弹性我不能,只有带有本机接口方法或自定义 jpql-query 的 Spring-Data。


SELECT id, label

FROM TABLE10

WHERE label like '%gam%' AND

      label like '%alpha%' AND

      label LIKE '%theta%'


SELECT id, label

FROM TABLE10

WHERE label like '%alpha%' AND

      label like '%theta%'

和更多..


青春有我
浏览 207回答 1
1回答

倚天杖

使用自定义本机查询:@Query(value = "SELECT id, labelFROM TABLE10WHERE label like %?1%", native = true)&nbsp; List<User> findByLabel(String a);https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_using_advanced_like_expressions如果您需要可变数量的参数,则必须编写自己的查询:@PersistenceContextEntityManager entityManager;@Overridepublic List<YourClass> findByLabelLike(List<String> labels) {&nbsp; &nbsp; String q = "SELECT id, label FROM TABLE10";&nbsp; &nbsp; for (String l : labels) {&nbsp; &nbsp; &nbsp; &nbsp;// Add where and ORs&nbsp; &nbsp; }&nbsp; &nbsp; Query query = entityManager.createNativeQuery(q, YourClass.class);&nbsp; &nbsp; int i = 0;&nbsp; &nbsp; for (String label : labels) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.setParameter(++i, "%"+label+"%");&nbsp; &nbsp; }&nbsp; &nbsp; return query.getResultList();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java