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
MyBatis not working with Boolean mapping
提问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.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;
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”,这就是映射不起作用的原因,您有两种解决方案:
Change the name of the column to "available" or change the name of the attribute"is_available"(not ok for Java)
In the sql query use "is_available as available".
将列的名称更改为“可用”或将属性名称更改为“is_available”(不适用于 Java)
在 sql 查询中使用“is_available as available”。
回答by Mateusz Stefek
Use
利用
javaType="_boolean"
Not
不是
javaType="boolean"
_boolean
maps to boolean
, boolean
maps to java.lang.Boolean
.
_boolean
映射到boolean
,boolean
映射到java.lang.Boolean
。
Documentation: http://www.mybatis.org/mybatis-3/configuration.html