'CustomInterceptor to intercept @UpdateProvider linked method,but execute twice
I defined an interceptor to intercept the update method.The method body only prints the intercepted SQL.
The SQL is constructed through the buildupdatesql () method in @ updateprovider and internally created using "org. Apache. Ibatis. JDBC. SQL".
But the buildoupdatesql () method was executed twice.
The effect is shown in the figure below:
MyBatis version
3.5.7
Database vendor and version
MySQL 5.7.34-log
Expected result
buildUpdateSql() method only execute once.
Actual result
buildUpdateSql() method execute twice.
Kernel Code
@Repository
public interface UserMapper {
List<User> Sel();
@UpdateProvider(type = UserMapperProvider.class, method = "buildUpdateSql")
public int updateSubSite(int id);
public static class UserMapperProvider{
public String buildUpdateSql(int id) {
String sql = "update user set name='gfgfgfg' where id=#{id}";
System.out.println("构造的sql时:"+sql);
return sql;
}
}
}
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class CustomInterceptor implements Interceptor {
private Properties properties;
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
String sql = mappedStatement.getBoundSql(invocation.getArgs()[1]).getSql();
System.out.println("获取到SQL语句:" + sql);
}
}
@Configuration
public class MybatisConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@PostConstruct
public void addMySqlInterceptor() {
CustomInterceptor interceptor = new CustomInterceptor();
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

