[英]How to map a DB column name that is a Java reserved word to a Java model object field using MyBatis?
So I have a table CoolTable
with 2 columns: something
and short
. 所以我有一个带有2列的
CoolTable
表: something
和short
。
And my model class named CoolClass
reflects it with 2 fields: something
and _short
. 我的模型类
CoolClass
反映了两个字段: something
和_short
。 short
is a Java reserved keyword, so the field had to be prefixed with the underscore. short
是Java保留的关键字,因此该字段必须以下划线作为前缀。
Now my mybatis mapper XML uses CoolClass
like so: 现在,我的
CoolClass
映射器XML使用CoolClass
就像这样:
<select id="getStuff" resultType="CoolClass">
SELECT * FROM CoolTable
</select>
<insert id = "insertStuff" parameterType = "CoolClass">
INSERT INTO CoolTable (something, short)
VALUES (#{something}, #{short})
</insert>
Now, when I getStuff
and insertStuff
only something
column gets retrieved and inserted. 现在,当我得到
getStuff
和insertStuff
只有something
列被检索和插入。 short
is always null. short
始终为null。
I searched through all the MyBatis documentation but couldn't find anything relevant to help with this case. 我搜索了所有MyBatis文档,但找不到与该案例有关的任何相关信息。
How can I go about mapping short
column to _short
field of class? 如何将
short
列映射到类的_short
字段?
Fixed it by using a ResultMap like so: 通过使用ResultMap修复它,如下所示:
<resultMap id="coolResultMap" type="CoolClass">
<result property="_short" column="short"/>
</resultMap>
<select id="getStuff" resultMap="coolResultMap">
SELECT * FROM CoolTable
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.