ResultMap mybatis 不工作

ResultMap mybatis not working

我有这个简单的映射器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0 Powerhouse//EN" "http://mybatis.org/dtd/mybatis-3-Powerhouse-mapper.dtd">

<mapper namespace="nl.powerhouse.data.domain.CountryMapper">
<resultMap id="countryDsoMap" type="nl.powerhouse.domain.common.types.algemeen.Land">
    <id column="land_id"/>
    <association property="landId" columnPrefix="land_" column="id"/>
    <association property="landcode" columnPrefix="land_" column="landcode"/>
    <association property="landnaam" columnPrefix="land_" column="landnaam"/>
    <association property="landnummer" columnPrefix="land_" column="landnummer"/>
    <association property="handelsland" columnPrefix="land_" column="handelsland"/>
</resultMap>

<select id="findCountryByCode" resultMap="countryDsoMap">
    select <includeColumns tableAlias="land" columnPrefix="land_" refid="nl.powerhouse.data.dao.algemeen.LandMapper.Base_Column_List"/>
    from alg_t_land land
    where landcode = #{countryCode}
</select>

还有这个Land.javaclass

public final class Land implements Serializable {

    private LandId landId;
    private String landcode;
    private String landnaam;
    private String landnummer;
    private boolean handelsland;

    public Land() {
        // default constructor for mybatis
    }
    // getters...
}

但是,当我尝试使用它时,出现以下错误:

org.mybatis.spring.MyBatisSystemException: nested exception is    org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NullPointerException
### The error may exist in nl/powerhouse/data/domain/CountryMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
...
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)

我知道查询正在运行,问题出在 resultMap 上,有什么提示吗?

问题是您使用的是 <association> 标签而不是 <result>,这是用于 JavaBean 属性的标签,您可以在 MyBatis:[=17 的文档中看到=]

ResultMap

result – a normal result injected into a field or JavaBean property

association – a complex type association; many results will roll up into this

毫无疑问,您的映射不是复杂的类型关联,因此您必须使用 <result> 而不是使用 <association>

您的结果地图将是:

<resultMap id="countryDsoMap" type="nl.powerhouse.domain.common.types.algemeen.Land">
    <id column="land_id"/>
    <result property="landId" columnPrefix="land_" column="id"/>
    <result property="landcode" columnPrefix="land_" column="landcode"/>
    <result property="landnaam" columnPrefix="land_" column="landnaam"/>
    <result property="landnummer" columnPrefix="land_" column="landnummer"/>
    <result property="handelsland" columnPrefix="land_" column="handelsland"/>
</resultMap>