You shouldn't equally try to have all 1000 rows in a single query.Instead break it
into smaller sizes.
它强调,当插入数量很多时,不能一次性全放在一条语句里。可是为什么不能放在同一
Insert inside Mybatis foreach is not batch,this is a single (could become giant)SQL statement and that brings drawbacks: 2 some database such as Oracle here does not support. 3 in relevant cases: there will be a Large number of records to insert and the database configured limit (by default around 2000 parameters per statement)wi11 4 be hit, and eventually possibly DB stack error if the statement itself become too
7 Iteration over the collection must not be done in the mybatis XML. Just execute a simple Insertstatement in a Java Foreach 1oop. The most important thing is the session Executor type.
10 Unlike default ExecutorType.SIMPLE,the statement will be prepared once and executed for each record to insert.
从资料中可知,默认执行器类型为 Simple,会为每个语句创建一个新的预处理语句, 也就是创建一个 PreparedStatement 对象。在我们的项目中,会不停地使用批量插入这 个方法,而因为 MyBatis 对于含有<foreach>的语句,无法采用缓存,那么在每次调用 方法时,都会重新解析 sql 语句。
1 Internally,it still generates the same single insert statement with many placeholders as the JDBC code above. 2 MyBatis has an ability to cache PreparedStatement, but this statement cannot be cached because it contains<foreach /> 3 element and the statement varies depending on the parameters. As a result, MyBatis has to 1)evaluate the foreach part and 2) 4 parse the statement string to build parameter mapping [ 1 ] on every execution of this statement. 5 And these steps are relatively costly process when the statement string is big and contains many placeholders. 6 [ 1 ] simply put, it is a mapping between placeholders and the parameters.