欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis插件實現(xiàn)自定義改寫表名實例代碼

 更新時間:2022年04月24日 11:51:13   作者:beiwangnull  
在數(shù)據(jù)庫操作過程中,經(jīng)常有修改表名的需求,下面這篇文章主要給大家介紹了關(guān)于mybatis插件實現(xiàn)自定義改寫表名的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

代碼如下:

@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;
        }
    }
}

總結(jié)

到此這篇關(guān)于mybatis插件實現(xiàn)自定義改寫表名的文章就介紹到這了,更多相關(guān)mybatis自定義改寫表名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JDK14性能管理工具之Jconsole的使用詳解

    JDK14性能管理工具之Jconsole的使用詳解

    JConsole是JDK自帶的管理工具,在JAVA_HOME/bin下面,直接命令JConsole即可開啟JConsole。接下來通過本文給大家分享JDK14性能管理工具之Jconsole的使用,感興趣的朋友一起看看吧
    2020-05-05
  • MyBatis-Plus條件構(gòu)造器Wrapper應(yīng)用實例

    MyBatis-Plus條件構(gòu)造器Wrapper應(yīng)用實例

    QueryWrapper是用于查詢的Wrapper條件構(gòu)造器,可以通過它來構(gòu)建SELECT語句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構(gòu)造器Wrapper,需要的朋友可以參考下
    2023-09-09
  • JAVA實現(xiàn)簡單停車場系統(tǒng)代碼

    JAVA實現(xiàn)簡單停車場系統(tǒng)代碼

    JAVA項目中正號需要一個停車收費系統(tǒng),就整理出來java實現(xiàn)的一個簡單的停車收費系統(tǒng)給大家分享一下,希望對大家有所幫助
    2017-04-04
  • Kotlin 基礎(chǔ)教程之異常

    Kotlin 基礎(chǔ)教程之異常

    這篇文章主要介紹了Kotlin 基礎(chǔ)教程之異常的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Java實現(xiàn)的日歷功能完整示例

    Java實現(xiàn)的日歷功能完整示例

    這篇文章主要介紹了Java實現(xiàn)的日歷功能,結(jié)合完整實例形式分析了Java日歷功能相關(guān)的日期時間獲取、計算、顯示等操作技巧,需要的朋友可以參考下
    2019-02-02
  • Java字符串定義及常用方法

    Java字符串定義及常用方法

    這篇文章主要介紹了Java字符串定義及常用方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • mybatis中#{}和${}的區(qū)別詳解

    mybatis中#{}和${}的區(qū)別詳解

    本文主要介紹了mybatis中#{}和${}的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Spring Event事件通知機制解讀

    Spring Event事件通知機制解讀

    這篇文章主要介紹了Spring Event事件通知機制解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java排序算法之冒泡排序的原理及優(yōu)化

    Java排序算法之冒泡排序的原理及優(yōu)化

    這篇文章主要介紹了Java排序算法之冒泡排序的原理及優(yōu)化,冒泡排序的思想很簡單,遍歷數(shù)組,比較相鄰的兩個元素,順序錯誤就把它們交換,直到整個數(shù)組排序完成,因為每經(jīng)過一趟排序,越小的元素會經(jīng)交換而慢慢“浮”到數(shù)列的頂端,因此叫做冒泡排序,需要的朋友可以參考下
    2023-11-11
  • spring系列筆記之常用注解

    spring系列筆記之常用注解

    這篇文章主要給大家介紹了關(guān)于spring系列筆記之常用注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論