java MyBatis,插入复杂对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31649089/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:55:10  来源:igfitidea点击:

MyBatis, insert with complex object

javamybatis

提问by Koop4

I have the following scenario:

我有以下场景:

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:

在我的 Query.xml 上,我以这种方式编写了插入内容:

<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>

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

阅读 mybatis Result Map Guide 后,我尝试在 mybatis-config.xml 文件中添加以下几行:

<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?

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

采纳答案by AngerClown

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

resultMapin的属性<association>需要引用结果映射的名称,而不是 Java 类型:<association property="MyChildObject" resultMap="PrimaryKeyMap"/>

However, if MyChildObjectis 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.

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

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:

如果您只是将一张表中的几列放在一个单独的对象中,那么您可以使用点表示法来做到这一点,myChildObject.myField_4. 像这样的东西:

<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>