有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JDBI bindBean找不到命名参数

我正在尝试使用JDBI更新Employee表中的Employee行。然而,bindBean方法似乎不喜欢我的bean。我包括了接球手和二传手。bean有一个公共的默认构造函数。对象的属性名与数据库列名完全匹配。例如,LastName字符串对应于LastName数据库列。完全匹配。我做错了什么?我是否误解了bindBean的工作原理?我还尝试了同样的代码,前面有一个前缀:parameters,仍然并没有骰子

编辑:经过进一步的研究,我认为问题的根源在于我的列名和属性以大写字母开头。不过,用@ColumnName和适当的大写列名注释我的getter和setter似乎没有帮助

已解决:此问题的简单解决方案是重命名查询本身中的命名参数,以匹配小写版本的属性名。i、 e如果属性名为Name,请将查询中的参数更改为:Name,这样问题就可以在不涉及bean或数据库列的情况下得到解决

Dao方法:

    @Override
public void updateEmployee(Employee empl){
    try(Handle handle = daoFactory.getDataSourceController().open()){
        handle.createUpdate("UPDATE Employees SET LastName = :LastName, FirstName = :FirstName, EmailAddress = :EmailAddress, OnVacation = :OnVacation, Active = :Active, EscalationLevel = :EscalationLevel," +
                " ScheduleExempt = :ScheduleExempt, GroupID = :GroupID, ScheduleID = :ScheduleID, SecurityGID = :SecurityGID, JobTitle = :JobTitle, Blurb = :Blurb WHERE IDX = :IDX")
                .bindBean(empl)
                .execute();
        handle.commit();
    }
    catch(Exception e){
        if(verbose){ e.printStackTrace(); }
        logger.logError("Web-EmployeeDaoService-E04", "Error updating single user in DB.");
    }
}

还有我的豆子:

package app.pojos.Employee;

import java.io.Serializable;
import java.sql.Timestamp;

public class Employee implements Serializable {
    private int IDX;
    private String LastName;
    private String FirstName;
    private String EmailAddress;
    private boolean OnVacation;
    private boolean Active;
    private int EscalationLevel;
    private boolean ScheduleExempt;
    private int GroupID;
    private int ScheduleID;
    private int SecurityGID;
    private String JobTitle;
    private String Blurb;
    private Timestamp LastSeen;
    private String ProfilePic;

    //Default constructor
    public Employee(){}

    //Data mapped getters and setters
    public int getIDX(){ return IDX; }
    public void setIDX(int IDX){ this.IDX = IDX; }

    public String getFirstName(){ return FirstName; }
    public void setFirstName(String firstName){ this.FirstName = firstName; }

    public String getLastName(){ return LastName; }
    public void setLastName(String lastName){ this.LastName = lastName; }

    public String getProfilePic(){ return ProfilePic; }
    public void setProfilePic(String ProfilePic){ this.ProfilePic = ProfilePic; }

    public String getEmailAddress(){ return EmailAddress; }
    public void setEmailAddress(String emailAddress){ this.EmailAddress = emailAddress; }

    public int getGroupID(){ return GroupID; }
    public void setGroupID(int GroupID){ this.GroupID = GroupID; }

    public boolean getScheduleExempt(){ return ScheduleExempt; }
    public void setScheduleExempt(boolean ScheduleExempt){ this.ScheduleExempt = ScheduleExempt; }

    public boolean getOnVacation(){ return OnVacation; }
    public void setOnVacation(boolean OnVacation){ this.OnVacation = OnVacation; }

    public boolean getActive(){ return Active; }
    public void setActive(boolean Active){ this.Active = Active; }

    public int getEscalationLevel(){ return EscalationLevel; }
    public void setEscalationLevel(int EscalationLevel){ this.EscalationLevel = EscalationLevel; }

    public int getScheduleID(){ return ScheduleID; }
    public void setScheduleID(int ScheduleID){ this.ScheduleID = ScheduleID; }

    public int getSecurityGID(){ return SecurityGID; }
    public void setSecurityGID(int SecurityGID){ this.SecurityGID = SecurityGID; }

    public String getJobTitle(){ return JobTitle; }
    public void setJobTitle(String JobTitle){ this.JobTitle = JobTitle; }

    public String getBlurb(){ return Blurb; }
    public void setBlurb(String Blurb){ this.Blurb = Blurb; }

    public Timestamp getLastSeen() { return LastSeen; }
    public void setLastSeen(Timestamp LastSeen) { this.LastSeen = LastSeen; }

    //Extra helper functions
    public String getFullName(){ return this.FirstName + " " + this.LastName; }
}

共 (1) 个答案

  1. # 1 楼答案

    已解决:此问题的简单解决方案是重命名查询本身中的命名参数,以匹配小写版本的属性名。i、 e如果属性名为Name,请将查询中的参数更改为:Name,这样问题就可以在不涉及bean或数据库列的情况下得到解决

    See this response for clarity。如果您和我一样,犯了违反最佳实践命名约定并将所有bean属性大写的错误,那么这是一个简单的解决方案。您只需更改在创建/更新/插入查询中引用属性的方式,而无需更改其他内容