IllegalArgumentException:找不到命名参数

IllegalArgumentException: Could not locate named parameter

我将 Map 作为以下方法的参数传递:

    public List<User> getByParameterOrAll(Map<String, String> paramsMap) {
    String email = null;
    String emailValue = null;
    String phone = null;
    String phoneValue = null;

    Iterator<Map.Entry<String, String>> entries = paramsMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        if (entry.getKey().equals("email")){
            email = entry.getKey();
            emailValue = entry.getValue();
        } else if (entry.getKey().equals("phone")){
            phone = entry.getKey();
            phoneValue = entry.getValue();
        }
    }

    List<User> users = em.createQuery("SELECT u FROM User u WHERE u.email=:emailValue AND u.phone=:phoneValue", User.class).
            setParameter(emailValue, email).
            setParameter(phoneValue, phone).
            getResultList();
    return users;
}

然后我抓住了这条线 "setParameter(emailValue, email)"

java.lang.IllegalArgumentException: Could not locate named parameter [gmail@gmail.com], expecting one of [phoneValue, emailValue]

我附上我的测试屏幕

您应该按以下方式更正您的查询(参见 documentation):

List<User> users = em.createQuery("SELECT u FROM User u WHERE u.email=:emailValue AND u.phone=:phoneValue", User.class).
            setParameter("emailValue", email).
            setParameter("phoneValue", phone).
            getResultList();