Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何在spring boot jpa中将%searchKey%等转换为本机查询_Spring Boot_Jpa_Nativequery - Fatal编程技术网

Spring boot 如何在spring boot jpa中将%searchKey%等转换为本机查询

Spring boot 如何在spring boot jpa中将%searchKey%等转换为本机查询,spring-boot,jpa,nativequery,Spring Boot,Jpa,Nativequery,我试图将以下查询转换为本机查询,当查询返回2个元组时,我得到的是空列表 以下是查询: SELECT s.id AS shopID, s.shop_name AS shopName FROM shop s WHERE s.shop_name LIKE '%store%' ; 下面是我创建的方法,该方法返回空列表,而它应该发送包含两个对象的列表 (这是m存储库的方法) 依赖关系与数据库相关 <dependency> <gr

我试图将以下查询转换为本机查询,当查询返回2个元组时,我得到的是空列表

以下是查询:

SELECT 
  s.id AS shopID,
  s.shop_name AS shopName 
FROM
  shop s 
WHERE s.shop_name LIKE '%store%' ;
下面是我创建的方法,该方法返回空列表,而它应该发送包含两个对象的列表 (这是m存储库的方法)

依赖关系与数据库相关

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

org.springframework.boot
弹簧靴启动器jdbc
org.springframework.boot
spring引导启动器数据jpa
mysql
mysql连接器java
8.0.21

我怀疑
%:searchKey%
在本机查询中的组合,我认为
%
必须是参数的一部分

@Query(value = "SELECT \n" +
            "  s.id AS shopID,\n" +
            "  s.shop_name AS shopName \n" +
            "FROM\n" +
            "  shop s \n" +
            "WHERE s.shop_name LIKE :searchKey", nativeQuery = true)
List<Object[]> buyerDashboardSearchSuggestion_internal(@Param("searchKey") String searchKey);

default List<Object[]> buyerDashboardSearchSuggestion(String searchKey) {
    return buyerDashboardSearchSuggestion_internal("%"+searchKey+"%");
    //todo escape special chars like % and _ in "searchKey"
}
@Query(value=“SELECT\n”+
s.id作为shopID,\n+
“s.shop\u name作为shopName\n”+
“来自\n”+
“商店s\n”+
“WHERE s.shop_name LIKE:searchKey”,nativeQuery=true)
List buyerDashboardSearchSuggestion_internal(@Param(“searchKey”)字符串searchKey);
默认列表buyerDashboardSearchSuggestion(字符串搜索键){
返回buyerDashboardSearchSuggestion\u内部(“%”+搜索键+“%”);
//todo在“searchKey”中转义特殊字符,如%和u
}

您缺少围绕LIKE和围绕百分号的单引号。是否类似于“WHERE s.shop_name”“LIKE”“%:searchKey%”?我尝试了这个“WHERE s.shop_name LIKE”“:searchKey%”,但仍然得到了一个0个本应有效的对象列表。查看您的参数,是否区分大小写?是的,但即使我维护大小写,如“Store”,也会得到空列表。
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
@Query(value = "SELECT \n" +
            "  s.id AS shopID,\n" +
            "  s.shop_name AS shopName \n" +
            "FROM\n" +
            "  shop s \n" +
            "WHERE s.shop_name LIKE :searchKey", nativeQuery = true)
List<Object[]> buyerDashboardSearchSuggestion_internal(@Param("searchKey") String searchKey);

default List<Object[]> buyerDashboardSearchSuggestion(String searchKey) {
    return buyerDashboardSearchSuggestion_internal("%"+searchKey+"%");
    //todo escape special chars like % and _ in "searchKey"
}