简体   繁体   English

如何使用MyBatis将Java保留字的数据库列名称映射到Java模型对象字段?

[英]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表: somethingshort

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. 现在,当我得到getStuffinsertStuff只有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.

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