简体   繁体   English

MyBatis不使用布尔映射

[英]MyBatis not working with Boolean mapping

I am just trying to map a boolean value with Mybatis, but I am having a problem. 我只是想用Mybatis映射一个布尔值,但我遇到了问题。 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: 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;
    }

}

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" javaType="boolean"更改为java.lang.Boolean并指定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; 在你的类中更改private boolean available; to private Boolean isAvailable; to private Boolean isAvailable; and add getter/setter 并添加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:- 更改你的setter,ibatis期望bojo名称的标准格式为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: 您的数据库列名为“is_available”,并且您的属性名为“available”,这就是映射不起作用的原因,您有两个解决方案:

  1. Change the name of the column to "available" or change the name of the attribute"is_available"(not ok for Java) 将列的名称更改为“available”或更改属性“is_available”的名称(对Java不适用)

  2. In the sql query use "is_available as available". 在sql查询中使用“is_available as available”。

Use 使用

javaType="_boolean"

Not

javaType="boolean"

_boolean maps to boolean , boolean maps to java.lang.Boolean . _boolean映射到booleanboolean映射到java.lang.Boolean

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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