Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Created December 15, 2022 04:26
Show Gist options
  • Save A-pZ/95353ece0be26589e2e9033370fd23c3 to your computer and use it in GitHub Desktop.
Save A-pZ/95353ece0be26589e2e9033370fd23c3 to your computer and use it in GitHub Desktop.
パラメータのバインド前後を確認する設定
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.github.apz.sample.config.mybatis.SqlIdLoggingInterceptor"></plugin>
<plugin interceptor="com.github.apz.sample.config.mybatis.ParameterBindingInterceptor"></plugin>
</plugins>
</configuration>
package com.github.apz.sample.config.mybatis;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import java.sql.PreparedStatement;
@Intercepts({
@Signature(
type = ParameterHandler.class,
method = "setParameters",
args = {PreparedStatement.class}
)
})
@Slf4j
public class ParameterBindingInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
PreparedStatement preparedStatement = (PreparedStatement)invocation.getArgs()[0];
// バインド前
log.info("before: {}", preparedStatement.toString());
// MyBatisはパラメータをバインド
Object result = invocation.proceed();
// バインド後
log.info("after : {}", preparedStatement.toString());
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment