SpringBoot集成mybatis攔截器修改表名

背景

公司的框架是基於mysql5.7開發的,最近有一個應用項目部署在linux系統上,使用的是mysql8.0,安裝時未開啓大小寫敏感忽略,客戶又不允許重裝mysql環境,導致一些框架代碼和業務代碼中表名使用大寫的地方會出現表名找不不到的情況,所以需要進行統一處理

自定義SQLAST適配器

自定義ASTVisitorAdapter對錶名進行修改

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

自定義Mybatis攔截器

通過BoundSql獲取sql語句,使用Druid的SQLUtils對sql語句進行結構化分析,表名修改完成後再重新複製sql語句

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

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        DataBaseInfoUtil dataBaseInfoUtil = SpringBootBeanUtil.getBean(DataBaseInfoUtil.class);
        if(dataBaseInfoUtil.checkOpenTableNameHandle()){
            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) {
                stmt.accept(visitor);
            }
            String handleSQL = SQLUtils.toSQLString(stmtList, JdbcConstants.MYSQL);

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

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

    @Override
    public void setProperties(Properties properties) {

    }

}

註冊自定義Myabits攔截器

@Configuration
public class FrameMyBatisPluginConfig {
    @Bean
    @Conditional({SQLTableNameHandleInterceptorCondition.class})
    public String SQLTableNameHandleInterceptor(SqlSessionFactory sqlSessionFactory) {
        //實例化插件
        SQLTableNameHandleInterceptor sqlTableNameHandleInterceptor = new SQLTableNameHandleInterceptor();
        //創建屬性值
        Properties properties = new Properties();
        properties.setProperty("prop1","value1");
        //將屬性值設置到插件中
        sqlTableNameHandleInterceptor.setProperties(properties);
        //將插件添加到SqlSessionFactory工廠
        sqlSessionFactory.getConfiguration().addInterceptor(sqlTableNameHandleInterceptor);
        return "interceptor";
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章