mybatis插件實(shí)現(xiàn)自定義改寫(xiě)表名實(shí)例代碼
更新時(shí)間:2022年04月24日 11:51:13 作者:beiwangnull
在數(shù)據(jù)庫(kù)操作過(guò)程中,經(jīng)常有修改表名的需求,下面這篇文章主要給大家介紹了關(guān)于mybatis插件實(shí)現(xiàn)自定義改寫(xiě)表名的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(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)識(shí)使用了該插件 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插件實(shí)現(xiàn)自定義改寫(xiě)表名的文章就介紹到這了,更多相關(guān)mybatis自定義改寫(xiě)表名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus條件構(gòu)造器Wrapper應(yīng)用實(shí)例
QueryWrapper是用于查詢的Wrapper條件構(gòu)造器,可以通過(guò)它來(lái)構(gòu)建SELECT語(yǔ)句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構(gòu)造器Wrapper,需要的朋友可以參考下2023-09-09JAVA實(shí)現(xiàn)簡(jiǎn)單停車(chē)場(chǎng)系統(tǒng)代碼
JAVA項(xiàng)目中正號(hào)需要一個(gè)停車(chē)收費(fèi)系統(tǒng),就整理出來(lái)java實(shí)現(xiàn)的一個(gè)簡(jiǎn)單的停車(chē)收費(fèi)系統(tǒng)給大家分享一下,希望對(duì)大家有所幫助2017-04-04