Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I add arbitrary parameters using Interceptor? #3245

Closed
aki-caffeine opened this issue Sep 24, 2024 · 1 comment
Closed

Can I add arbitrary parameters using Interceptor? #3245

aki-caffeine opened this issue Sep 24, 2024 · 1 comment

Comments

@aki-caffeine
Copy link

aki-caffeine commented Sep 24, 2024

I created the following settings to add common parameters using Interceptor and reference them from SQL managed in xml.

【Interceptor】

@Intercepts({
      @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }),
})
@Component
public class MybatisAddParamInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler stHandler = StatementHandler.class.cast(invocation.getTarget());
        BoundSql boundSql = stHandler.getBoundSql();
        boundSql.setAdditionalParameter("key1", "value1");
        return invocation.proceed();
    }
}

【mapper interface】

@Mapper
public interface MapperInterface {
    int selectKey1(@Param("param") ParamObject param);
}

【xml】

<select id="selectKey1" resultType="int" parameterType="map">
        SELECT
             #{key1} 
        FROM table_A
</select>

However, although the system starts up successfully, the following error occurs when calling SQL.

Caused by: org.apache.ibatis.binding.BindingException: Parameter 'key1' not found. Available parameters are [param, param1]
	at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:210)
	at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:116)
	at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:225)
	at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:149)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)

I was hoping that parameter key1 would be registered using boundSql.setAdditionalParameter, but it has not been registered. How should I modify the Interceptor to make these work properly?

@harawata
Copy link
Member

Hello @aki-caffeine ,

The official answer is "no".
You might be able to achieve it using reflection, but we do not recommend it nor guarantee it will work in future version.

We do not use issues for questions.
Please post your next question to one of the following.

And when you ask, you should use more realistic example.
The SQL in your example seems useless and it makes it difficult for other developers to provide useful advice.

SELECT #{key1} FROM table_A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants