mybatis插入带有列表的嵌套pojo

时间:2019-06-12 10:01:35

标签: java spring-boot mybatis

我在将这些信息插入数据库的地方嵌套了pojos。我正在使用mybatis插入数据库。

public class Student {

    private List<Bike> bikes;
    private long lastInsertId;

    //getters and setters
}

public class Bike {
   private String name;
   private List<Key> bikeKeys;

   //getters and setters
}

public class Key {

   private String id;
   private String name;

   //getters and setters
}

此后,我在mybatis映射器文件中插入如下内容,该文件不起作用。

<insert id="insertDetails" parameterType="com.media.domain.Student">
    <foreach item="bike" index="index" collection="bikes">
       <foreach item="bkItem" index="index" collection="bikeKeys">
        INSERT INTO mapping(key,id,name,keyname) VALUES
        (#{lastInsertId, jdbcType=INTEGER}, #{bkItem.id, jdbcType=VARCHAR}, #{bike.name, jdbcType=VARCHAR}, #{bkItem.name, jdbcType=VARCHAR});
       </foreach>
    </foreach>
</insert>

对于上面的插入语句,我的错误越来越小。

java.util.concurrent.CompletionException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'bikeKeys' in 'class com.media.domain.Student'

我可以寻求帮助吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

对我有用的解决方案

<insert id="insertDetails" parameterType="com.media.domain.Student">
<foreach item="bike" index="index" collection="bikes">
   <foreach item="bkItem" index="index" collection="bike.bikeKeys">
    INSERT INTO mapping(key,id,name,keyname) VALUES
    (#{lastInsertId, jdbcType=INTEGER}, #{bkItem.id, jdbcType=VARCHAR}, #{bike.name, jdbcType=VARCHAR}, #{bkItem.name, jdbcType=VARCHAR});
   </foreach>
</foreach>