DynamicTableNameInnerInterceptor implements InnerInterceptor { protected String changeTable(String sql) { TableNameParser parser = new TableNameParser(sql); List<TableNameParser.SqlToken> names = new ArrayList<>(); parser.accept(names::add); StringBuilder builder = new StringBuilder(); int last = 0; for (TableNameParser.SqlToken name : names) { int start = name.getStart(); if (start != last) { builder.append(sql, last, start); String value = name.getValue(); TableNameHandler handler = tableNameHandlerMap.get(value); if (handler != null) { builder.append(handler.dynamicTableName(sql, value)); } else { builder.append(value); } } last = name.getEnd(); } if (last != sql.length()) { builder.append(sql.substring(last)); } return builder.toString(); }  @Override void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql); @Override void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout);  }
@Test @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"}) void doIt() { DynamicTableNameInnerInterceptor interceptor = new DynamicTableNameInnerInterceptor(); interceptor.setTableNameHandlerMap(newTableNameHandlerMap()); @Language("SQL") String origin = "SELECT * FROM t_user, t_user_role", replaced = "SELECT * FROM t_user_r, t_user_role"; assertEquals(replaced, interceptor.changeTable(origin)); origin = "SELECT * FROM t_user"; replaced = "SELECT * FROM t_user_r"; assertEquals(replaced, interceptor.changeTable(origin)); origin = "SELECT * FROM t_user"; replaced = "SELECT * FROM t_user_r"; assertEquals(replaced, interceptor.changeTable(origin)); origin = "SELECT * FROM t_user WHERE name = 't_user'"; replaced = "SELECT * FROM t_user_r WHERE name = 't_user'"; assertEquals(replaced, interceptor.changeTable(origin)); origin = "SELECT t_user.* FROM t_user_real t_user"; assertEquals(origin, interceptor.changeTable(origin)); }