SpringBoot integrates mybatis interceptor to modify table name



The company’s framework is developed based on mysql5.7. Recently, an application project was deployed on a linux system, using mysql8.0. Case sensitivity was not enabled during installation, and the customer was not allowed to reinstall the mysql environment, resulting in some frameworks Where the table name is capitalized in the code and business code, the table name cannot be found, so it needs to be handled uniformly

Custom SQLAST adapter

Customize ASTVisitorAdapter to modify the table name

public class MySqlExportTableAliasVisitor extends MySqlASTVisitorAdapter {
    public boolean visit(SQLExprTableSource x) {
        SystemConfig systemConfig = SpringBootBeanUtil.getBean(SystemConfig.class);
        }else if(systemConfig.getDbTableNameProxy().equals("upcase")){
        return true;

Customize Mybatis interceptor

Obtain the sql statement through BoundSql, use Druid’s SQLUtils to perform structural analysis on the sql statement, and then re-copy the sql statement after the table name is modified

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class SQLTableNameHandleInterceptor implements Interceptor {

    public Object intercept(Invocation invocation) throws Throwable {
        DataBaseInfoUtil dataBaseInfoUtil = SpringBootBeanUtil.getBean(DataBaseInfoUtil.class);
            StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
            MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
            BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");

            String sql = boundSql.getSql();
            List<SQLStatement> stmtList  = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
            MySqlExportTableAliasVisitor visitor = new MySqlExportTableAliasVisitor();
            for (SQLStatement stmt : stmtList) {
            String handleSQL = SQLUtils.toSQLString(stmtList, JdbcConstants.MYSQL);

            metaStatementHandler.setValue("delegate.boundSql.sql", handleSQL);
        return invocation.proceed();

    public Object plugin(Object target) {
        return Plugin.wrap(target, this);

    public void setProperties(Properties properties) {



Register a custom Myabits interceptor

public class FrameMyBatisPluginConfig {
    public String SQLTableNameHandleInterceptor(SqlSessionFactory sqlSessionFactory) {
        //Instantiate the plugin
        SQLTableNameHandleInterceptor sqlTableNameHandleInterceptor = new SQLTableNameHandleInterceptor();
        //Create attribute value
        Properties properties = new Properties();
        //Set the attribute value to the plugin
        //Add the plugin to the SqlSessionFactory factory
        return "interceptor";