MyBatis,插入复杂对象 [英] MyBatis, insert with complex object

查看:2834
本文介绍了MyBatis,插入复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

public class MyObj{
    private String myField_1
    private String myField_2
    private MyChildObj myChild
    // Constructor & get/set
}

public class MyChildObj{
    private String myField_3
    private String myField_4
    // Constructor & get/set
}

我用这种方式编写了插入内容:

on my Query.xml i wrote the insert in this way:

<insert id="insertMyObj" parameterType="MyObj">
    INSERT INTO MY_TABLE    (   FIELD_1,
                                FIELD_2,
                                FIELD_3,
                                FIELD_4)
    values  (   #{myField_1},
                #{myField_2},
                #{myField_3},
                #{myField_4},
    )
</insert>

阅读mybatis结果地图指南后,我尝试在mybatis-config.xml文件中添加以下行:

after reading mybatis Result Map Guide i tried to add following lines on mybatis-config.xml file:

<typeAliases>
    <typeAlias alias="MyObj"        type="myPackage.MyObj"/>
    <typeAlias alias="MyChildObj"   type="myPackage.MyChildObj"/>
</typeAliases>

<resultMap id="insertObj" type="MyObj">
    <result property="myField_1"  column="FIELD_1"/>
    <result property="myField_2"  column="FIELD_2"/>
    <association property="PrimaryKeyMap" resultMap="PrimaryKey"/>
</resultMap>

<resultMap id="PrimaryKeyMap" type="PrimaryKey">
    <result property=myField_3  column="FIELD_3"/>
    <result property="myField_4"  column="FIELD_4"/>
</resultMap>

但我一直收到以下错误:

but i keep getting the following error:

### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: xx; columnNumber: xx; Element type "resultMap" must be declared.

有谁能澄清我如何设置这个?

Can anyone clarify me how to set up this?

推荐答案

< association> 中的 resultMap 属性需要请参考结果映射的名称,而不是Java类型:< association property =MyChildObjectresultMap =PrimaryKeyMap/>

The resultMap attribute in <association> needs to refer to the name of your result map, not the Java type: <association property="MyChildObject" resultMap="PrimaryKeyMap"/>

但是,如果 MyChildObject 作为单独的表存储在数据库中,则嵌套插入不支持。您需要在Java中调用两个插入。 ResultMaps用于选择

However, if MyChildObject is stored in the database as a separate table, nested inserts are not supported. You will need to call both inserts in Java. ResultMaps are for selects.

如果您只是将一个表中的几列放在一个单独的对象中,那么您可以使用dot执行此操作符号, myChildObject.myField_4 。这样的事情:

If you are just putting a few columns from one table in a separate object, then you can do this with dot notation, myChildObject.myField_4. Something like this:

<insert id="insertMyObj" parameterType="MyObj">
  INSERT INTO MY_TABLE    (   FIELD_1,
                              FIELD_2,
                              FIELD_3,
                              FIELD_4)
  values  (   #{myField_1},
              #{myField_2},
              #{myChildObject.myField_3},
              #{myChildObject.myField_4},
  )
</insert>

这篇关于MyBatis,插入复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆