How to fill in parameters with Mybatis dynamic SQL through the Mybatis interceptor
courage0916 opened this issue · 2 comments
encountered a problem using mybatis dynamic SQL. The mybatis interceptor I wrote couldn't obtain the parameters for value passing for data filling, and the invoice. getArgs() [1] parameter layer contains an extra layer of objects of the defaultInsertStatementProvider type. I thought it was due to the latest version of mybatis, but after searching through a browser, I found that the defaultInsertStatementProvider is an object of mybatis dynamic SQL, That is to say, it may be a problem with mybatis dynamic SQL, so I am asking this question here,
My Maven version is 3.8.4
Mybatis is:
org. mybatis. spring. boot
mybatis spring boot starter
3.0.2
Mybatis.dynamic-sql is:
org. mybatis. dynamic sql
mybatis dynamic sql
1.4.1
The function I am doing here is to use interceptors to fill in some new parameters in SQL

@component
@Intercepts({@Signature(
type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class})})
public class MybatisGainInterceptor implements Interceptor {
private static final Logger log = LoggerFactory.getLogger(MybatisGainInterceptor.class);
// 关于该插件的配置
private final AllowUpdateTables allowUpdateTables;
public MybatisGainInterceptor(AllowUpdateTables allowUpdateTables) {
this.allowUpdateTables = allowUpdateTables;
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 对应上面类注解的args
final Object[] args = invocation.getArgs();
// 获取需要的MappedStatement对象
MappedStatement statement = (MappedStatement) args[0];
// 获取插入对象,即实体类如:SysUser
Object obj = args[1];
safe(statement.getSqlCommandType(), statement.getBoundSql(obj).getSql());
fillId(obj);
Answered in the cross posted stack overflow question.