简体   繁体   中英

MyBatis not working with Boolean mapping

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.

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM