springboot + mybatis 拦截器

1.拦截器类

2.配置类

 

1. 拦截器类

import java.sql.Connection;
import java.util.Properties;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })//按需配置
public class MybatisInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		System.out.println("mybatis拦截器======");
		return invocation.proceed();
	}

	// 获取代理对象
	@Override
	public Object plugin(Object o) {
		return Plugin.wrap(o, this);
	}

	// 设置代理对象的参数
	@Override
	public void setProperties(Properties properties) {
	}
}

2.配置类

import org.apache.ibatis.plugin.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
	@Bean
	public Interceptor getInterceptor(){
		return new MybatisInterceptor();
	}
}

 

你可能感兴趣的:(springBoot,mybatis,java)