注册拦截器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 注册mybatis拦截器
*
* @author LISHUANG
* @date 2019/12/2
*/
@Configuration
public class MybatisConfiguration {

@Bean
public TestInterceptor testInterceptor() {
return new TestInterceptor();

}
}

重写SqlSource

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;

/**
* 内部辅助类,作用是包装sql
*
* @author LISHUANG
* @date 2019/11/28
*/
public class BoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;

public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}


@Override
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}

自定义mybatis拦截(示例只拦截query方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.util.StringUtils;

import java.util.Properties;

/**
* 实现自定义的sql
*
* @author LISHUANG
* @date 2019/11/28
*/
@Intercepts(@Signature(
type = Executor.class, method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
)
@Slf4j
public class TestInterceptor implements Interceptor {

@Override
public Object intercept(Invocation invocation) throws Throwable {
this.queryInvocation(invocation);
// 责任传递
return invocation.proceed();
}

@Override
public Object plugin(Object target) {

return Plugin.wrap(target, this);

}

@Override
public void setProperties(Properties properties) {

}


/**
* 查询语句拦截
*
* @param invocation
*/
private void queryInvocation(Invocation invocation) {
log.info("进入自定义query拦截器");

// 获取MappedStatement
MappedStatement arg = (MappedStatement) invocation.getArgs()[0];

// 获取sql
SqlSource sqlSource = arg.getSqlSource();
Object param = invocation.getArgs()[1];
BoundSql boundSql = sqlSource.getBoundSql(param);
String sql = boundSql.getSql();
if (StringUtils.isEmpty(sql)) {
return;
}

// 创建一个新的 MappedStatement
MappedStatement newMappedStatement = this.copyFromMappedStatement(arg, new BoundSqlSqlSource(boundSql));

MetaObject metaObject = MetaObject.forObject(newMappedStatement,
new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),
new DefaultReflectorFactory());
metaObject.setValue("sqlSource.boundSql.sql", this.newSql(sql));
invocation.getArgs()[0] = newMappedStatement;
}

/**
* 创建新的MappedStatement
*
* @param ms
* @param newSqlSource
* @return
*/
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(),
newSqlSource, ms.getSqlCommandType());

builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}


/**
* 自定义sql
*
* @param sql
* @return java.lang.String
*/
public String newSql(String sql) {
log.info("封装前的sql: {}", sql);
sql += " limit 1";
log.info("封装后的sql: {}", sql);
return sql;
}
}