In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "mybatis plug-in how to achieve custom rewrite table name", in the daily operation, I believe that many people have doubts about how to achieve custom rewrite table name in mybatis plug-in. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to achieve custom rewrite table name in mybatis plug-in". Next, please follow the editor to study!
The code is as follows: @ 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 tableSet; public static boolean openInterceptor = false Public RerouteToTableInterceptor () {/ / logo uses the plug-in 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 At this point, the study on "how the mybatis plug-in implements custom rewriting table names" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.