[英]How to assign the result of a SELECT query to java object in mybatis?
我正在嘗試使用如下所示的foreach循環將作者列表插入mybatis mapper xml中的Author表中,
作者VO.java
class AuthorVO {
private List<Author> authors;
...
}
作者.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)的語法或正確方法嗎?
嘗試在沒有foreach的情況下使用selectKey並在您的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.