av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

SpringBoot整合MybatisSQL過濾@Intercepts的實現

瀏覽:7日期:2023-05-28 15:19:13

場景:

系統模塊查詢數據庫需要根據用戶的id去篩選數據。那么如果在 每個sql加user_id的過濾顯然不明確。所以要在查詢前將sql拼接上條件,做統一管理。

開發環境:

spring boot + mybatis

只需一個攔截類即可搞定(在看代碼前需要了解注解@Intercepts()):

@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; }// 繼續執行 Object result = invocation.proceed(); return result; } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } // 復制原始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過濾@Intercepts的實現的文章就介紹到這了,更多相關SpringBoot MybatisSQL過濾@Intercepts內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 91精品国产日韩91久久久久久 | 色六月婷婷| 中文字幕国产一区 | 欧美激情五月 | 亚洲在线免费 | 青青综合 | 日韩av成人在线 | 国产区一区 | 欧美视频在线观看免费 | 久久激情网 | 日本欧美久久久久免费播放网 | 综合网av| 高清av在线| 久久精品福利视频 | 97人人看| 91av在线播放 | 97在线观看视频 | 久久一区二区视频 | 免费在线观看毛片 | 在线观看小视频 | 成人在线视频免费 | 日韩图色 | 欧美又大又硬又粗bbbbb | 亚洲区在线 | 深夜福利在线播放 | 国产伦精品一区二区三区视频我 | 中文字幕在线一区 | 天天狠狠 | 国产精品福利在线观看 | 欧美在线综合 | 青草视频在线播放 | 婷久久| 51调教丨国产调教视频 | 日韩精品一区二区在线 | av网在线 | 福利视频网址 | 欧美一区二 | 自拍偷拍欧美 | 日韩精品三区 | 亚洲美女一区 | 日韩高清精品免费观看 |