0

我正在尝试使用 JDBI 更新 Employee 表中的 Employee 行。但是,bindBean 方法似乎不喜欢我的 bean。我已经包括了 getter 和 setter。bean 有一个公共的默认构造函数。对象的属性名称与数据库列名称完全匹配。例如,LastName String 对应于 LastName 数据库列。完全符合。我在这里做错了什么?我是否误解了 bindBean 的工作原理?我还尝试了在 :parameters 前面添加前缀的相同代码,但仍然没有骰子。

编辑:经过更多研究,我相信问题出在我的列名和属性以大写字母开头的事实。用 @ColumnName 和适当的大写列名注释我的 getter 和 setter 似乎没有帮助。

已解决:此问题的简单解决方案是重命名查询本身中的命名参数以匹配属性名称的小写版本。即,如果属性被称为名称,将查询中的参数更改为:名称,问题就解决了,而无需触及您的 bean 或数据库列。

道法:

    @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; }
}
4

1 回答 1

2

已解决:此问题的简单解决方案是重命名查询本身中的命名参数以匹配属性名称的小写版本。即,如果属性被称为名称,将查询中的参数更改为:名称,问题就解决了,而无需触及您的 bean 或数据库列。

为清楚起见,请参阅此回复。如果您像我一样犯了违反最佳实践命名约定并将所有 bean 属性大写的错误,那么这是一个简单的解决方案。您只需要更改在创建/更新/插入查询中引用属性的方式即可。

于 2021-02-14T16:49:30.337 回答