简体   繁体   English

MyBatis select语句,不带resultMap

[英]MyBatis select statement WITHOUT a resultMap

I'm trying to avoid using resultMap's in MyBatis (or preferably without XML altogether). 我试图避免在MyBatis中使用resultMap(或最好完全不使用XML)。

I have the following working: 我有以下工作:

     <select id="getAllSales" resultType="Sales">
            SELECT TOP 100
                ID as "id",
                PLANE as "plane",
                PLANE_TYPE as "plane_type"
        FROM SALES
     </select>

This maps to a domain object (POJO) without issue. 这将毫无问题地映射到域对象(PO​​JO)。 However, if I have a child domain object, there doesn't seem to be any info out there how to make this work. 但是,如果我有一个子域对象,那么似乎没有任何信息可以使它起作用。 For example: 例如:

<select id="getAllSales" resultType="Sales">
                SELECT TOP 100
                    ID as "id",
                    PLANE as "plane.type",
                    PLANE_TYPE as "plane.type.serial_num"
            FROM SALES
 </select>

This will not map with a resultMap="Sales" attribute (Sales object is the parent object and contains the "Plane" type or, more specifically a List type. 这将不会使用resultMap =“ Sales”属性进行映射(Sales对象是父对象,并且包含“ Plane”类型,或更具体地说是List类型。

Any suggestion or ideas on how to get away from using a resultMap within the XML file? 关于如何摆脱在XML文件中使用resultMap的任何建议或想法?

Even more desirable, would be to go away from XML files completely and use @ annotations in the interface. 更可取的是,完全摆脱XML文件并在界面中使用@批注。

Thank you. 谢谢。

Mybatis cannot guess an object property is a complex type if it is not "told" the relation is an association. 如果Mybatis不“告诉”关系是关联,则无法猜测其属性是否为复杂类型。 It can be done when defining the resultMap in XML. 可以在XML中定义resultMap时完成。

Concerning annotations, according to API documentation about @One : 关于注释,根据有关@One API文档

join mapping is not supported via the Annotations API. 注解API不支持联接映射。 This is due to the limitation in Java Annotations that does not allow for circular references 这是由于Java注释中的限制,不允许循环引用

Absence of join is SQL query is irrelevant here, but this is the same scenario. 缺少联接在这里与SQL查询无关,但这是相同的情况。

Annotations would be an option if you considered using nested select (why not with lazy loading). 如果您考虑使用嵌套选择(为什么不使用延迟加载),则可以选择注释。 But you scheme does not seem to require/allow it. 但是您的方案似乎并不需要/不允许它。

Eventually, I guess XML is the only way. 最终,我猜想XML是唯一的方法。

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

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