public class CustomSQLInterceptor implements Interceptor {
@
Override public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
if (sql.contains("mobile =")) {
// 对 SQL 进行修改
sql = sql.replace("mobile =", "mobile in");
sql = sql.replace("'", "('");
sql = sql + "', '密文')";
// 使用新的 SQL
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, sql);
}
return invocation.proceed();
}
@
Override public Object plugin(Object target) {
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
}
return target;
}
@
Override public void setProperties(Properties properties) {
}
}