简体   繁体   English

如何使用MyBatis批注在单个查询中插入多个值?

[英]How can I insert multiple values in a single query using MyBatis annotations?

Are there any examples for inserting a collection into a Database (MySQL) using a single query using MyBatis annotations and not the XML? 有没有使用MyBatis批注而不是XML使用单个查询将集合插入数据库(MySQL)的示例?

I have the following query in a MyBatis DAO. 我在MyBatis DAO中有以下查询。

  @Insert("insert into deleted_items(item_id) " + "values (#{itemID})")
 int put(String itemID);

I want to insert a List<String> using the same query as above, just allow for multiple values. 我想使用与上面相同的查询插入List<String> ,只允许使用多个值。

How can I do that using only annotations? 我该如何仅使用注释呢?

AFAIK, this is not possible using annotations. AFAIK,使用注释是不可能的。 Not sure about xml. 不确定xml。

Yes you can insert collection in database using MyBatis annotation Here is example 是的,您可以使用MyBatis注释在数据库中插入集合。这是示例

I have one user list and want to insert that list in database using MyBatis annotation without xml mapping 我有一个用户列表,并希望使用MyBatis批注在数据库中插入该列表而不使用xml映射

 @Insert({"<script>", 
        "insert into  user_master (first_name,last_name) values ",
        "<foreach collection='userList' item='user' index='index' open='(' separator = '),(' close=')' >#{user.first_name},#{user.last_name}</foreach>",
        "</script>"})
    int insertUserList(@Param("userList") List<UserNew> userList);

I successfully insert more then 25 record in my rest call using above insertUserList. 我使用上面的insertUserList成功在其余调用中插入了25条以上的记录。

I hope it will helpful to you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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