In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains how to change the table name dynamically with MyBatis interceptor. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to change the table name dynamically by MyBatis interceptor".
I. Mybatis Interceptor interceptor interface and comments
Simply put, when mybatis executes sql, it intercepts the target method and adds our business logic before and after. It is actually adding @ Intercepts annotations and implementing the org.apache.ibatis.plugin.Interceptor interface
@ Intercepts (@ Signature (method = "query", type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) public interface Interceptor {/ / mainly overrides this method and implements our business logic Object intercept (Invocation invocation) throws Throwable; / / generates a proxy object. Here you can determine whether to generate a proxy object Object plugin (Object target). / / if our interceptor needs to use some variable parameters, you can read void setProperties (Properties properties) here.
In the intercept method, there is a parameter Invocation object, which has three member variables corresponding to @ Signature
The member variable type describes the parameters required by the targetObject proxy object methodMethod intercepted method argsObject [] intercepted method execution
Through the args variable in Invocation. We can get the object MappedStatement (args [0]) and pass in the parameter Object (args [1]) of the sql statement. MappedStatement is an object that records sql statements (sqlSource objects), parameter value structure, return value structure, mapper configuration, and so on.
The sqlSource object and the parameter object Object of the incoming sql statement get BoundSql. BoundSql's toString method can get the placeholder sql statement, and our business logic can intervene here.
Get the SQL statement, replace the table name according to the rule, plug it back into the BoundSql object, and then plug the BoundSql object back into the MappedStatement object. Finally, assign a value to args [0] (the parameters required by the actual intercepted method) and it is done.
Third, code implementation of import org.apache.ibatis.executor.Executor;import org.apache.ibatis.mapping.BoundSql;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.mapping.SqlSource;import org.apache.ibatis.plugin.*;import org.apache.ibatis.session.ResultHandler;import org.apache.ibatis.session.RowBounds;import java.util.* / * * @ description: dynamically replace the table name interceptor * @ author: hinotoyk * @ created: 2022-04-19 * / method = "query" intercepts select methods, while method = "update" can intercept insert, update, delete methods @ 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 ReplaceTableInterceptor implements Interceptor {private final static Map TABLE_MAP = new LinkedHashMap () Static {/ / the table name is long first to avoid matching the replacement subset TABLE_MAP.put ("t_game_partners", "t_game_partners_test"); / / testing TABLE_MAP.put ("t_file_recycle", "t_file_recycle_other"); TABLE_MAP.put ("t_folder", "t_folder_other") TABLE_MAP.put ("t_file", "t_file_other");} @ Override public Object intercept (Invocation invocation) throws Throwable {Object [] args = invocation.getArgs (); / / get the MappedStatement object MappedStatement ms = (MappedStatement) args [0]; / / get the parameter object Object parameterObject = args [1] of the incoming sql statement; BoundSql boundSql = ms.getBoundSql (parameterObject) / / get the sql statement with placeholder String sql = boundSql.getSql (); System.out.println ("sql before intercept:" + sql) / / determine whether to replace the table name if (isReplaceTableName (sql)) {for (Map.Entry entry: TABLE_MAP.entrySet ()) {sql = sql.replace (entry.getKey (), entry.getValue ());} System.out.println ("sql after intercept:" + sql) / / regenerate a BoundSql object BoundSql bs = new BoundSql (ms.getConfiguration (), sql,boundSql.getParameterMappings (), parameterObject); / / regenerate a MappedStatement object MappedStatement newMs = copyMappedStatement (ms, new BoundSqlSqlSource (bs)) / / assign back to the parameters required by the actual execution method args [0] = newMs;} return invocation.proceed ();} @ Override public Object plugin (Object target) {return Plugin.wrap (target, this) } @ Override public void setProperties (Properties properties) {} / * determine whether the table name needs to be replaced * @ param sql * @ return * / private boolean isReplaceTableName (String sql) {for (String tableName: TABLE_MAP.keySet ()) {if (sql.contains (tableName)) {return true }} return false;} / * copy a new MappedStatement * @ param ms * @ param newSqlSource * @ return * / private MappedStatement copyMappedStatement (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 (String.join (",", ms.getKeyProperties ();} 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 () } / * MappedStatement constructor accepts SqlSource * to implement SqlSource interface, encapsulates BoundSql into * / public static class BoundSqlSqlSource implements SqlSource {private BoundSql boundSql; public BoundSqlSqlSource (BoundSql boundSql) {this.boundSql = boundSql;} @ Override public BoundSql getBoundSql (Object parameterObject) {return boundSql;}} 4. Run result
At this point, I believe you have a deeper understanding of "MyBatis interceptor how to dynamically replace table names". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.