简体   繁体   中英

Insert list of objects with MyBatis annotation

I know how to insert list of values with xml config mapper, but I want to do it with annotation.

Something like this, but working

@Insert("insert into  tableName (param1, param2) values (#{obj1.param1}, #{obj1.param2})")
void insert(@Param("obj1") List<MyObject> obj1);

I guess in XML you do it with foreach , then it is the same in annotation, just surround with <script> tags.

@Insert({"<script>", 
         "insert into  tableName (param1, param2) values ", 
         "<foreach collection='obj1' item='o' separator = ', ' open ="(" close=")" >#{o}</foreach>",
         "</script>"})
void insert(@Param("obj1") List<MyObject> obj1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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