溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在SpringBoot中利用MybatisSQL對(duì)@Intercepts進(jìn)行過(guò)濾

發(fā)布時(shí)間:2021-04-16 17:17:30 來(lái)源:億速云 閱讀:546 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在SpringBoot中利用MybatisSQL對(duì)@Intercepts進(jìn)行過(guò)濾,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

開(kāi)發(fā)環(huán)境:

spring boot + mybatis

@Component
@Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })
public class SqlInterceptor implements Interceptor {

  private Logger logger = LoggerFactory.getLogger(SqlInterceptor.class);

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
 logger.info("Interceptor......");
//    獲取sql
 MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
 Object parameter = invocation.getArgs()[1];
 BoundSql boundSql = mappedStatement.getBoundSql(parameter);
 String oldsql = boundSql.getSql();
 logger.info("old:"+oldsql);

//    判斷sql是否有where條件。改變sql。
 boolean status = oldsql.toLowerCase().contains("where");

 if (status) {
   BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), oldsql + " and 1=1",
       boundSql.getParameterMappings(), boundSql.getParameterObject());
   MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
   invocation.getArgs()[0] = newMs;
 }
// 繼續(xù)執(zhí)行
 Object result = invocation.proceed();
 return result;
  }

  @Override
  public Object plugin(Object target) {
 return Plugin.wrap(target, this);
  }

  @Override
  public void setProperties(Properties properties) {

  }

  // 復(fù)制原始MappedStatement
  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) {
   for (String keyProperty : ms.getKeyProperties()) {
     builder.keyProperty(keyProperty);
   }
 }
 builder.timeout(ms.getTimeout());
 builder.parameterMap(ms.getParameterMap());
 builder.resultMaps(ms.getResultMaps());
 builder.cache(ms.getCache());
 builder.useCache(ms.isUseCache());
 return builder.build();
  }

  public static class BoundSqlSqlSource implements SqlSource {
 BoundSql boundSql;

 public BoundSqlSqlSource(BoundSql boundSql) {
   this.boundSql = boundSql;
 }

 public BoundSql getBoundSql(Object parameterObject) {
   return boundSql;
 }
  }

}

上述就是小編為大家分享的怎么在SpringBoot中利用MybatisSQL對(duì)@Intercepts進(jìn)行過(guò)濾了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI