MyBatis not working with Boolean mapping

给你一囗甜甜゛ 提交于 2019-12-13 12:30:01

问题


I am just trying to map a boolean value with Mybatis, but I am having a problem. Firstly, I'll show you the parts involved:

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

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

}

But, I am getting this error log:

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'

I spent hours trying to find what's going on but without success. Any hint?

Thanks everyone.


回答1:


Change javaType="boolean" to java.lang.Boolean and specify property="available"

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

In your class change private boolean available; to private Boolean isAvailable; and add getter/setter

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

public Boolean getIsAvailable() {
    return available;
}



回答2:


Change your setter , ibatis expects boolean name with standard format of pojo:-

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



回答3:


Your database column is named "is_available" and your attribute is named "available" that's why the mapping is not working, you have two solutions for this:

  1. Change the name of the column to "available" or change the name of the attribute"is_available"(not ok for Java)

  2. In the sql query use "is_available as available".




回答4:


Use

javaType="_boolean"

Not

javaType="boolean"

_boolean maps to boolean, boolean maps to java.lang.Boolean.

Documentation: http://www.mybatis.org/mybatis-3/configuration.html



来源:https://stackoverflow.com/questions/27568633/mybatis-not-working-with-boolean-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!