您好,登錄后才能下訂單哦!
這篇文章主要介紹“mybatis插件怎么實(shí)現(xiàn)自定義改寫表名”,在日常操作中,相信很多人在mybatis插件怎么實(shí)現(xiàn)自定義改寫表名問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mybatis插件怎么實(shí)現(xiàn)自定義改寫表名”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
@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() { //標(biāo)識使用了該插件 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; } } }
到此,關(guān)于“mybatis插件怎么實(shí)現(xiàn)自定義改寫表名”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。