mybatis插件实现自定义改写表名实例代码

网友投稿 386 2022-08-10


mybatis插件实现自定义改写表名实例代码

代码如下:

@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.chttp://lass, 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() {

//标识使用了该插件

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){

SQLUpcVIPcDeBwLdateStatement 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;

}

}

}

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:javaweb实现注册登录页面(javaweb登录)
下一篇:MyBatis拦截器动态替换表名的方法详解(mybatis拦截器获取表名)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~