mybatis攔截器在批量插入數據時失效的解決辦法
在mybatis中使用攔截器對數據進行自動填充時,如果批量插入時攔截器失效,原因可能是不當的攔截方法簽名。默認情況下,攔截器只攔截executor對象上的“update”方法,而不攔截statementhandler對象上的“update”方法,這會導致批量插入時無法生效。
解決辦法是修改攔截器的注解,同時攔截executor和statementhandler對象上的“update”方法,代碼如下:
@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 { // 原有代碼省略 }
登錄后復制
通過修改攔截器簽名,將statementhandler對象上的“update”方法也納入攔截范圍,即可確保批量插入時也能觸發攔截器,從而實現自動填充功能。