繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM