【问题标题】:ResultMap mybatis not workingResultMap mybatis 不工作
【发布时间】:2016-09-28 08:44:41
【问题描述】:

我有这个简单的映射器

<?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 上,有什么提示吗?

【问题讨论】:

  • 您的 id:&lt;id column="land_id"/&gt; 没有属性 property。我想您需要输入&lt;id column="land_id" property="landId"/&gt; 或属性ID 如何在Land 中调用
  • 我试过了,但也没有用
  • 否则,你为什么使用标签association?将 替换为 。示例:&lt;result property="handelsland" columnPrefix="land_" column="handelsland"/&gt;
  • 结果有效,谢谢!

标签: xml mybatis


【解决方案1】:

问题是您使用的是&lt;association&gt; 标签而不是&lt;result&gt;,这是用于JavaBean 属性的标签,您可以在MyBatis 的文档中看到:

结果图

result – 注入到字段或 JavaBean 属性中的正常结果

association——复杂类型的关联;许多结果将汇总 进入这个

明确地说,您的映射不是复杂类型关联,因此您必须使用&lt;result&gt;,而不是使用&lt;association&gt;

您的结果图将是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2016-07-23
    • 1970-01-01
    • 2014-01-30
    • 2012-11-10
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多