我正在尝试使用foreach循环在mybatis mapper xml中将作者列表插入到Author表中,如下所示,
AuthorVO.java
class AuthorVO {
private List<Author> authors;
...
}
Author.java
class Author{
private int id;
private String name;
private String level;
....
}
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>
这里我从get_next_id表中获取Author表的主键(id)。
现在我需要使用主键值更新author.id。 类似的东西:
#{author.id} = (select id from get_next_id where table_name="Author")
请告诉我将表中的id值复制到java pojo对象(Author.id)的语法或正确方法?
答案 0 :(得分:0)
尝试使用selectKey而不使用foreach并在java代码中循环收集:
<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>