微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JDBI bindBean 无法找到命名参数

如何解决JDBI bindBean 无法找到命名参数

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

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

解决:此问题的简单解决方案是重命名查询本身中的命名参数以匹配属性名称的小写版本。即,如果该属性名为 Name,则将查询中的参数更改为 :name,问题就解决了,而无需触及您的 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; }
}

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。