是否可以使用 'LIKE' 和 '= ?'在同一个 SQL 查询中?

Is it possible to use 'LIKE' and '= ?' In the same SQL query?

我正在创建一个对列表进行排序的过滤器函数。我正在使用 ajax 到 return 并显示结果。

但是当搜索输入和选项都已设置时,我似乎无法找到 return 结果的方法。

每当我这样写查询时,我总是收到错误消息:

@Query(value = "SELECT * FROM colaboradores where name LIKE :search% AND department = ? AND category = ? ORDER BY id DESC", nativeQuery = true)
List <Colaboradores> filterColaboradorBySearch(@Param("search")String search, int department, int category);

错误:

    Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.dexa.hr.repository.ColaboradorRepository.filterColaboradoresAll(java.lang.String,int,int)! Reason: Mixing of ? parameters and other forms like ?1 is not supported!; nested exception is java.lang.IllegalArgumentException: Mixing of ? parameters and other forms like ?1 is not supported!
    at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
    at java.base/java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1061) ~[na:na]
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:96) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new[=13=](QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:364) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet(RepositoryFactoryBeanSupport.java:322) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328) ~[spring-data-commons-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
    ... 84 common frames omitted
Caused by: java.lang.IllegalArgumentException: Mixing of ? parameters and other forms like ?1 is not supported!
    at org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(StringQuery.java:281) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.access[=13=]0(StringQuery.java:182) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.StringQuery.<init>(StringQuery.java:73) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.DeclaredQuery.of(DeclaredQuery.java:38) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.assertParameterNamesInAnnotatedQuery(JpaQueryMethod.java:158) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:139) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory.build(DefaultJpaQueryMethodFactory.java:44) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87) ~[spring-data-jpa-2.6.1.jar:2.6.1]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.6.1.jar:2.6.1]
    ... 106 common frames omitted

我想知道为什么。

此错误与=LIKE的混合无关。

发生这种情况是因为对于一个值(搜索)您使用 @Param 而对于其他 2 个属性您使用问号作为占位符。

改为

@Query(value = "SELECT * FROM colaboradores where name LIKE :search% AND department = :department AND category = :category ORDER BY id DESC", nativeQuery = true)
List<Colaboradores> filterColaboradorBySearch(@Param("search") String search, @Param("department") int department, @Param("category") int category);

或到

@Query(value = "SELECT * FROM colaboradores where name LIKE ?1% AND department = ?2 AND category = ?3 ORDER BY id DESC", nativeQuery = true)
List<Colaboradores> filterColaboradorBySearch(String search, int department, int category);