Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将SELECT查询的结果分配给mybatis中的java对象?_Java_Mybatis - Fatal编程技术网

如何将SELECT查询的结果分配给mybatis中的java对象?

如何将SELECT查询的结果分配给mybatis中的java对象?,java,mybatis,Java,Mybatis,我试图使用foreach循环将作者列表插入mybatis mapper xml中的Author表,如下所示 AuthorVO.java class AuthorVO { private List<Author> authors; ... } Author.xml <insert id="insertAuthor(authorVO)"> <foreach item="author" index="index" collection="authors"> I

我试图使用foreach循环将作者列表插入mybatis mapper xml中的Author表,如下所示

AuthorVO.java

class AuthorVO {
private List<Author> authors;
...
} 
Author.xml

<insert id="insertAuthor(authorVO)">
<foreach item="author" index="index" collection="authors">

INSERT into Author (id, name, level)
values
(
(select id from get_next_id where table_name="Author"),
#{author.name},
#{author.level}
)
</insert>

<!-- Need to copy the id value into the author object (author.id) -->

</foreach>

请告诉我将id值从表复制到JavaPOJO对象(Author.id)的语法或正确方法

尝试在java代码中使用selectKey而不使用foreach并循环遍历集合:

<insert id="insertAuthor(authorVO)">
    <selectKey keyProperty="author.id" keyColumn="id" resultType="int" order="BEFORE">
        select id from get_next_id where table_name="Author"
    </selectKey>

    INSERT into Author (id, name, level)
    values
    (
         #{author.id},
         #{author.name},
         #{author.level}
    )
</insert>

从get_next_id where table_name=“Author”中选择id
插入作者(id、姓名、级别)
价值观
(
#{author.id},
#{author.name},
#{作者级别}
)

在内部不受支持。还有别的办法吗?好的,很抱歉。我更新了我的答案,建议您在java中对集合进行多次迭代,以调用insert,而不是使用foreach,因为foreach似乎是为构建sql语句而设计的,而不是多次执行语句
#{author.id} = (select id from get_next_id where table_name="Author")
<insert id="insertAuthor(authorVO)">
    <selectKey keyProperty="author.id" keyColumn="id" resultType="int" order="BEFORE">
        select id from get_next_id where table_name="Author"
    </selectKey>

    INSERT into Author (id, name, level)
    values
    (
         #{author.id},
         #{author.name},
         #{author.level}
    )
</insert>