广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis插件实现自定义改写表名实例代码
  • 203
分享到

mybatis插件实现自定义改写表名实例代码

2024-04-02 19:04:59 203人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

代码如下: @Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatem

代码如下:

@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
    @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),})
public class RerouteToTableInterceptor implements Interceptor {
    private Map map;
    private Set<String> tableSet;
    public static boolean openInterceptor = false;
    public RerouteToTableInterceptor() {
        //标识使用了该插件
        setOpenInterceptor(true);
    }
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];
        Boundsql boundSql = ms.getBoundSql(parameterObject);
        String sql = boundSql.getSql();
        MysqlStatementParser mysqlStatementParser = new MySqlStatementParser(sql);
        SQLStatement statement = mySqlStatementParser.parseStatement();
        SQLExprTableSource sqlTableSource = null;
        if(statement instanceof SQLSelectStatement){
            SQLSelect selectQuery = ((SQLSelectStatement)statement).getSelect();
            MySqlSelectQueryBlock sqlSelectQuery = (MySqlSelectQueryBlock)selectQuery.getQuery();
            sqlTableSource = (SQLExprTableSource)sqlSelectQuery.getFrom();
        }else if(statement instanceof SQLInsertStatement){
            SQLInsertStatement sqlInsertStatement = (SQLInsertStatement)statement;
            sqlTableSource = sqlInsertStatement.getTableSource();
        }else if(statement instanceof SQLUpdateStatement){
            SQLUpdateStatement sqlUpdateStatement = (SQLUpdateStatement)statement;
            sqlTableSource = (SQLExprTableSource)sqlUpdateStatement.getTableSource();
        }else if(statement instanceof SQLDeleteStatement){
            SQLDeleteStatement sqlUpdateStatement = (SQLDeleteStatement)statement;
            sqlTableSource = (SQLExprTableSource)sqlUpdateStatement.getTableSource();
        }
        String tableName = sqlTableSource.toString();
        if(tableSet.contains(tableName)){
            SQLIdentifierExpr sqlExpr = (SQLIdentifierExpr)sqlTableSource.getExpr();
            String newTableName = (String)map.get(tableName);
            if(!StringUtils.isEmpty(newTableName) && null != newTableName)
                sqlExpr.setName(newTableName);
        }
 
        BoundSql bs = new BoundSql(ms.getConfiguration(),statement.toString(),boundSql.getParameterMappings(),parameterObject);
 
        MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(bs));
        for (ParameterMapping mapping : boundSql.getParameterMappings()) {
            String prop = mapping.getProperty();
            if (boundSql.hasAdditionalParameter(prop)) {
                bs.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
            }
        }
        args[0] = newMs;
 
        return invocation.proceed();
    }
 
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
 
    @Override
    public void setProperties(Properties properties) {}
 
    public Map getMap() {
        return map;
    }
 
    public void setMap(Map map) {
        tableSet = map.keySet();
        this.map = map;
    }
    public boolean isOpenInterceptor() {
        return openInterceptor;
    }
 
    public void setOpenInterceptor(boolean openInterceptor) {
        this.openInterceptor = openInterceptor;
    }
 
    private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.geTKEyGenerator());
        if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
            builder.keyProperty(ms.getKeyProperties()[0]);
        }
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }
 
    public static class BoundSqlSqlSource implements SqlSource {
        private BoundSql boundSql;
        public BoundSqlSqlSource(BoundSql boundSql) {
            this.boundSql = boundSql;
        }
        public BoundSql getBoundSql(Object parameterObject) {
            return boundSql;
        }
    }
}

总结

到此这篇关于mybatis插件实现自定义改写表名的文章就介绍到这了,更多相关mybatis自定义改写表名内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: mybatis插件实现自定义改写表名实例代码

本文链接: https://www.lsjlt.com/news/147125.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作