记一次Mybatis-Plus动态分表DynamicTableNameInnerInterceptor里无法动态替换表名的坑
首先上源码
protected String changeTable(String sql) { ExceptionUtils.throwMpe(null == tableNameHandler, "Please implement TableNameHandler processing logic"); 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); builder.append(tableNameHandler.dynamicTableName(sql, name.getValue())); } last = name.getEnd(); } if (last != sql.length()) { builder.append(sql.substring(last)); } if (hook != null) { hook.run(); } return builder.toString(); }
仅支持一下关键词
所以如果使用动态表名替换功能时候使用:INSERT IGNORE 类似这样的语句 IGNORE 无法被识别,所以后面跟着的表名也无法识别