我试图在 Spring Boot 中进行原生查询,但不幸的是它抛出了一个异常。
这是我的方法:
@Override
public List<Classification> doFuzzySearch(String search) {
Query query = em.createNativeQuery("SELECT * FROM `classification` WHERE SOUNDEX(`data`) LIKE CONCAT('%', SUBSTRING(SOUNDEX('(?1)'), 2), '%')", Classification.class);
query.setParameter(1, search);
return query.getResultList();
}
查询在 MySQL 中运行没有任何问题,但在 Spring 中它抛出此异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
我不知道我做错了什么。我将 ?1
更改为 ?0
,但这没有用。希望有人知道我做错了什么......
最佳答案
查询应该是
Query query = em.createNativeQuery("SELECT * FROM `classification` +
"WHERE SOUNDEX(`data`) LIKE CONCAT('%', SUBSTRING(SOUNDEX(?'), 2), '%')",
Classification.class);
原因是参数不需要被包装到' '
中。也只是使用自然顺序。无需提供?1
关于java - native 查询抛出 InvalidDataAccessApiUsageException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470563/