java MyBatis 不使用布尔映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27568633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:00:35  来源:igfitidea点击:

MyBatis not working with Boolean mapping

javajakarta-eepersistencemybatismybatis-generator

提问by stack man

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:

我只是想用 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 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.

感谢大家。

回答by sol4me

Change javaType="boolean"to java.lang.Booleanand 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;to private Boolean isAvailable;and add getter/setter

在你的类变private boolean available;private Boolean isAvailable;和附加的getter / setter

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

public Boolean getIsAvailable() {
    return available;
}

回答by Panther

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

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

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

回答by cgab

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)

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

  1. 将列的名称更改为“可用”或将属性名称更改为“is_available”(不适用于 Java)

  2. 在 sql 查询中使用“is_available as available”。

回答by Mateusz Stefek

Use

利用

javaType="_boolean"

Not

不是

javaType="boolean"

_booleanmaps to boolean, booleanmaps 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