最近在用spring boot框架做一个项目。做到使用关键字搜索的时候出现以下报错:
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
我的repository类如下:(关于@RepositoryRestResource可以参考http://www.jianshu.com/p/3423fa97d185)
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.transaction.annotation.Transactional;
import edu.dgut.supervision.domain.VDuchadian;
@RepositoryRestResource(collectionResourceRel = "VDuchadian", path = "vDuchadian")
public interface DuchadianRepository extends PagingAndSortingRepository<VDuchadian, String>{//String代表主键的数据类型
List<VDuchadian> findByProjectNo(@Param("projectNo") String projectNo);
@Transactional(readOnly=true)
@Query("select u from VDuchadian u where u.number = ?1")
VDuchadian findByNumber(@Param("number") String number);
@Transactional(readOnly=true)
@Query("select u from VDuchadian u where u.name like '%?1%'")
Page<VDuchadian> findByKeyWords(@Param("keywords") String keywords,Pageable pageable);
}
其中findByKeyWords是我写的模糊查询的接口
经过多次测试发现将模糊查询语句中like后面的单引号去掉就行了,但是sql语句中like后面是要加单引号的,可能这是spring boot中的一种设计吧,后面有待深入研究