mybatis攔截器在批量插入數(shù)據(jù)時(shí)失效的解決辦法
在mybatis中使用攔截器對(duì)數(shù)據(jù)進(jìn)行自動(dòng)填充時(shí),如果批量插入時(shí)攔截器失效,原因可能是不當(dāng)?shù)臄r截方法簽名。默認(rèn)情況下,攔截器只攔截executor對(duì)象上的“update”方法,而不攔截statementhandler對(duì)象上的“update”方法,這會(huì)導(dǎo)致批量插入時(shí)無(wú)法生效。
解決辦法是修改攔截器的注解,同時(shí)攔截executor和statementhandler對(duì)象上的“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 { // 原有代碼省略 }
登錄后復(fù)制
通過(guò)修改攔截器簽名,將statementhandler對(duì)象上的“update”方法也納入攔截范圍,即可確保批量插入時(shí)也能觸發(fā)攔截器,從而實(shí)現(xiàn)自動(dòng)填充功能。