iBatis无法映射列

时间:2011-01-22 21:37:36

标签: java sql ibatis

当我使用iBatis映射POJO时,如下所示:

Person {
    String firstName
    String lastName;
}

查询:

select firstName, last_name from Person

我发现Person.firstName正确填充而Person.lastName为null - 这是静默发生的。我可以引入一个ResultMap来修复null问题(通过翻译last_name - > lastName),但是我希望iBatis在尝试映射时抛出错误,而不是在静默地做到最好...

有谁知道我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以尝试aliasing with AS

SELECT firstName, last_name AS lastName
FROM   Person

答案 1 :(得分:0)

我看到了几个问题。

我是盲人!

班级Person { String firstName String lastName; } 为iBatis提供了完全0个可见元素,因为iBatis既不扩展Person也不共享Person包。请考虑以下事项:

public class Person
{
    private String firstName;
    private String lastName;

    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setFirstName(final String newValue)
    {
        firstName = newValue;
    }

    public void setLastName(final String newValue)
    {
        lastName = newValue;
    }
}

单独和不等于

last_name与lastName不同。
请考虑使用如下结果映射:

<resultMap id="theIdOfThisMap"
           class="Person">
    <result property="firstName" column="firstName"/>
    <result property="lastName" column="last_name"/>
</resultMap>

<select id="searchForEntities"
        resultMap="theIdOfThisMap">
    select
        firstName,
        last_name
    from
        PERSON
</select>