mybatis / mybatis-dynamic-sql

SQL DSL (Domain Specific Language) for Kotlin and Java. Supports rendering for MyBatis or Spring JDBC Templates
http://www.mybatis.org/mybatis-dynamic-sql/docs/introduction.html
Apache License 2.0
1.09k stars 213 forks source link

How to fill in parameters with Mybatis dynamic SQL through the Mybatis interceptor #679

Closed courage0916 closed 1 year ago

courage0916 commented 1 year ago

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

微信图片_20231017093720
courage0916 commented 1 year ago

@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);
jeffgbutler commented 1 year ago

Answered in the cross posted stack overflow question.