MyBatis不使用布尔映射

时间:2014-12-19 15:02:31

标签: java java-ee persistence mybatis mybatis-generator

我只是想用Mybatis映射一个布尔值,但我遇到了问题。首先,我将向您展示所涉及的部分:

XML File:

<resultMap id="destinationTypeMap" type="DestinationTypeDTO">
        <result property="destinationTypeId" column="education_destination_type_id" javaType="java.lang.Long" jdbcType="NUMERIC"/>
        <result property="description" column="description" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result property="available" column="is_available" javaType="boolean" jdbcType="VARCHAR" typeHandler="BooleanHandler"/>
    </resultMap>

Java类:

public class DestinationTypeDTO {

    private long destinationTypeId;
    private String description;
    private boolean available;

    public long getDestinationTypeId() {
        return destinationTypeId;
    }

    public void setDestinationTypeId(long destinationTypeId) {
        this.destinationTypeId = destinationTypeId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

}

但是,我收到此错误日志:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'isAvailable' of '....DestinationTypeDTO@bbd76bf' with value 'true' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'isAvailable' in 'class ....DestinationTypeDTO'

我花了好几个小时试图找到发生了什么但没有成功。任何提示?

谢谢大家。

4 个答案:

答案 0 :(得分:4)

javaType="boolean"更改为java.lang.Boolean并指定property="available"

<result property="available" column="is_available" property="available" javaType="java.lang.Boolean" jdbcType="VARCHAR" typeHandler="BooleanHandler"/>

在您的班级中将private boolean available;更改为private Boolean isAvailable;并添加getter / setter

public void setIsAvailable(Boolean available) {
    this.available = available;
}

public Boolean getIsAvailable() {
    return available;
}

答案 1 :(得分:2)

更改你的setter,ibatis需要标准格式为pojo的布尔名: -

 public void setIsAvailable(boolean available) {
    this.available = available;
}

答案 2 :(得分:0)

您的数据库列名为&#34; is_available&#34;并且您的属性命名为&#34;可用&#34;这就是映射不起作用的原因,你有两个解决方案:

  1. 将列名更改为&#34;可用&#34;或更改属性的名称&#34; is_available&#34;(不适合Java)

  2. 在sql查询中使用&#34; is_available as available&#34;。

答案 3 :(得分:0)

使用

javaType="_boolean"

不是

javaType="boolean"

_boolean映射到booleanboolean映射到java.lang.Boolean

文档:http://www.mybatis.org/mybatis-3/configuration.html