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?