question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CustomInterceptor to intercept @Provider linked method,but execute twice

See original GitHub issue

Bug 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拦截器

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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gfgfggf1988commented, Apr 11, 2022

@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.

fine,i will try another way to solve that.thx

0reactions
harawatacommented, Apr 8, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found