CustomInterceptor to intercept @Provider linked method,but execute twice
See original GitHub issueBug Report
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);
}
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
java - CustomInterceptor to intercept @UpdateProvider linked ...
I defined an interceptor to intercept the update method.The method body only prints the intercepted SQL. The SQL is constructed through the ...
Read more >Angular HTTP Interceptors : Multiple Interceptors and 6 Code ...
Interceptors are used in Angular to Intercept HttpRequest/HttpResponse . We can use interceptors to log HttpRequest/HttpResponse ...
Read more >Method interceptor loaded and executed twice. - Google Groups
Hi, I have a strange problem. My AOP method interceptor executes twice. And I am not able to figure out why. Any leads...
Read more >[Question] In this sample the interceptor is called twice. #1020
Hi,. I have a pretty simple sample of code (based on a gist I saw here) that intercepts the HttpURLConnection#getRequestMethod method, but ......
Read more >Coding Filters and Interceptors for your RESTFul services
PreMatching filters are request filters that are executed before the request matching is started. The advantage of PreMatching request filters ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
fine,i will try another way to solve that.thx
@gfgfggf1988 ,
I understand what you want, but
ProviderSqlSource#getBoundSql()
subsequently calls the provider method, so there is nothing we can do.Also note that modifying SQL using interceptor is not officially supported. Some users use reflection to do that, but I’m not sure if it’s possible with
ProviderSqlSource
.If you need a help with writing complex interceptor, try posting a question to the mailing list or StackOverflow explaining what specifically you want to achieve.
Closing as it is not a bug.