mybatis批量插入時攔截器失效的原因
使用mybatis時,批量插入數據可能會導致攔截器失效。這是因為在執行批量插入時,mybatis會使用statementhandler執行更新,而不是executor。默認情況下,攔截器只攔截executor上的update方法,因此不會被觸發。
解決方法
要解決這個問題,需要在@intercepts注解中添加statementhandler.update方法的簽名,以確保攔截器可以攔截批量插入。修改后的攔截器如下:
@Component @Intercepts({ @Signature(type = Executor.class,method = "update",args = {MappedStatement.class, Object.class}), @Signature(type = StatementHandler.class,method = "update",args = {Statement.class}) }) public class MyBatisAutoFillPlugin implements Interceptor { // some code... }
登錄后復制