背景
mybatis中实现批量插入数据,我们可以使用Script脚本进行开发,需要用到foreach标签
代码
@Insert("<script>" +
"insert into book(category_id,name,level,price,img_path,create_time," +
"update_time) values" +
"<foreach collection='list' item='book' separator=','>" +
"(#{book.category_id},#{book.name},#{book.level},#{book.price}," +
"#{book.img_path}" +
",#{book.create_time},#{book.update_time})" +
"</foreach>" +
"</script>")
@Options(useGeneratedKeys = true,keyProperty = "id")
void batchInsert(List<Book> list);