JpaRepository 本机查询参数不起作用

JpaRepository native query parameter not working

jpaMainClass.java

package com.springjpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaMainClass {
    public static void main(String[] args) {
        try {
            SpringApplication.run(JpaMainClass.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

jpaController.java

package com.springjpa.controller;
import com.springjpa.entity.User;
import com.springjpa.repository.UserJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("jpa")
public class JpaController {
    @Autowired
    private UserJpaRepository userJpaRepository;

    @GetMapping("get")
    public User getUser(@RequestParam("name") String name) {
        System.out.println("Request get "+name);
        List<User> user = userJpaRepository.findUserByName(name);
        if (!user.isEmpty()){
            System.out.println("User List Contain Elements");
            return user.get(0);
        } else {
            System.out.println("User List is Empty");
        }
        return null;
    }
}

jpaRepository.java

package com.springjpa.repository;

import com.springjpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;

public interface UserJpaRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT * FROM user where name = ?1", nativeQuery = true)
    List<User> findUserByName(String name);
}

数据库

通过使用上面的结构,我尝试通过“名称”获取记录及其返回的空列表[失败]。

Hibernate: SELECT * FROM user where name = ?
2020-09-14 10:00:25.979 TRACE 4280 --- [nio-6060-exec-2] o.h.type.descriptor.sql.BasicBinder
: binding parameter [1] as [VARCHAR] - ['Nipuna']
User List is Empty

然后我尝试了以下查询,但也返回了相同的空列表 [Failed]。

public interface UserJpaRepository extends JpaRepository<User, Long> {
        @Query(value = "SELECT * FROM user where name = :name", nativeQuery = true)
        List<User> findUserByName(@Param("name") String name);
    }

所以我不确定参数 (:name, ?1) 并检查它我在 where 子句中硬编码名称如下,它正确返回响应 [通过] 但很难编码.

public interface UserJpaRepository extends JpaRepository<User, Long> {
        @Query(value = "SELECT * FROM user where name = 'Nipuna'", nativeQuery = true)
        List<User> findUserByName(String name);
    }

具有硬编码名称的请求和响应

根据您在此处提到的错误

2020-09-14 10:00:25.979 TRACE 4280 --- [nio-6060-exec-2] o.h.type.descriptor.sql.BasicBinder
: binding parameter [1] as [VARCHAR] - ['Nipuna']

您传递查询字符串的方式似乎有误。所以你可能会像这样传递它,

<BASE_URL_WITH_PATHS>?name='Nipuna'

如果是,请按如下方式尝试,

<BASE_URL_WITH_PATHS>?name=Nipuna

您不需要在引号内传递查询字符串。

刚调试了一下代码,发现跟请求参数有关系

  1. likeURL传递的请求参数 'http://localhost:8090/jpa/get?name=%27Don%20Joe%27' 被纳入 在这两种情况下,请求参数是否为“Don 乔”或简单地说是唐·乔。
  2. 使用的查询也考虑了引号,这导致无法检索用户详细信息。请通过请求 没有任何引号的参数,例如 http://localhost:8090/jpa/get?name=prakhar.

调试后的代码片段:-

Successful retrieval` endpoint get request