[mybatis] intercepts the sql statement to replace the specified field

I would like to ask you that when using mybatis, there is a need to replace some fields of dynamically generated sql according to a certain flag bit. For example, dao.query ("dao.TestSettingMapper.qryData", param)
this method normally executes sql is
"select A1 from tmp where id=001 a2 from tmp where id=001",
but when flag= "special" in param, the desired sql is
"select b1 talent b2 from tmp where id=001"
, so I think it is to use aop to intercept Grab the sql in the method interception to replace it. The code looks like this:

public Object invoke (MethodInvocation invocation) throws Throwable {

    
    Object[] arguments = invocation.getArguments();

    String sql_id = (String) arguments[0];//sql_id
    Map<String, Object> mParam = (Map<String, Object>) arguments[1];//
    //sql
    String sql = mSessionFactory.getConfiguration().getMappedStatement(sql_id)

.getboundSQL (mParam) .getSql ();

    if("special"){
    //
    ...
    }

}
the problem is that the sql I obtained through the getBoundSql method contains wildcards unexpectedly, so it doesn"t match the input parameters. I"d like to ask you how to convert the sql statement with wildcards into a way that mybatis can recognize, or is there a better way to intercept and replace fields in sql?

Apr.22,2022

if you want to modify the sql, you still need to deal with
reference address
https://blog.csdn.net/qq_2220...

when you want to modify the prepare.

but I think you can think about your current design, although interceptors can solve your current problems, but there is something strange about the way you implement it. See if there are other ways to solve your problem. Otherwise, people who look at the code later can not see the design through the code, but there is a hidden danger.

Menu