ResultMap mybatis无法正常工作

时间:2016-09-28 08:44:41

标签: xml mybatis

我有这个简单的映射器

<?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.java类

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,任何提示?

1 个答案:

答案 0 :(得分:1)

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

  

的ResultMap

     

结果 - 注入字段或JavaBean属性的正常结果

     

关联 - 复杂类型关联;许多结果将会卷起来   进入这个

确切地说,您的映射不是复杂的类型关联,因此您必须使用<association>而不是使用<result>

您的结果图将是:

<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>