mybatis攔截器在批量插入數(shù)據(jù)時失效的解決辦法
在mybatis中使用攔截器對數(shù)據(jù)進行自動填充時,如果批量插入時攔截器失效,原因可能是不當?shù)臄r截方法簽名。默認情況下,攔截器只攔截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”方法也納入攔截范圍,即可確保批量插入時也能觸發(fā)攔截器,從而實現(xiàn)自動填充功能。